Build fd_netlink
[vsys-scripts.git] / fd_bmsocket.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include "fdpass.h"
7
8 #define MAX_BUFSIZE (32*1024*1024)
9
10 /*------------------------------------------------------------------*/
11 void
12 receive_argument(int control_fd, int *rcvbuf, int *sndbuf)
13 {
14   if (recv(control_fd, rcvbuf, sizeof(int), 0) != sizeof(int)) {
15     fprintf(stderr, "receiving the first argument failed\n");
16     exit(-1);
17   }
18   if (recv(control_fd, sndbuf, sizeof(int), 0) != sizeof(int)) {
19     fprintf(stderr, "receiving the first argument failed\n");
20     exit(-1);
21   }
22 }
23 /*------------------------------------------------------------------*/
24 int 
25 main(int argc, char *argv[]) 
26 {
27     int control_channel_fd, magic_socket;
28     int rcvbufsize = 0, sndbufsize = 0;
29     
30     if (argc < 3) {
31         printf("This script is called by vsys.\n");
32         exit(1);
33     }
34
35     sscanf(argv[2],"%d", &control_channel_fd);
36
37     /* receive paramaters: rcvbufsize and sndbufsize */
38     receive_argument(control_channel_fd, &rcvbufsize, &sndbufsize);
39     if (rcvbufsize > MAX_BUFSIZE)
40       rcvbufsize = MAX_BUFSIZE;
41     if (sndbufsize > MAX_BUFSIZE)
42       sndbufsize = MAX_BUFSIZE;
43
44     magic_socket = socket(AF_INET, SOCK_STREAM, 0);
45     if (magic_socket == -1) {
46       fprintf(stderr, "Error creating socket: %d\n", errno);
47       exit(1);
48     }
49
50     /* buffer size <= 0 means we should ignore the parameter */
51     if (rcvbufsize > 0) {
52       if (setsockopt(magic_socket, 
53                      SOL_SOCKET, 
54                      SO_RCVBUFFORCE, 
55                      &rcvbufsize, sizeof(unsigned int))) {
56         fprintf(stderr, "Error calling setsockopt for RCVBUFFORCE: %d\n", 
57                 errno);
58         exit(1);
59       }
60     }
61     if (sndbufsize > 0) {
62       if (setsockopt(magic_socket, 
63                      SOL_SOCKET, 
64                      SO_SNDBUFFORCE, 
65                      &sndbufsize, sizeof(unsigned int))) {
66         fprintf(stderr, "Error calling setsockopt for SNDBUFFORCE: %d\n", 
67                 errno);
68         exit(1);
69       }
70     }
71
72     send_fd(control_channel_fd, magic_socket);
73 }