summaryrefslogtreecommitdiff
path: root/src/clients.c
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2010-12-10 00:53:02 +0000
committerChristian Pointner <equinox@spreadspace.org>2010-12-10 00:53:02 +0000
commit7bee6be245cf1dd94a95ebbfdb909c02ae332e50 (patch)
treed1d97ea7234ccdc3f69246170fb38f9971cd5de4 /src/clients.c
parentsetting TCP_NODELAY option on sockets (diff)
deferring buffer allocation at client_add for easier error handling
git-svn-id: https://svn.spreadspace.org/tcpproxy/trunk@45 e61f0598-a718-4e21-a8f0-0aadfa62ad6b
Diffstat (limited to 'src/clients.c')
-rw-r--r--src/clients.c50
1 files changed, 21 insertions, 29 deletions
diff --git a/src/clients.c b/src/clients.c
index 1d529d6..dec47ae 100644
--- a/src/clients.c
+++ b/src/clients.c
@@ -71,7 +71,6 @@ void clients_clear(clients_t* list)
int clients_add(clients_t* list, int fd, const tcp_endpoint_t remote_end, const tcp_endpoint_t source_end)
{
-
if(!list)
return -1;
@@ -82,31 +81,9 @@ int clients_add(clients_t* list, int fd, const tcp_endpoint_t remote_end, const
}
element->fd_[0] = fd;
-
- element->write_buf_[0].buf_ = malloc(list->buffer_size_);
- if(!element->write_buf_[0].buf_) {
- free(element);
- close(fd);
- return -2;
- }
- element->write_buf_[0].length_ = list->buffer_size_;
- element->write_buf_offset_[0] = 0;
-
- element->write_buf_[1].buf_ = malloc(list->buffer_size_);
- if(!element->write_buf_[1].buf_) {
- free(element->write_buf_[0].buf_);
- free(element);
- close(fd);
- return -2;
- }
- element->write_buf_[1].length_ = list->buffer_size_;
- element->write_buf_offset_[1] = 0;
-
element->fd_[1] = socket(remote_end.addr_.ss_family, SOCK_STREAM, 0);
if(element->fd_[1] < 0) {
log_printf(INFO, "Error on socket(): %s, not adding client %d", strerror(errno), element->fd_[0]);
- free(element->write_buf_[0].buf_);
- free(element->write_buf_[1].buf_);
close(element->fd_[0]);
free(element);
return -1;
@@ -116,8 +93,6 @@ int clients_add(clients_t* list, int fd, const tcp_endpoint_t remote_end, const
if(setsockopt(element->fd_[0], IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) ||
setsockopt(element->fd_[1], IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on))) {
log_printf(ERROR, "Error on setsockopt(): %s", strerror(errno));
- free(element->write_buf_[0].buf_);
- free(element->write_buf_[1].buf_);
close(element->fd_[0]);
close(element->fd_[1]);
free(element);
@@ -127,8 +102,6 @@ int clients_add(clients_t* list, int fd, const tcp_endpoint_t remote_end, const
if(source_end.addr_.ss_family != AF_UNSPEC) {
if(bind(element->fd_[1], (struct sockaddr *)&(source_end.addr_), source_end.len_)==-1) {
log_printf(INFO, "Error on bind(): %s, not adding client %d", strerror(errno), element->fd_[0]);
- free(element->write_buf_[0].buf_);
- free(element->write_buf_[1].buf_);
close(element->fd_[0]);
close(element->fd_[1]);
free(element);
@@ -138,14 +111,33 @@ int clients_add(clients_t* list, int fd, const tcp_endpoint_t remote_end, const
if(connect(element->fd_[1], (struct sockaddr *)&(remote_end.addr_), remote_end.len_)==-1) {
log_printf(INFO, "Error on connect(): %s, not adding client %d", strerror(errno), element->fd_[0]);
- free(element->write_buf_[0].buf_);
- free(element->write_buf_[1].buf_);
close(element->fd_[0]);
close(element->fd_[1]);
free(element);
return -1;
}
+ element->write_buf_[0].buf_ = malloc(list->buffer_size_);
+ if(!element->write_buf_[0].buf_) {
+ close(element->fd_[0]);
+ close(element->fd_[1]);
+ free(element);
+ return -2;
+ }
+ element->write_buf_[0].length_ = list->buffer_size_;
+ element->write_buf_offset_[0] = 0;
+
+ element->write_buf_[1].buf_ = malloc(list->buffer_size_);
+ if(!element->write_buf_[1].buf_) {
+ free(element->write_buf_[0].buf_);
+ close(element->fd_[0]);
+ close(element->fd_[1]);
+ free(element);
+ return -2;
+ }
+ element->write_buf_[1].length_ = list->buffer_size_;
+ element->write_buf_offset_[1] = 0;
+
if(slist_add(&(list->list_), element) == NULL) {
free(element->write_buf_[0].buf_);
free(element->write_buf_[1].buf_);