From 1cc648b5ad2637a12c75881cb285248af22f2fa6 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sun, 14 Nov 2010 00:46:02 -0500 Subject: [PATCH] Clean up and comment listen_socket(). --- ndhc/socket.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ndhc/socket.c b/ndhc/socket.c index 2018c89..1c46a4a 100644 --- a/ndhc/socket.c +++ b/ndhc/socket.c @@ -89,6 +89,8 @@ int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *mac) return ret; } +/* Returns fd of new listen socket bound to @ip:@port on interface @inf + * on success, or -1 on failure. */ int listen_socket(unsigned int ip, int port, char *inf) { struct ifreq interface; @@ -98,15 +100,10 @@ int listen_socket(unsigned int ip, int port, char *inf) log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf); if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - log_error("socket call failed: %s", strerror(errno)); + log_error("listen_socket: socket failed: %s", strerror(errno)); goto out; } - memset(&addr, 0, sizeof addr); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - addr.sin_addr.s_addr = ip; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) goto out_fd; if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) == -1) @@ -118,6 +115,10 @@ int listen_socket(unsigned int ip, int port, char *inf) &interface, sizeof interface) < 0) goto out_fd; + memset(&addr, 0, sizeof addr); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = ip; if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) goto out_fd;