]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wserver.c
Removed debug message
[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 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[AC_NUM]);
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         
100         ac = (int) queue;
101         rem_addr_length=sizeof(rem_addr);
102
103         block_signals();
104         set_rt_prio(90-ac);
105
106         while (1) {
107                 struct cmsghdr *cmsg;
108
109                 iov.iov_base = &buff;
110                 iov.iov_len = sizeof(buff);
111                 msg.msg_name = (void*)&rem_addr;
112                 msg.msg_namelen = sizeof(rem_addr);
113                 msg.msg_iov = &iov;
114                 msg.msg_iovlen = 1;
115                 msg.msg_flags = 0;
116                 msg.msg_control = cbufrec;
117                 msg.msg_controllen = sizeof(cbufrec);
118
119                 while ((mlen = recvmsg(ac_sockfd[ac], &msg, 0)) < 0) {
120                         if (errno == EINTR) continue;
121                         perror("recvmsg");
122                         return NULL;
123                 }
124                 clock_gettime(CLOCK_REALTIME, &buff.msg.sendback_timestamp);
125
126
127                 if (opt_same_interface) {
128                         /* determine receiving interface */
129                         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
130                                 if (cmsg->cmsg_level == SOL_IP) {
131                                         if (cmsg->cmsg_type == IP_PKTINFO) {
132 /*                                              char spec_dst[20], addr[20]; */
133                                                 ipi = (struct in_pktinfo*)CMSG_DATA(cmsg);
134                                                 if (cmsg->cmsg_len <= sizeof(cbufsend)) {
135                                                         struct in_pktinfo *ipi2;
136                                                         msg.msg_control = cbufsend;
137                                                         msg.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
138                                                         cmsg = CMSG_FIRSTHDR(&msg);
139                                                         cmsg->cmsg_level = SOL_IP;
140                                                         cmsg->cmsg_type = IP_PKTINFO;
141                                                         cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
142                                                         /* Initialize the payload: */
143                                                         ipi2 = (struct in_pktinfo*)CMSG_DATA(cmsg);
144                                                         memset(ipi2, 0, sizeof(*ipi2));
145                                                         ipi2->ipi_ifindex = ipi->ipi_ifindex;
146
147                                                 } else {
148                                                         fprintf(stderr, "cbufsend too small\n");
149                                                         msg.msg_control = NULL;
150                                                         msg.msg_controllen = 0;
151                                                 }
152 /*                                              strncpy(spec_dst, inet_ntoa(ipi->ipi_spec_dst), sizeof(spec_dst)-1); */
153 /*                                              strncpy(addr,     inet_ntoa(ipi->ipi_addr),     sizeof(addr)-1); */
154 /*                                              printf("pktinfo if=%d %s %s\n", ipi->ipi_ifindex, spec_dst, addr); */
155                                         }
156                                 }
157                         }
158                 } else {
159                         msg.msg_control = NULL;
160                         msg.msg_controllen = 0;
161                 }
162 #ifdef DEBUG
163                 printf("%d",ac);
164                 fflush(stdout);
165 #endif
166                 receivers[ac].received++;
167                 msg.msg_iov->iov_len = mlen;
168                 while (sendmsg(ac_sockfd[ac], &msg, 0) < 0) {
169                             if (errno == EINTR) continue;
170                             perror("sendmsg");
171                             return NULL;
172                 }
173         }
174 }
175
176
177 int main(int argc, char *argv[])
178 {
179         int ac,rc;
180         pthread_attr_t attr;
181         pthread_t thread;
182
183         char opt;
184
185
186         while ((opt = getopt(argc, argv, "I")) != -1) {
187                 switch (opt) {
188                         case 'I':
189                                 opt_same_interface = true;
190                                 break;
191                         default:
192                                 fprintf(stderr, "Usage: %s [ options ]\n\n", argv[0]);
193                                 fprintf(stderr, "Options:\n");
194                                 fprintf(stderr, "    -I  send back through the same interface (bypass routing tables)\n");
195                                 exit(1);
196                 }
197         }
198         pthread_attr_init(&attr);
199
200         if (signal(SIGTERM, stopper) == SIG_ERR) {
201                 perror("Signal handler registration error");
202                 exit(1);
203         }
204                 
205         if (signal(SIGINT, stopper) == SIG_ERR) {
206                 perror("Signal handler registration error");
207                 exit(1);
208         }
209
210         for (ac = 0; ac < AC_NUM; ac++) {
211                 ac_sockfd[ac] = create_ac_socket(ac);
212                 rc = pthread_create(&thread, &attr, qhandler, (void*) ac); 
213
214         }
215         
216         while (1) {
217                 printf("\r");
218                 for (ac = 0; ac < AC_NUM; ac++) {
219                         int delta = receivers[ac].received - receivers[ac].last_received;
220                         receivers[ac].last_received = receivers[ac].received;
221                         fprintf(stderr, "%s %5d %4d/s ", ac_to_text[ac], receivers[ac].received, delta);
222                 }
223                 fflush(stdout);
224                 sleep(1);
225         }
226         printf("\n");
227
228         return 0;
229
230 }