diff options
author | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-12-24 01:51:44 +0100 |
---|---|---|
committer | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-12-24 01:51:44 +0100 |
commit | dbadc1ab26b3cca6b3697d9c07f284f582dde75d (patch) | |
tree | 9cd8e6ec3283247eeeccc1a4b6e1cae1c86e0048 /protocol.c | |
parent | c91ff9668142a9d0aac0bc364ccaf40611fa9c72 (diff) | |
download | MAC-Telnet-dbadc1ab26b3cca6b3697d9c07f284f582dde75d.tar.gz MAC-Telnet-dbadc1ab26b3cca6b3697d9c07f284f582dde75d.zip |
Fixed a bug where the mndp port/socket was not closed after a parseMNDP call.
Diffstat (limited to 'protocol.c')
-rw-r--r-- | protocol.c | 15 |
1 files changed, 12 insertions, 3 deletions
@@ -19,6 +19,7 @@ #include <string.h> #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include <linux/if_ether.h> #include <arpa/inet.h> #include <netinet/in.h> @@ -250,6 +251,9 @@ int queryMNDP(const char *identity, unsigned char *mac) { /* Open a UDP socket handle */ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + /* Allow to share socket */ + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); + /* Set initialize address/port */ memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; @@ -259,6 +263,7 @@ int queryMNDP(const char *identity, unsigned char *mac) { /* Bind to specified address/port */ if (bind(sock, (struct sockaddr *)&si_me, sizeof(si_me)) == -1) { fprintf(stderr, "Error binding to %s:%d\n", inet_ntoa(si_me.sin_addr), MT_MNDP_PORT); + close(sock); return 0; } @@ -281,7 +286,7 @@ int queryMNDP(const char *identity, unsigned char *mac) { while (1) { /* Timeout, in case we receive a lot of packets, but from the wrong routers */ if (time(0) - startTime > (fastlookup ? MT_MNDP_TIMEOUT : MT_MNDP_LONGTIMEOUT)) { - return 0; + goto done; } FD_ZERO(&read_fds); @@ -292,13 +297,13 @@ int queryMNDP(const char *identity, unsigned char *mac) { select(sock + 1, &read_fds, NULL, NULL, &timeout); if (!FD_ISSET(sock, &read_fds)) { - return 0; + goto done; } /* Read UDP packet */ length = recvfrom(sock, buff, MT_PACKET_LEN, 0, 0, 0); if (length < 0) { - return 0; + goto done; } /* Parse MNDP packet */ @@ -307,8 +312,12 @@ int queryMNDP(const char *identity, unsigned char *mac) { if (packet != NULL) { if (strcasecmp(identity, packet->identity) == 0) { memcpy(mac, packet->address, ETH_ALEN); + close(sock); return 1; } } } +done: + close(sock); + return 0; } |