]> rtime.felk.cvut.cz Git - frescor/fwp.git/blobdiff - wme_test/wserver.c
Allow wclient and wserver to send packets with a specified interface.
[frescor/fwp.git] / wme_test / wserver.c
index c6d0bfa1354bead028c07fc333fa20a4625c3545..03dbd855028f86196e999f7a8d2312032a2f41b5 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 
 #include <signal.h>
 #include <sys/wait.h>
@@ -14,6 +15,9 @@
 #include <string.h>
 #include <pthread.h>
 #include "common.h"
+#include <stdbool.h>
+
+bool opt_same_interface = false;
 
 int ac_sockfd[AC_NUM];
 
@@ -50,6 +54,15 @@ int create_ac_socket(unsigned int ac)
                return -1;
        }
 
+       if (opt_same_interface) {
+               int receive = 1;
+               if (setsockopt(sockfd, SOL_IP, IP_PKTINFO, &receive, sizeof(receive)) == -1) {
+                       perror("setsockopt: IP_PKTINFO");
+                       exit(1);
+               }
+       }
+
+
    //  bzero(&my_addr, sizeof(my_addr));
        memset(&my_addr,0, sizeof(my_addr));
        my_addr.sin_family = AF_INET;
@@ -79,6 +92,10 @@ void* qhandler(void* queue)
        struct  sockaddr_in rem_addr;
        int     mlen;
        unsigned int ac, rem_addr_length; 
+       char cbufrec[512], cbufsend[512];
+       struct iovec  iov;
+       struct msghdr msg;
+       struct in_pktinfo *ipi = NULL;
        
        ac = (int) queue;
        printf("AC= %d\n",ac);
@@ -88,23 +105,70 @@ void* qhandler(void* queue)
        set_rt_prio(90-ac);
 
        while (1) {
-               while ((mlen = recvfrom(ac_sockfd[ac], &buff, sizeof(buff) , 0, \
-                       (struct sockaddr*)&rem_addr, &rem_addr_length)) < 0) {
-                           if (errno == EINTR) continue;
-                           perror("Chyba pri prijimani pozadavku");
-                           return NULL;
+               struct cmsghdr *cmsg;
+
+               iov.iov_base = &buff;
+               iov.iov_len = sizeof(buff);
+               msg.msg_name = (void*)&rem_addr;
+               msg.msg_namelen = sizeof(rem_addr);
+               msg.msg_iov = &iov;
+               msg.msg_iovlen = 1;
+               msg.msg_flags = 0;
+               msg.msg_control = cbufrec;
+               msg.msg_controllen = sizeof(cbufrec);
+
+               while ((mlen = recvmsg(ac_sockfd[ac], &msg, 0)) < 0) {
+                       if (errno == EINTR) continue;
+                       perror("recvmsg");
+                       return NULL;
                }
                clock_gettime(CLOCK_REALTIME, &buff.msg.sendback_timestamp);
 
+
+               if (opt_same_interface) {
+                       /* determine receiving interface */
+                       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+                               if (cmsg->cmsg_level == SOL_IP) {
+                                       if (cmsg->cmsg_type == IP_PKTINFO) {
+/*                                             char spec_dst[20], addr[20]; */
+                                               ipi = (struct in_pktinfo*)CMSG_DATA(cmsg);
+                                               if (cmsg->cmsg_len <= sizeof(cbufsend)) {
+                                                       struct in_pktinfo *ipi2;
+                                                       msg.msg_control = cbufsend;
+                                                       msg.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
+                                                       cmsg = CMSG_FIRSTHDR(&msg);
+                                                       cmsg->cmsg_level = SOL_IP;
+                                                       cmsg->cmsg_type = IP_PKTINFO;
+                                                       cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
+                                                       /* Initialize the payload: */
+                                                       ipi2 = (struct in_pktinfo*)CMSG_DATA(cmsg);
+                                                       memset(ipi2, 0, sizeof(*ipi2));
+                                                       ipi2->ipi_ifindex = ipi->ipi_ifindex;
+
+                                               } else {
+                                                       fprintf(stderr, "cbufsend too small\n");
+                                                       msg.msg_control = NULL;
+                                                       msg.msg_controllen = 0;
+                                               }
+/*                                             strncpy(spec_dst, inet_ntoa(ipi->ipi_spec_dst), sizeof(spec_dst)-1); */
+/*                                             strncpy(addr,     inet_ntoa(ipi->ipi_addr),     sizeof(addr)-1); */
+/*                                             printf("pktinfo if=%d %s %s\n", ipi->ipi_ifindex, spec_dst, addr); */
+                                       }
+                               }
+                       }
+               } else {
+                       msg.msg_control = NULL;
+                       msg.msg_controllen = 0;
+               }
 #ifdef DEBUG
                printf("%d",ac);
                fflush(stdout);
 #endif
                receivers[ac].received++;
-               while (sendto(ac_sockfd[ac], &buff, mlen,0 ,(struct sockaddr*)&rem_addr, \
-                       sizeof(rem_addr)) < 0){
+               msg.msg_iov->iov_len = mlen;
+               while (sendmsg(ac_sockfd[ac], &msg, 0) < 0) {
                            if (errno == EINTR) continue;
-                           perror("Chyba pri zapisu");
+                           perror("sendmsg");
                            return NULL;
                }
        }
@@ -117,6 +181,21 @@ int main(int argc, char *argv[])
        pthread_attr_t attr;
        pthread_t thread;
 
+       char opt;
+
+
+       while ((opt = getopt(argc, argv, "I")) != -1) {
+               switch (opt) {
+                       case 'I':
+                               opt_same_interface = true;
+                               break;
+                       default:
+                               fprintf(stderr, "Usage: %s [ options ]\n\n", argv[0]);
+                               fprintf(stderr, "Options:\n");
+                               fprintf(stderr, "    -I  send back through the same interface (bypass routing tables)\n");
+                               exit(1);
+               }
+       }
        pthread_attr_init(&attr);
 
        if (signal(SIGTERM, stopper) == SIG_ERR) {