install a 'sliceip' initscript that cleans up stuff at boot-time for the sake of...
[vsys-scripts.git] / root-context / fd_tos.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <netinet/in.h>
6
7 #include "fdpass.h"
8
9 /*
10  * Definitions for IP type of service
11  */
12 #define IPTOS_LOWDELAY    0x10
13 #define IPTOS_THROUGHPUT  0x08
14 #define IPTOS_RELIABILITY 0x04
15 #define IPTOS_MINCOST     0x02
16 #define IPTOS_NORMALSVC   0x00
17
18 static void receive_argument(int control_fd, int *TOS_value)
19 {
20     if (recv(control_fd, TOS_value, sizeof(int), 0) != sizeof(int)) {
21         fprintf(stderr, "receiving the IP_TOS argument failed\n");
22         exit(-1);
23     }
24 }
25
26 int main(int argc, char *argv[]) 
27 {
28     int control_channel_fd, magic_socket;
29     int TOS_value = IPTOS_NORMALSVC;
30  
31     if (argc < 3) {
32         printf("This script is called by vsys.\n");
33         exit(1);
34     }
35
36     control_channel_fd = atoi(argv[2]);  
37     
38     /* receive IP_TOS paramater */
39     receive_argument(control_channel_fd, &TOS_value);
40
41     switch (TOS_value)
42     {
43         case IPTOS_NORMALSVC:
44         case IPTOS_MINCOST:
45         case IPTOS_RELIABILITY:
46         case IPTOS_THROUGHPUT:
47         case IPTOS_LOWDELAY:
48             break;
49         default:
50             fprintf(stderr, "IP_TOS value not known: %d\n", errno);
51             exit(1);
52     }
53
54     magic_socket =  receive_fd(control_channel_fd);
55     if (magic_socket == -1) { 
56         fprintf(stderr, "Error creating socket: %d\n", errno);
57         exit(1);
58     }
59
60     if (setsockopt(magic_socket, IPPROTO_IP, IP_TOS, &TOS_value, sizeof(TOS_value)) < 0 ) {
61         fprintf(stderr, "Error calling setsockopt for IPPROTO_IP: %d\n", errno);
62         exit(1);
63     }
64
65     send_fd(control_channel_fd, magic_socket);
66 }