]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wserver.c
1649ed5a97eee9fbcdec456460162e37a389efac
[frescor/fwp.git] / wme_test / wserver.c
1 #include <stdlib.h>
2 #include <errno.h>
3
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8
9 #include <signal.h>
10 #include <sys/wait.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <time.h>
15 #include <string.h>
16 #include <pthread.h>
17 #include "common.h"
18 #include <stdbool.h>
19
20 bool opt_same_interface = false;
21
22 static int ac_sockfd[AC_NUM];
23
24 struct receiver {
25         unsigned received, last_received;
26 } receivers[AC_NUM];
27
28
29 void stopper()
30 {
31         int i;
32
33         for (i = 0; i < AC_NUM; i++) 
34                 close(ac_sockfd[i]);
35
36         exit(0);
37 }
38
39 int create_ac_socket(unsigned int ac) 
40 {
41         struct sockaddr_in my_addr;
42         int sockfd;
43         unsigned int yes=1, tos;
44
45
46         if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
47         {
48                 perror("Socket nelze otevrit");
49                 return -1;
50         }
51         
52         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
53                 perror("Chyba v nastaveni soketu");
54                 return -1;
55         }
56
57         if (opt_same_interface) {
58                 int receive = 1;
59                 if (setsockopt(sockfd, SOL_IP, IP_PKTINFO, &receive, sizeof(receive)) == -1) {
60                         perror("setsockopt: IP_PKTINFO");
61                         exit(1);
62                 }
63         }
64
65
66    //   bzero(&my_addr, sizeof(my_addr));
67         memset(&my_addr,0, sizeof(my_addr));
68         my_addr.sin_family = AF_INET;
69         my_addr.sin_addr.s_addr = INADDR_ANY;
70         my_addr.sin_port = htons(BASE_PORT + ac);
71         
72         if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
73                 perror("Chyba v bind");
74                 close(sockfd);
75                 return -1;
76         }
77         
78         //tos = ((AC_NUM - ac) *2 - 1)*32;
79         tos = ac_to_tos[ac];
80         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
81                 perror("Unable to set TOS");
82                 close(sockfd);
83                 return -1;
84         }
85
86         return sockfd;
87 }
88
89 void* qhandler(void* queue)
90 {
91         union msg_buff buff;
92         struct  sockaddr_in rem_addr;
93         int     mlen;
94         unsigned int ac, rem_addr_length; 
95         char cbufrec[512], cbufsend[512];
96         struct iovec  iov;
97         struct msghdr msg;
98         struct in_pktinfo *ipi = NULL;
99         struct timespec ts;
100         
101         ac = (int) queue;
102         rem_addr_length=sizeof(rem_addr);
103
104         block_signals();
105         set_rt_prio(90-ac);
106
107         while (1) {
108                 struct cmsghdr *cmsg;
109
110                 iov.iov_base = &buff;
111                 iov.iov_len = sizeof(buff);
112                 msg.msg_name = (void*)&rem_addr;
113                 msg.msg_namelen = sizeof(rem_addr);
114                 msg.msg_iov = &iov;
115                 msg.msg_iovlen = 1;
116                 msg.msg_flags = 0;
117                 msg.msg_control = cbufrec;
118                 msg.msg_controllen = sizeof(cbufrec);
119
120                 while ((mlen = recvmsg(ac_sockfd[ac], &msg, 0)) < 0) {
121                         if (errno == EINTR) continue;
122                         perror("recvmsg");
123                         return NULL;
124                 }
125                 clock_gettime(CLOCK_REALTIME, &ts);
126                 buff.msg.sendback_timestamp = ts.tv_sec*1000000000 + ts.tv_nsec;
127
128
129                 if (opt_same_interface) {
130                         /* determine receiving interface */
131                         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
132                                 if (cmsg->cmsg_level == SOL_IP) {
133                                         if (cmsg->cmsg_type == IP_PKTINFO) {
134 /*                                              char spec_dst[20], addr[20]; */
135                                                 ipi = (struct in_pktinfo*)CMSG_DATA(cmsg);
136                                                 if (cmsg->cmsg_len <= sizeof(cbufsend)) {
137                                                         struct in_pktinfo *ipi2;
138                                                         msg.msg_control = cbufsend;
139                                                         msg.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
140                                                         cmsg = CMSG_FIRSTHDR(&msg);
141                                                         cmsg->cmsg_level = SOL_IP;
142                                                         cmsg->cmsg_type = IP_PKTINFO;
143                                                         cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
144                                                         /* Initialize the payload: */
145                                                         ipi2 = (struct in_pktinfo*)CMSG_DATA(cmsg);
146                                                         memset(ipi2, 0, sizeof(*ipi2));
147                                                         ipi2->ipi_ifindex = ipi->ipi_ifindex;
148
149                                                 } else {
150                                                         fprintf(stderr, "cbufsend too small\n");
151                                                         msg.msg_control = NULL;
152                                                         msg.msg_controllen = 0;
153                                                 }
154 /*                                              strncpy(spec_dst, inet_ntoa(ipi->ipi_spec_dst), sizeof(spec_dst)-1); */
155 /*                                              strncpy(addr,     inet_ntoa(ipi->ipi_addr),     sizeof(addr)-1); */
156 /*                                              printf("pktinfo if=%d %s %s\n", ipi->ipi_ifindex, spec_dst, addr); */
157                                         }
158                                 }
159                         }
160                 } else {
161                         msg.msg_control = NULL;
162                         msg.msg_controllen = 0;
163                 }
164 #ifdef DEBUG
165                 printf("%d",ac);
166                 fflush(stdout);
167 #endif
168                 receivers[ac].received++;
169                 msg.msg_iov->iov_len = mlen;
170 #ifdef WITH_FWP
171                 /* resp_port is already stored in network order */
172                 rem_addr.sin_port = buff.msg.resp_port;
173 #endif
174                 while (sendmsg(ac_sockfd[ac], &msg, 0) < 0) {
175                             if (errno == EINTR) continue;
176                             perror("sendmsg");
177                             return NULL;
178                 }
179         }
180 }
181
182
183 int main(int argc, char *argv[])
184 {
185         int ac,rc;
186         pthread_attr_t attr;
187         pthread_t thread;
188
189         char opt;
190
191
192         while ((opt = getopt(argc, argv, "I")) != -1) {
193                 switch (opt) {
194                         case 'I':
195                                 opt_same_interface = true;
196                                 break;
197                         default:
198                                 fprintf(stderr, "Usage: %s [ options ]\n\n", argv[0]);
199                                 fprintf(stderr, "Options:\n");
200                                 fprintf(stderr, "    -I  send back through the same interface (bypass routing tables)\n");
201                                 exit(1);
202                 }
203         }
204         pthread_attr_init(&attr);
205
206         if (signal(SIGTERM, stopper) == SIG_ERR) {
207                 perror("Signal handler registration error");
208                 exit(1);
209         }
210                 
211         if (signal(SIGINT, stopper) == SIG_ERR) {
212                 perror("Signal handler registration error");
213                 exit(1);
214         }
215
216         for (ac = 0; ac < AC_NUM; ac++) {
217                 ac_sockfd[ac] = create_ac_socket(ac);
218                 rc = pthread_create(&thread, &attr, qhandler, (void*) ac); 
219
220         }
221         
222         while (1) {
223                 printf("\r");
224                 for (ac = 0; ac < AC_NUM; ac++) {
225                         int delta = receivers[ac].received - receivers[ac].last_received;
226                         receivers[ac].last_received = receivers[ac].received;
227                         fprintf(stderr, "%s %5d %4d/s ", ac_to_text[ac], receivers[ac].received, delta);
228                 }
229                 fflush(stdout);
230                 sleep(1);
231         }
232         printf("\n");
233
234         return 0;
235
236 }