From: darkcoven Date: Sat, 12 Oct 2013 10:29:40 +0000 (+0200) Subject: Spy: handle two sockets X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=542715dfab6ccc9433697b65ae984a7919f1992b;p=ngadmin Spy: handle two sockets --- diff --git a/spy/src/spy.c b/spy/src/spy.c index a2c686e..1d291f2 100644 --- a/spy/src/spy.c +++ b/spy/src/spy.c @@ -1,6 +1,8 @@ #include #include +#include +#include #include #include @@ -8,42 +10,96 @@ #include +static void handler (int sig) +{ + (void)sig; + printf("interrupt\n"); +} + + int main (void) { - int err = 0, s; + int err = 0, sw_sock = -1, cl_sock = -1; List *attr; ListNode *ln; struct attr *at; struct nsdp_cmd nc; - struct sockaddr_in local; + struct sockaddr_in sw_local, cl_local; + struct pollfd fds[2]; + struct sigaction sa; + + sw_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sw_sock < 0) { + perror("socket"); + err = 1; + goto end; + }; - s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (s < 0) { + cl_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (cl_sock < 0) { perror("socket"); err = 1; goto end; }; - memset(&nc, 0, sizeof(struct nsdp_cmd)); - nc.remote_addr.sin_family = AF_INET; - nc.remote_addr.sin_port = htons(CLIENT_PORT); + memset(&sw_local, 0, sizeof(struct sockaddr_in)); + sw_local.sin_family = AF_INET; + sw_local.sin_addr.s_addr = htonl(INADDR_ANY); + sw_local.sin_port = htons(SWITCH_PORT); - memset(&local, 0, sizeof(struct sockaddr_in)); - local.sin_family = AF_INET; - local.sin_addr.s_addr = htonl(INADDR_ANY); - local.sin_port = htons(SWITCH_PORT); + cl_local = sw_local; + cl_local.sin_port = htons(CLIENT_PORT); - if (bind(s, (struct sockaddr*)&local, sizeof(struct sockaddr_in)) < 0) { + if (bind(sw_sock, (struct sockaddr*)&sw_local, sizeof(struct sockaddr_in)) < 0) { perror("bind"); err = 2; goto end; } + if (bind(cl_sock, (struct sockaddr*)&cl_local, sizeof(struct sockaddr_in)) < 0) { + perror("bind"); + err = 2; + goto end; + } + + fds[0].fd = sw_sock; + fds[0].events = POLLIN; + fds[0].revents = 0; + fds[1].fd = cl_sock; + fds[1].events = POLLIN; + fds[1].revents = 0; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = handler; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + attr = createEmptyList(); + while (1) { - attr = createEmptyList(); - recvNsdpPacket(s, &nc, attr, NULL); + err = poll(fds, 2, -1); + if (err < 0) { + perror("poll"); + break; + } else if (err == 0) { + continue; + } + + memset(&nc, 0, sizeof(struct nsdp_cmd)); + nc.remote_addr.sin_family = AF_INET; + + if (fds[0].revents & POLLIN) { + nc.remote_addr.sin_port = htons(CLIENT_PORT); + err = recvNsdpPacket(sw_sock, &nc, attr, NULL); + } else { + nc.remote_addr.sin_port = htons(SWITCH_PORT); + err = recvNsdpPacket(cl_sock, &nc, attr, NULL); + } + + if (err < 0) + continue; printf("---------------------------------\n"); @@ -54,14 +110,17 @@ int main (void) printf("received attribute code = %04X, length = %d\n", at->attr, at->size); } - destroyList(attr, (void(*)(void*))freeAttr); + clearList(attr, (void(*)(void*))freeAttr); printf("---------------------------------\n\n"); } - close(s); + destroyList(attr, (void(*)(void*))freeAttr); end: + close(sw_sock); + close(cl_sock); + return err; }