8 #include <nsdp/protocol.h>
13 static const char* code_str (unsigned char code)
18 return "read request";
24 return "write request";
35 static const char* error_str (unsigned char err)
45 case ERROR_INVALID_VALUE:
46 return "invalid value";
49 return "access denied";
57 static void print_packet (const List *attr, const struct nsdp_cmd *nc)
60 const struct attr *at;
63 printf("---------------------------------\n");
64 printf("code = %s (%u)\n", code_str(nc->code), nc->code);
65 printf("error = %s (%u)\n", error_str(nc->error), nc->error);
66 if (nc->attr_error != 0)
67 printf("erroneous attribute = %04X\n", nc->attr_error);
68 printf("source address = %s:%u\n", inet_ntoa(nc->remote_addr.sin_addr), ntohs(nc->remote_addr.sin_port));
69 printf("client MAC = %s\n", ether_ntoa(&nc->client_mac));
70 printf("switch MAC = %s\n", ether_ntoa(&nc->switch_mac));
71 printf("sequence number = %u\n\n", nc->seqnum);
72 printf("received %d attribute(s)\n", attr->count);
74 for (ln = attr->first; ln != NULL; ln = ln->next) {
76 printf("received attribute code = %04X, length = %d\n", at->attr, at->size);
79 printf("---------------------------------\n\n");
83 static void handler (int sig)
86 printf("interrupt\n");
92 int err = 0, sw_sock = -1, cl_sock = -1;
95 struct sockaddr_in sw_local, cl_local;
100 sw_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
107 cl_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
115 memset(&sw_local, 0, sizeof(struct sockaddr_in));
116 sw_local.sin_family = AF_INET;
117 sw_local.sin_addr.s_addr = htonl(INADDR_ANY);
118 sw_local.sin_port = htons(SWITCH_PORT);
121 cl_local.sin_port = htons(CLIENT_PORT);
123 if (bind(sw_sock, (struct sockaddr*)&sw_local, sizeof(struct sockaddr_in)) < 0) {
129 if (bind(cl_sock, (struct sockaddr*)&cl_local, sizeof(struct sockaddr_in)) < 0) {
136 fds[0].events = POLLIN;
139 fds[1].events = POLLIN;
142 memset(&sa, 0, sizeof(struct sigaction));
143 sa.sa_handler = handler;
144 sigaction(SIGINT, &sa, NULL);
145 sigaction(SIGTERM, &sa, NULL);
147 attr = createEmptyList();
150 err = poll(fds, 2, -1);
154 } else if (err == 0) {
158 memset(&nc, 0, sizeof(struct nsdp_cmd));
159 nc.remote_addr.sin_family = AF_INET;
161 if (fds[0].revents & POLLIN) {
162 nc.remote_addr.sin_port = htons(CLIENT_PORT);
163 err = recvNsdpPacket(sw_sock, &nc, attr, NULL);
165 nc.remote_addr.sin_port = htons(SWITCH_PORT);
166 err = recvNsdpPacket(cl_sock, &nc, attr, NULL);
172 print_packet(attr, &nc);
174 clearList(attr, (void(*)(void*))freeAttr);
177 destroyList(attr, (void(*)(void*))freeAttr);