Browse code

core: basic support for receiving udp sip packets on raw sockets

Functions for receiving udp sip packets from raw sockets.

Andrei Pelinescu-Onciul authored on 09/06/2010 20:55:21
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,147 @@
1
+/*
2
+ * Copyright (C) 2010 iptelorg GmbH
3
+ *
4
+ * Permission to use, copy, modify, and distribute this software for any
5
+ * purpose with or without fee is hereby granted, provided that the above
6
+ * copyright notice and this permission notice appear in all copies.
7
+ *
8
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ */
16
+/** raw socket udp listen functions.
17
+ *  @file raw_listener.c
18
+ *  @ingroup core
19
+ *  Module: @ref core
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2010-06-09  intial version (from older code) andrei
25
+ */
26
+
27
+#ifdef USE_RAW_SOCKS
28
+
29
+#include "../../config.h"
30
+#include "../../receive.h"
31
+
32
+#include "raw_listener.h"
33
+#include "raw_sock.h"
34
+#include "tlb_send.h"
35
+#include "tlb_cfg.h"
36
+
37
+#include <errno.h>
38
+#include <string.h>
39
+
40
+struct raw_listen_info* raw_sendipv4=0;
41
+
42
+/** creates a raw socket based on a socket_info structure.
43
+ * Side-effects: sets raw_udp_sendipv4 if not already set.
44
+ * @param si - pointer to partially filled socket_info structure (su must
45
+ *              be set).
46
+ * @param iface - pointer to network interface to bind on (str). Can be null.
47
+ * @param iphdr_incl - 1 if send on these socket will include the IP header.
48
+ * @return <0 on error, socket on success.
49
+ */
50
+int raw_listener_init(struct socket_info* si, str* iface, int iphdr_incl)
51
+{
52
+	int sock;
53
+	struct ip_addr ip;
54
+	
55
+	su2ip_addr(&ip, &si->su);
56
+	sock=raw_udp4_socket(&ip, iface, iphdr_incl);
57
+	if (sock>=0){
58
+		if (raw_sendipv4==0 || iface==0 || iface->s==0)
59
+			raw_udp_sendipv4=si;
60
+	}
61
+	return sock;
62
+}
63
+
64
+
65
+
66
+/** receive sip udp ipv4 packets over a raw socket in a loop.
67
+ * It should be called by a "raw socket receiver" process
68
+ * (since the function never exits unless it encounters a
69
+ *  critical error).
70
+ * @param rsock - initialized raw socket.
71
+ * @param port1 - start of port range.
72
+ * @param port2 - end of port range. If 0 it's equivalent to listening only
73
+ *                on port1.
74
+ * @return <0 on error, never returns on success.
75
+ */
76
+int raw_udp4_rcv_loop(int rsock, int port1, int port2)
77
+{
78
+	static char buf[BUF_SIZE+1];
79
+	char* p;
80
+	char* tmp;
81
+	union sockaddr_union from;
82
+	union sockaddr_union to;
83
+	struct receive_info ri;
84
+	struct raw_filter rf;
85
+	int len;
86
+	
87
+	/* this will not change */
88
+	from.sin.sin_family=AF_INET;
89
+	ri.bind_address=0;
90
+	ri.proto=PROTO_UDP;
91
+	ri.proto_reserved1=0;
92
+	ri.proto_reserved2=0;
93
+	/* set filter to match any address but with the specified port range */
94
+	memset(&rf, 0, sizeof(rf));
95
+	rf.dst.ip.af=AF_INET;
96
+	rf.dst.ip.len=4;
97
+	rf.dst.mask.af=AF_INET;
98
+	rf.dst.mask.len=4;
99
+	rf.proto=PROTO_UDP;
100
+	rf.port1=port1;
101
+	rf.port2=port2?port2:port1;
102
+	for(;;){
103
+		p=buf;
104
+		len=raw_udp4_recv(rsock, &p, BUF_SIZE, &from, &to, &rf);
105
+		if (len<0){
106
+			if (len==-1){
107
+				LOG(L_ERR, "ERROR: raw_udp4_rcv_loop:raw_udp4_recv: %s [%d]\n",
108
+						strerror(errno), errno);
109
+				if ((errno==EINTR)||(errno==EWOULDBLOCK))
110
+					continue;
111
+				else
112
+					goto error;
113
+			}else{
114
+				DBG("raw_udp4_rcv_loop: raw_udp4_recv error: %d\n", len);
115
+				continue;
116
+			}
117
+		}
118
+		/* we must 0-term the message */
119
+		p[len]=0;
120
+		ri.src_su=from;
121
+		su2ip_addr(&ri.src_ip, &from);
122
+		ri.src_port=su_getport(&from);
123
+		su2ip_addr(&ri.dst_ip, &to);
124
+		ri.dst_port=su_getport(&to);
125
+		/* sanity checks */
126
+		if (len<MIN_UDP_PACKET){
127
+			tmp=ip_addr2a(&ri.src_ip);
128
+			DBG("raw_udp4_rcv_loop: probing packet received from %s %d\n",
129
+					tmp, htons(ri.src_port));
130
+			continue;
131
+		}
132
+		if (ri.src_port==0){
133
+			tmp=ip_addr2a(&ri.src_ip);
134
+			LOG(L_INFO, "raw_udp4_rcv_loop: dropping 0 port packet from %s\n",
135
+						tmp);
136
+			continue;
137
+		}
138
+		tmp=ip_addr2a(&ri.src_ip);
139
+		DBG("raw_udp4_rcv_loop: received from %s:\n[%.*s]\n", tmp, len, p);
140
+		receive_msg(p, len, &ri);
141
+	}
142
+error:
143
+	return -1;
144
+}
145
+
146
+
147
+#endif /* USE_RAW_SOCKS */
0 148
new file mode 100644
... ...
@@ -0,0 +1,39 @@
1
+/*
2
+ * Copyright (C) 2010 iptelorg GmbH
3
+ *
4
+ * Permission to use, copy, modify, and distribute this software for any
5
+ * purpose with or without fee is hereby granted, provided that the above
6
+ * copyright notice and this permission notice appear in all copies.
7
+ *
8
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ */
16
+/** raw socket udp listen functions.
17
+ *  @file raw_listener.h
18
+ *  @ingroup core
19
+ *  Module: @ref core
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2010-06-09  initial version (from older code) andrei
25
+ */
26
+
27
+#ifndef _raw_listener_h
28
+#define _raw_listener_h
29
+
30
+#include "../../ip_addr.h"
31
+
32
+
33
+/** default raw socket used for sending on udp ipv4 */
34
+struct struct_info* raw_udp_sendipv4;
35
+
36
+int raw_listener_init(struct socket_info* si, str* iface, int iphdr_incl);
37
+int raw_udp4_rcv_loop(int rsock, int port1, int port2);
38
+
39
+#endif /* _raw_listener_h */