summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2010-11-28 23:24:16 +0000
committerChristian Pointner <equinox@spreadspace.org>2010-11-28 23:24:16 +0000
commit64ef9b77afabde7f0856d908489a5255107606ff (patch)
treea80035ed6f920e09f1c08762c831bd2479c83aba
parentreplaced inet_ntop with getnameinfo (diff)
added length field to tcp_endpoint_t
git-svn-id: https://svn.spreadspace.org/tcpproxy/trunk@15 e61f0598-a718-4e21-a8f0-0aadfa62ad6b
-rw-r--r--src/clients.c22
-rw-r--r--src/clients.h2
-rw-r--r--src/listener.c23
-rw-r--r--src/tcp.c4
-rw-r--r--src/tcp.h5
5 files changed, 26 insertions, 30 deletions
diff --git a/src/clients.c b/src/clients.c
index 6db10fc..7179961 100644
--- a/src/clients.c
+++ b/src/clients.c
@@ -63,7 +63,7 @@ void clients_clear(clients_t* list)
slist_clear(list);
}
-int clients_add(clients_t* list, int fd, const tcp_endpoint_t* remote_end, const tcp_endpoint_t* source_end)
+int clients_add(clients_t* list, int fd, const tcp_endpoint_t remote_end, const tcp_endpoint_t source_end)
{
if(!list)
@@ -79,7 +79,7 @@ int clients_add(clients_t* list, int fd, const tcp_endpoint_t* remote_end, const
element->write_buf_len_[1] = 0;
element->fd_[0] = fd;
- element->fd_[1] = socket(remote_end->ss_family, SOCK_STREAM, 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]);
close(element->fd_[0]);
@@ -87,14 +87,8 @@ int clients_add(clients_t* list, int fd, const tcp_endpoint_t* remote_end, const
return -1;
}
- if(source_end->ss_family != AF_UNSPEC) {
- socklen_t socklen = sizeof(*source_end);
- if(source_end->ss_family == AF_INET)
- socklen = sizeof(struct sockaddr_in);
- else if (source_end->ss_family == AF_INET6)
- socklen = sizeof(struct sockaddr_in6);
-
- if(bind(element->fd_[1], (struct sockaddr *)source_end, socklen)==-1) {
+ 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]);
close(element->fd_[0]);
close(element->fd_[1]);
@@ -103,13 +97,7 @@ int clients_add(clients_t* list, int fd, const tcp_endpoint_t* remote_end, const
}
}
- socklen_t socklen = sizeof(*remote_end);
- if(remote_end->ss_family == AF_INET)
- socklen = sizeof(struct sockaddr_in);
- else if (remote_end->ss_family == AF_INET6)
- socklen = sizeof(struct sockaddr_in6);
-
- if(connect(element->fd_[1], (struct sockaddr *)remote_end, socklen)==-1) {
+ 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]);
close(element->fd_[0]);
close(element->fd_[1]);
diff --git a/src/clients.h b/src/clients.h
index 8a78aa2..2515837 100644
--- a/src/clients.h
+++ b/src/clients.h
@@ -47,7 +47,7 @@ typedef slist_t clients_t;
int clients_init(clients_t* list);
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);
+int clients_add(clients_t* list, int fd, const tcp_endpoint_t remote_end, const tcp_endpoint_t source_end);
void clients_remove(clients_t* list, int fd);
client_t* clients_find(clients_t* list, int fd);
void clients_print(clients_t* list);
diff --git a/src/listener.c b/src/listener.c
index 5e34c3d..0f7f937 100644
--- a/src/listener.c
+++ b/src/listener.c
@@ -101,15 +101,20 @@ int listener_add(listeners_t* list, const char* laddr, const char* lport, const
ret = -2;
break;
}
- memset(&(element->remote_end_), 0, sizeof(element->remote_end_));
- memcpy(&(element->remote_end_), re->ai_addr, re->ai_addrlen);
-
- memset(&(element->source_end_), 0, sizeof(element->source_end_));
- if(se) memcpy(&(element->source_end_), se->ai_addr, se->ai_addrlen);
- else element->source_end_.ss_family = AF_UNSPEC;
+ memset(&(element->remote_end_.addr_), 0, sizeof(element->remote_end_.addr_));
+ memcpy(&(element->remote_end_.addr_), re->ai_addr, re->ai_addrlen);
+ element->remote_end_.len_ = re->ai_addrlen;
+
+ memset(&(element->source_end_.addr_), 0, sizeof(element->source_end_.addr_));
+ if(se) {
+ memcpy(&(element->source_end_.addr_), se->ai_addr, se->ai_addrlen);
+ element->source_end_.len_ = se->ai_addrlen;
+ }
+ else element->source_end_.addr_.ss_family = AF_UNSPEC;
- memset(&(element->local_end_), 0, sizeof(element->local_end_));
- memcpy(&(element->local_end_), l->ai_addr, l->ai_addrlen);
+ memset(&(element->local_end_.addr_), 0, sizeof(element->local_end_.addr_));
+ memcpy(&(element->local_end_.addr_), l->ai_addr, l->ai_addrlen);
+ element->local_end_.len_ = l->ai_addrlen;
element->fd_ = socket(l->ai_family, SOCK_STREAM, 0);
if(element->fd_ < 0) {
@@ -249,7 +254,7 @@ int listener_handle_accept(listeners_t* list, clients_t* clients, fd_set* set)
if(rs) free(rs);
FD_CLR(l->fd_, set);
- clients_add(clients, new_client, &(l->remote_end_), &(l->source_end_));
+ clients_add(clients, new_client, l->remote_end_, l->source_end_);
}
tmp = tmp->next_;
}
diff --git a/src/tcp.c b/src/tcp.c
index c994eba..ea2af7d 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -44,7 +44,7 @@ char* tcp_endpoint_to_string(tcp_endpoint_t e)
char addrstr[INET6_ADDRSTRLEN + 1], portstr[6], *ret;
char addrport_sep = ':';
- switch(e.ss_family)
+ switch(e.addr_.ss_family)
{
case AF_INET: addrport_sep = ':'; break;
case AF_INET6: addrport_sep = '.'; break;
@@ -52,7 +52,7 @@ char* tcp_endpoint_to_string(tcp_endpoint_t e)
default: asprintf(&ret, "unknown address type"); return ret;
}
- int errcode = getnameinfo((struct sockaddr *)&e, sizeof(e), addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST | NI_NUMERICSERV);
+ int errcode = getnameinfo((struct sockaddr *)&(e.addr_), e.len_, addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST | NI_NUMERICSERV);
if (errcode != 0) return NULL;
asprintf(&ret, "%s%c%s", addrstr, addrport_sep ,portstr);
return ret;
diff --git a/src/tcp.h b/src/tcp.h
index f56ff46..ce559ef 100644
--- a/src/tcp.h
+++ b/src/tcp.h
@@ -34,7 +34,10 @@
enum resolv_type_enum { ANY, IPV4_ONLY, IPV6_ONLY };
typedef enum resolv_type_enum resolv_type_t;
-typedef struct sockaddr_storage tcp_endpoint_t;
+typedef struct {
+ socklen_t len_;
+ struct sockaddr_storage addr_;
+} tcp_endpoint_t;
char* tcp_endpoint_to_string(tcp_endpoint_t e);
struct addrinfo* tcp_resolve_endpoint(const char* addr, const char* port, resolv_type_t rt);