]> git.sur5r.net Git - ngadmin/blob - spy/src/spy.c
Spy: handle two sockets
[ngadmin] / spy / src / spy.c
1
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <poll.h>
5 #include <signal.h>
6 #include <arpa/inet.h>
7
8 #include <nsdp/protocol.h>
9 #include <nsdp/attr.h>
10 #include <nsdp/net.h>
11
12
13 static void handler (int sig)
14 {
15         (void)sig;
16         printf("interrupt\n");
17 }
18
19
20 int main (void)
21 {
22         int err = 0, sw_sock = -1, cl_sock = -1;
23         List *attr;
24         ListNode *ln;
25         struct attr *at;
26         struct nsdp_cmd nc;
27         struct sockaddr_in sw_local, cl_local;
28         struct pollfd fds[2];
29         struct sigaction sa;
30         
31         
32         sw_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
33         if (sw_sock < 0) {
34                 perror("socket");
35                 err = 1;
36                 goto end;
37         };
38         
39         cl_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
40         if (cl_sock < 0) {
41                 perror("socket");
42                 err = 1;
43                 goto end;
44         };
45         
46         
47         memset(&sw_local, 0, sizeof(struct sockaddr_in));
48         sw_local.sin_family = AF_INET;
49         sw_local.sin_addr.s_addr = htonl(INADDR_ANY);
50         sw_local.sin_port = htons(SWITCH_PORT);
51         
52         cl_local = sw_local;
53         cl_local.sin_port = htons(CLIENT_PORT);
54         
55         if (bind(sw_sock, (struct sockaddr*)&sw_local, sizeof(struct sockaddr_in)) < 0) {
56                 perror("bind");
57                 err = 2;
58                 goto end;
59         }
60         
61         if (bind(cl_sock, (struct sockaddr*)&cl_local, sizeof(struct sockaddr_in)) < 0) {
62                 perror("bind");
63                 err = 2;
64                 goto end;
65         }
66         
67         fds[0].fd = sw_sock;
68         fds[0].events = POLLIN;
69         fds[0].revents = 0;
70         fds[1].fd = cl_sock;
71         fds[1].events = POLLIN;
72         fds[1].revents = 0;
73         
74         memset(&sa, 0, sizeof(struct sigaction));
75         sa.sa_handler = handler;
76         sigaction(SIGINT, &sa, NULL);
77         sigaction(SIGTERM, &sa, NULL);
78         
79         attr = createEmptyList();
80         
81         while (1) {
82                 err = poll(fds, 2, -1);
83                 if (err < 0) {
84                         perror("poll");
85                         break;
86                 } else if (err == 0) {
87                         continue;
88                 }
89                 
90                 memset(&nc, 0, sizeof(struct nsdp_cmd));
91                 nc.remote_addr.sin_family = AF_INET;
92                 
93                 if (fds[0].revents & POLLIN) {
94                         nc.remote_addr.sin_port = htons(CLIENT_PORT);
95                         err = recvNsdpPacket(sw_sock, &nc, attr, NULL);
96                 } else {
97                         nc.remote_addr.sin_port = htons(SWITCH_PORT);
98                         err = recvNsdpPacket(cl_sock, &nc, attr, NULL);
99                 }
100                 
101                 if (err < 0)
102                         continue;
103                 
104                 printf("---------------------------------\n");
105                 
106                 printf("received %d attribute(s)\n", attr->count);
107                 
108                 for (ln = attr->first; ln != NULL; ln = ln->next) {
109                         at = ln->data;
110                         printf("received attribute code = %04X, length = %d\n", at->attr, at->size);
111                 }
112                 
113                 clearList(attr, (void(*)(void*))freeAttr);
114                 
115                 printf("---------------------------------\n\n");
116         }
117         
118         destroyList(attr, (void(*)(void*))freeAttr);
119         
120 end:
121         close(sw_sock);
122         close(cl_sock);
123         
124         return err;
125 }
126