Switch to production version of vsys
[vsys-wrappers.git] / packetseer / packetseer.c
1 #include <sys/socket.h>
2 #include <sys/un.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include "fdpass.h"
7 #include <dlfcn.h>
8
9 #define VSYS_PACKETSEER "/vsys/fd_packetseer.control"
10
11 int (*socket_orig)(int f, int p, int s);
12
13 int _init_pslib() {
14     socket_orig = &socket;
15     printf("Stored value of socket");
16 }
17
18 int socket(int f, int p, int s)
19 {
20     if (!socket_orig) {
21         void *handle = dlopen("/lib/libc.so.6",RTLD_LAZY);
22         if (!handle) {
23             fprintf(stderr,"Error loading libc.so.6\n");
24             return -1;
25         }
26         socket_orig = dlsym(handle, "socket");
27         if (!socket_orig) {
28             fprintf(stderr,"Error loading socket symbol\n");
29             return -1;
30         }
31     }
32
33     if (f == PF_PACKET) {
34         int sfd;
35         struct sockaddr_un addr;
36         int remotefd;
37
38         printf("[packetseer] Rerouting socket call via vsys...\n");
39         sfd = socket(AF_UNIX, SOCK_STREAM, 0);
40         if (sfd == -1) {
41             perror("Could not create UNIX socket\n");
42             exit(-1);
43         }
44
45         memset(&addr, 0, sizeof(struct sockaddr_un));
46         /* Clear structure */
47         addr.sun_family = AF_UNIX;
48         strncpy(addr.sun_path, VSYS_PACKETSEER,
49                 sizeof(addr.sun_path) - 1);
50
51         if (connect(sfd, (struct sockaddr *) &addr,
52                     sizeof(struct sockaddr_un)) == -1) {
53             perror("Could not connect to Vsys control socket");
54             exit(-1);
55         }
56
57         remotefd = receive_fd(sfd);
58         printf("Received fd %d",remotefd);
59         return remotefd;
60     }
61     else
62         return (*socket_orig)(f, p, s);
63 }
64