engine: fix in dial + hangup
[transsip-mirror.git] / src / stun.c
blob4c90363b4cf7c57b2419eb973d916aaa38b90f6e
1 /*
2 * transsip - the telephony network
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2011, 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include <netdb.h>
17 #include <netinet/in.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <sys/select.h>
23 #include "xmalloc.h"
24 #include "xutils.h"
25 #include "die.h"
27 #define BINDING_REQUEST 0x0001
28 #define BINDING_RESPONSE 0x0101
30 #define MAPPED_ADDRESS 0x0001
32 #define TIMEOUT 5000
33 #define REQUEST_LEN 20
35 #define ID_COOKIE_FIELD htonl(((int) 'a' << 24) + \
36 ((int) 'c' << 16) + \
37 ((int) 'd' << 8) + \
38 (int) 'c')
40 struct stun_header {
41 uint16_t type;
42 uint16_t len;
43 uint32_t magic_cookie;
44 uint32_t transid[3];
47 struct stun_attrib {
48 uint16_t type;
49 uint16_t len;
50 uint8_t *value;
53 struct stun_mapped_addr {
54 uint8_t none;
55 uint8_t family;
56 uint16_t port;
57 uint32_t ip;
60 static int stun_test(const char *server_ip, int server_port,
61 int transsip_port)
63 int ret, sock, set = 1;
64 uint8_t pkt[256];
65 uint8_t rpkt[256];
66 size_t len, off, max;
67 struct in_addr in;
68 struct timeval timeout;
69 struct stun_header *hdr, *rhdr;
70 struct stun_attrib *attr;
71 struct stun_mapped_addr *addr;
72 struct sockaddr_in saddr, daddr;
73 fd_set fdset;
75 if (!server_ip)
76 return -EINVAL;
78 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
79 if (sock < 0)
80 panic("Cannot obtain socket!\n");
82 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
83 if (ret)
84 panic("Cannot set socket option!\n");
86 saddr.sin_family = PF_INET;
87 saddr.sin_port = htons(transsip_port);
88 saddr.sin_addr.s_addr = INADDR_ANY;
90 ret = bind(sock, (struct sockaddr *) &saddr, sizeof(saddr));
91 if (ret)
92 panic("Cannot bind udp socket!\n");
94 len = REQUEST_LEN;
95 hdr = (struct stun_header *) pkt;
96 hdr->type = htons(BINDING_REQUEST);
97 hdr->len = 0;
98 hdr->magic_cookie = ID_COOKIE_FIELD;
99 hdr->transid[0] = htonl(rand());
100 hdr->transid[1] = htonl(rand());
101 hdr->transid[2] = htonl(rand());
103 daddr.sin_family = PF_INET;
104 daddr.sin_port = htons(server_port);
105 daddr.sin_addr.s_addr = inet_addr(server_ip);
107 ret = sendto(sock, pkt, len, 0, (struct sockaddr *) &daddr,
108 sizeof(daddr));
109 if (ret != len) {
110 whine("Error sending request (%s)!\n", strerror(errno));
111 return -EIO;
114 set_timeout(&timeout, TIMEOUT);
116 FD_ZERO(&fdset);
117 FD_SET(sock, &fdset);
119 ret = select(sock + 1, &fdset, NULL, NULL, &timeout);
120 if (ret <= 0) {
121 whine("STUN server timeout!\n");
122 return -EIO;
125 memset(rpkt, 0, sizeof(rpkt));
126 len = read(sock, rpkt, sizeof(rpkt));
128 close(sock);
130 if (len < REQUEST_LEN) {
131 whine("Bad STUN response (%s)!\n", strerror(errno));
132 return -EIO;
135 rhdr = (struct stun_header *) rpkt;
136 if (ntohs(rhdr->type) != BINDING_RESPONSE) {
137 whine("Wrong STUN response type!\n");
138 return -EIO;
141 if (rhdr->len == 0) {
142 whine("No attributes in STUN response!\n");
143 return -EIO;
146 if (rhdr->magic_cookie != hdr->magic_cookie ||
147 rhdr->transid[0] != hdr->transid[0] ||
148 rhdr->transid[1] != hdr->transid[1] ||
149 rhdr->transid[2] != hdr->transid[2]) {
150 whine("Got wrong STUN transaction id!\n");
151 return -EIO;
154 off = REQUEST_LEN;
155 max = ntohs(rhdr->len) + REQUEST_LEN;
157 while (off + 8 < max) {
158 attr = (struct stun_attrib *) (rpkt + off);
159 if (ntohs(attr->type) != MAPPED_ADDRESS)
160 goto next;
162 addr = (struct stun_mapped_addr *) (rpkt + off + 4);
163 if (addr->family != 0x1)
164 break;
166 in.s_addr = addr->ip;
167 printf("Public mapping %s:%u!\n",
168 inet_ntoa(in), ntohs(addr->port));
169 break;
170 next:
171 off += 4;
172 off += ntohs(attr->len);
175 return 0;
178 int print_stun_probe(char *server, int sport, int tport)
180 char *address;
181 struct hostent *hp;
183 srand(time(NULL));
184 hp = gethostbyname(server);
185 if (!hp)
186 return -EIO;
187 address = inet_ntoa(*(struct in_addr *) hp->h_addr_list[0]);
188 return stun_test(address, sport, tport);