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