From 16e3296617d9f6c70aecb0c79ed861b6424330e7 Mon Sep 17 00:00:00 2001 From: Quentin Armitage Date: Wed, 9 Dec 2015 15:35:05 +0000 Subject: Simplify some IPv4/IPv6 code. Code blocks were (unnecessarily) repeated in functions which handled both IPv4 and IPv6 situations. Signed-off-by: Quentin Armitage (cherry picked from commit 3e0248ccd2bf48384f45c5c69703c4e56ece2156) --- genhash/http.c | 15 +++------------ genhash/layer4.c | 50 +++++++++++++------------------------------------- genhash/main.c | 2 +- 3 files changed, 17 insertions(+), 50 deletions(-) diff --git a/genhash/http.c b/genhash/http.c index 6d08dfd..f70095d 100644 --- a/genhash/http.c +++ b/genhash/http.c @@ -318,18 +318,9 @@ http_request_thread(thread_t * thread) ntohs(req->addr_port)); } - if(req->dst){ - if(req->dst->ai_family == AF_INET6 && !req->vhost) { - snprintf(str_request, GET_BUFFER_LENGTH, REQUEST_TEMPLATE_IPV6, - req->url, request_host, request_host_port); - } else { - snprintf(str_request, GET_BUFFER_LENGTH, REQUEST_TEMPLATE, - req->url, request_host, request_host_port); - } - } else { - snprintf(str_request, GET_BUFFER_LENGTH, REQUEST_TEMPLATE, - req->url, request_host, request_host_port); - } + snprintf(str_request, GET_BUFFER_LENGTH, + (req->dst && req->dst->ai_family == AF_INET6 && !req->vhost) ? REQUEST_TEMPLATE_IPV6 : REQUEST_TEMPLATE, + req->url, request_host, request_host_port); FREE(request_host_port); diff --git a/genhash/layer4.c b/genhash/layer4.c index 3f36037..da395d4 100644 --- a/genhash/layer4.c +++ b/genhash/layer4.c @@ -61,26 +61,15 @@ tcp_connect(int fd, REQ * req_obj) val = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, val | O_NONBLOCK); - if(req_obj->dst){ - if(req_obj->dst->ai_family == AF_INET6) { - long_inet = sizeof (struct sockaddr_in6); - memset(&adr_serv6, 0, long_inet); - adr_serv6.sin6_family = req_obj->dst->ai_family; - adr_serv6.sin6_port = req_obj->addr_port; - inet_pton(AF_INET6, req_obj->ipaddress, &adr_serv6.sin6_addr); - - /* Call connect function. */ - ret = connect(fd, (struct sockaddr *) &adr_serv6, long_inet); - } else { - long_inet = sizeof (struct sockaddr_in); - memset(&adr_serv, 0, long_inet); - adr_serv.sin_family = req_obj->dst->ai_family; - adr_serv.sin_port = req_obj->addr_port; - inet_pton(AF_INET, req_obj->ipaddress, &adr_serv.sin_addr); - - /* Call connect function. */ - ret = connect(fd, (struct sockaddr *) &adr_serv, long_inet); - } + if(req_obj->dst && req_obj->dst->ai_family == AF_INET6) { + long_inet = sizeof (struct sockaddr_in6); + memset(&adr_serv6, 0, long_inet); + adr_serv6.sin6_family = AF_INET6; + adr_serv6.sin6_port = req_obj->addr_port; + inet_pton(AF_INET6, req_obj->ipaddress, &adr_serv6.sin6_addr); + + /* Call connect function. */ + ret = connect(fd, (struct sockaddr *) &adr_serv6, long_inet); } else { long_inet = sizeof (struct sockaddr_in); memset(&adr_serv, 0, long_inet); @@ -242,23 +231,10 @@ tcp_connect_thread(thread_t * thread) { SOCK *sock_obj = THREAD_ARG(thread); - if(req->dst){ - if(req->dst->ai_family == AF_INET6) { - if ((sock_obj->fd = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP)) == -1) { - DBG("WEB connection fail to create socket.\n"); - return 0; - } - } else { - if ((sock_obj->fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP)) == -1) { - DBG("WEB connection fail to create socket.\n"); - return 0; - } - } - } else { - if ((sock_obj->fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP)) == -1) { - DBG("WEB connection fail to create socket.\n"); - return 0; - } + if ((sock_obj->fd = socket((req->dst && req->dst->ai_family == AF_INET6) ? AF_INET6 : AF_INET, + SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP)) == -1) { + DBG("WEB connection fail to create socket.\n"); + return 0; } sock->status = tcp_connect(sock_obj->fd, req); diff --git a/genhash/main.c b/genhash/main.c index 8e0384f..21f5fce 100644 --- a/genhash/main.c +++ b/genhash/main.c @@ -146,7 +146,7 @@ parse_cmdline(int argc, char **argv, REQ * req_obj) if(res->ai_family == AF_INET) { req_obj->dst = res; ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr; - inet_ntop (res->ai_family, ptr, req_obj->ipaddress, INET6_ADDRSTRLEN); + inet_ntop (res->ai_family, ptr, req_obj->ipaddress, INET_ADDRSTRLEN); } else if (res->ai_family == AF_INET6) { req_obj->dst = res; ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr; -- 1.7.12.1