r13655: Use new name of build header
[samba.git] / source4 / lib / util / util_sock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Tim Potter      2000-2001
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "system/network.h"
24
25 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
26
27 static const struct {
28         const char *name;
29         int level;
30         int option;
31         int value;
32         int opttype;
33 } socket_options[] = {
34   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
35   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
36   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
37 #ifdef TCP_NODELAY
38   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
39 #endif
40 #ifdef IPTOS_LOWDELAY
41   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
42 #endif
43 #ifdef IPTOS_THROUGHPUT
44   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
45 #endif
46 #ifdef SO_REUSEPORT
47   {"SO_REUSEPORT",      SOL_SOCKET,    SO_REUSEPORT,    0,                 OPT_BOOL},
48 #endif
49 #ifdef SO_SNDBUF
50   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
51 #endif
52 #ifdef SO_RCVBUF
53   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
54 #endif
55 #ifdef SO_SNDLOWAT
56   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
57 #endif
58 #ifdef SO_RCVLOWAT
59   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
60 #endif
61 #ifdef SO_SNDTIMEO
62   {"SO_SNDTIMEO",       SOL_SOCKET,    SO_SNDTIMEO,     0,                 OPT_INT},
63 #endif
64 #ifdef SO_RCVTIMEO
65   {"SO_RCVTIMEO",       SOL_SOCKET,    SO_RCVTIMEO,     0,                 OPT_INT},
66 #endif
67   {NULL,0,0,0,0}};
68
69
70 /****************************************************************************
71  Set user socket options.
72 ****************************************************************************/
73 void set_socket_options(int fd, const char *options)
74 {
75         const char **options_list = str_list_make(NULL, options, " \t,");
76         int j;
77
78         if (!options_list)
79                 return;
80
81         for (j = 0; options_list[j]; j++) {
82                 const char *tok = options_list[j];
83                 int ret=0,i;
84                 int value = 1;
85                 char *p;
86                 BOOL got_value = False;
87
88                 if ((p = strchr_m(tok,'='))) {
89                         *p = 0;
90                         value = atoi(p+1);
91                         got_value = True;
92                 }
93
94                 for (i=0;socket_options[i].name;i++)
95                         if (strequal(socket_options[i].name,tok))
96                                 break;
97
98                 if (!socket_options[i].name) {
99                         DEBUG(0,("Unknown socket option %s\n",tok));
100                         continue;
101                 }
102
103                 switch (socket_options[i].opttype) {
104                 case OPT_BOOL:
105                 case OPT_INT:
106                         ret = setsockopt(fd,socket_options[i].level,
107                                                 socket_options[i].option,(char *)&value,sizeof(int));
108                         break;
109
110                 case OPT_ON:
111                         if (got_value)
112                                 DEBUG(0,("syntax error - %s does not take a value\n",tok));
113
114                         {
115                                 int on = socket_options[i].value;
116                                 ret = setsockopt(fd,socket_options[i].level,
117                                                         socket_options[i].option,(char *)&on,sizeof(int));
118                         }
119                         break;    
120                 }
121       
122                 if (ret != 0)
123                         DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
124         }
125
126         talloc_free(options_list);
127 }
128