Browse code

raw sockets: ttl can be set or auto-detected

The IP TTL used when sending on raw sockets can be set using the
core.udp4_raw_ttl config variable.
By default it is auto-detected on startup (the same IP TTL as the
one for the first udp4 socket is used).

Andrei Pelinescu-Onciul authored on 10/08/2010 09:09:19
Showing 6 changed files
... ...
@@ -56,6 +56,8 @@
56 56
 #include "pt.h"
57 57
 #endif
58 58
 #include "msg_translator.h" /* fix_global_req_flags() */
59
+#include "globals.h"
60
+#include "sock_ut.h"
59 61
 #include "cfg/cfg.h"
60 62
 #include "cfg_core.h"
61 63
 
... ...
@@ -113,6 +115,7 @@ struct cfg_group_core default_core_cfg = {
113 113
 	0, /*!< udp_mtu_try_proto -> default disabled */
114 114
 	0, /**< udp4_raw (disabled by default) */
115 115
 	1500, /**< udp4_raw_mtu (1500 by default) */
116
+	-1,  /**< udp4_raw_ttl (auto detect by default) */
116 117
 	0,  /*!< force_rport */
117 118
 	L_DBG, /*!< memlog */
118 119
 	1 /*!< mem_summary -flags: 0 off, 1 shm/pkg_status, 2 shm/pkg_sums */
... ...
@@ -153,6 +156,24 @@ static int check_raw_sock_support(void* cfg_h, str* gname, str* name,
153 153
 
154 154
 
155 155
 
156
+static int  udp4_raw_ttl_fixup(void* cfg_h, str* gname, str* name, void** val)
157
+{
158
+	int v;
159
+	v = (int)(long)(*val);
160
+	if (v < 0) {
161
+		if (sendipv4)
162
+			v = sock_get_ttl(sendipv4->socket);
163
+	}
164
+	if (v < 0) {
165
+		/* some error => use a reasonable default */
166
+		v = 63;
167
+	}
168
+	*val = (void*)(long)v;
169
+	return 0;
170
+}
171
+
172
+
173
+
156 174
 cfg_def_t core_cfg_def[] = {
157 175
 	{"debug",		CFG_VAR_INT|CFG_ATOMIC,	0, 0, 0, 0,
158 176
 		"debug level"},
... ...
@@ -264,6 +285,9 @@ cfg_def_t core_cfg_def[] = {
264 264
 		"set the MTU used when using raw sockets for udp sending."
265 265
 		" This  value will be used when deciding whether or not to fragment"
266 266
 		" the packets."},
267
+	{"udp4_raw_ttl", CFG_VAR_INT | CFG_ATOMIC, -1, 255, udp4_raw_ttl_fixup, 0,
268
+		"set the IP TTL used when using raw sockets for udp sending."
269
+		" -1 will use the same value as for normal udp sockets."},
267 270
 	{"force_rport",     CFG_VAR_INT, 0, 1,  0, fix_global_req_flags,
268 271
 		"force rport for all the received messages" },
269 272
 	{"memlog",		CFG_VAR_INT|CFG_ATOMIC,	0, 0, 0, 0,
... ...
@@ -103,6 +103,7 @@ struct cfg_group_core {
103 103
 	int udp_mtu_try_proto; /*!< if packet> udp_mtu, try proto (e.g. TCP) */
104 104
 	int udp4_raw; /* use raw sockets for sending on udp ipv 4 */
105 105
 	int udp4_raw_mtu; /* mtu used when using udp raw socket */
106
+	int udp4_raw_ttl; /* ttl used when using udp raw sockets */
106 107
 	int force_rport; /*!< if set rport will always be forced*/
107 108
 	int memlog; /*!< log level for memory status/summary info */
108 109
 	int mem_summary; /*!< display memory status/summary info on exit */
... ...
@@ -188,6 +188,7 @@
188 188
 #include "basex.h" /* init */
189 189
 #include "pvapi_init.h" /* init */
190 190
 #include "pv_core.h" /* register core pvars */
191
+#include "sock_ut.h"
191 192
 
192 193
 #ifdef DEBUG_DMALLOC
193 194
 #include <dmalloc.h>
... ...
@@ -1266,6 +1267,13 @@ int main_loop()
1266 1266
 				default_core_cfg.udp4_raw = 1; /* enabled */
1267 1267
 				DBG("raw socket possible => turning it on\n");
1268 1268
 			}
1269
+			if (default_core_cfg.udp4_raw_ttl < 0) {
1270
+				/* auto-detect */
1271
+				default_core_cfg.udp4_raw_ttl = sock_get_ttl(sendipv4->socket);
1272
+				if (default_core_cfg.udp4_raw_ttl < 0)
1273
+					/* error, use some default value */
1274
+					default_core_cfg.udp4_raw_ttl = 63;
1275
+			}
1269 1276
 		}
1270 1277
 #else
1271 1278
 		default_core.cfg.udp4_raw = 0;
... ...
@@ -1417,6 +1425,14 @@ int main_loop()
1417 1417
 					default_core_cfg.udp4_raw = 1; /* enabled */
1418 1418
 					DBG("raw socket possible => turning it on\n");
1419 1419
 				}
1420
+				if (default_core_cfg.udp4_raw_ttl < 0) {
1421
+					/* auto-detect */
1422
+					default_core_cfg.udp4_raw_ttl =
1423
+						sock_get_ttl(sendipv4->socket);
1424
+					if (default_core_cfg.udp4_raw_ttl < 0)
1425
+						/* error, use some default value */
1426
+						default_core_cfg.udp4_raw_ttl = 63;
1427
+				}
1420 1428
 			}
1421 1429
 		}
1422 1430
 #else
... ...
@@ -56,6 +56,8 @@
56 56
 #include <netinet/udp.h>
57 57
 
58 58
 #include "raw_sock.h"
59
+#include "cfg/cfg.h"
60
+#include "cfg_core.h"
59 61
 
60 62
 
61 63
 /** create and return a raw socket.
... ...
@@ -79,11 +81,8 @@ int raw_socket(int proto, struct ip_addr* ip, str* iface, int iphdr_incl)
79 79
 	char* ifname;
80 80
 
81 81
 	sock = socket(PF_INET, SOCK_RAW, proto);
82
-	if (sock==-1){
83
-		ERR("raw_socket: socket() failed: %s [%d]\n",
84
-				strerror(errno), errno);
82
+	if (sock==-1)
85 83
 		goto error;
86
-	}
87 84
 	/* set socket options */
88 85
 	if (iphdr_incl) {
89 86
 		t=1;
... ...
@@ -426,7 +425,7 @@ inline static int mk_ip_hdr(struct ip* iph, struct in_addr* from,
426 426
 	iph->ip_len = htons(payload_len);
427 427
 	iph->ip_id = 0;
428 428
 	iph->ip_off = 0; /* frag.: first 3 bits=flags=0, last 13 bits=offset */
429
-	iph->ip_ttl = 63; /* FIXME: use some configured value */
429
+	iph->ip_ttl = cfg_get(core, core_cfg, udp4_raw_ttl);
430 430
 	iph->ip_p = proto;
431 431
 	iph->ip_sum = 0;
432 432
 	iph->ip_src = *from;
433 433
new file mode 100644
... ...
@@ -0,0 +1,71 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2010 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/** various socket related functions.
18
+ * @file sock_ut.c
19
+ * @ingroup: core 
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2010-08-09  initial version (andrei)
25
+*/
26
+
27
+#include "sock_ut.h"
28
+
29
+#include <sys/types.h>
30
+#include <sys/socket.h>
31
+#include <netinet/in.h>
32
+#include <netinet/in_systm.h>
33
+#include <netinet/ip.h>
34
+#include <errno.h>
35
+#include <arpa/inet.h>
36
+
37
+
38
+/** get the IP TTL.
39
+ * @return ttl on success, < 0 on error
40
+ */
41
+int sock_get_ttl(int sock)
42
+{
43
+	int ioptval;
44
+	unsigned int ioptvallen;
45
+
46
+	ioptvallen=sizeof(ioptval);
47
+	if (getsockopt( sock, IPPROTO_IP, IP_TTL, (void*) &ioptval,
48
+		    &ioptvallen) == -1 )
49
+	{
50
+		return -1;
51
+	}
52
+	return ioptval;
53
+}
54
+
55
+
56
+
57
+/** set the IP TTL on a socket.
58
+ * @return ttl on success, < 0 on error
59
+ */
60
+int sock_set_ttl(int sock, int ttl)
61
+{
62
+	int ioptval;
63
+
64
+	if (setsockopt( sock, IPPROTO_IP, IP_TTL, (void*) &ioptval,
65
+					sizeof(ioptval)) == -1 )
66
+		return -1;
67
+	return ioptval;
68
+}
69
+
70
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
0 71
new file mode 100644
... ...
@@ -0,0 +1,39 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2010 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/** various socket related functions.
18
+ * @file sock_ut.h
19
+ * @ingroup: core 
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2010-08-09  initial version (andrei)
25
+*/
26
+
27
+#ifndef __sock_ut_h
28
+#define __sock_ut_h
29
+
30
+
31
+
32
+int sock_get_ttl(int sock);
33
+int sock_set_ttl(int sock, int ttl);
34
+
35
+
36
+#endif /*__sock_ut_h*/
37
+
38
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */