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