mailmap: add mail alias
[transsip-mirror.git] / src / stun.c
blobb12c6ebdf2f6d59168f7e3e1780d664274b305bd
1 /*
2 * transsip - the telephony toolkit
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2011, 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <time.h>
14 #include <unistd.h>
15 #include <netdb.h>
16 #include <netinet/in.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <sys/select.h>
22 #include "xmalloc.h"
23 #include "xutils.h"
24 #include "die.h"
26 #define BINDING_REQUEST 0x0001
27 #define BINDING_RESPONSE 0x0101
29 #define MAPPED_ADDRESS 0x0001
31 #define TIMEOUT 5000
32 #define REQUEST_LEN 20
34 #define ID_COOKIE_FIELD htonl(((int) 'a' << 24) + \
35 ((int) 'c' << 16) + \
36 ((int) 'd' << 8) + \
37 (int) 'c')
39 struct stun_header {
40 uint16_t type;
41 uint16_t len;
42 uint32_t magic_cookie;
43 uint32_t transid[3];
46 struct stun_attrib {
47 uint16_t type;
48 uint16_t len;
49 uint8_t *value;
52 struct stun_mapped_addr {
53 uint8_t none;
54 uint8_t family;
55 uint16_t port;
56 uint32_t ip;
59 static int stun_test(const char *server_ip, int server_port,
60 int transsip_port)
62 int ret, sock, set = 1;
63 uint8_t pkt[256];
64 uint8_t rpkt[256];
65 size_t len, off, max;
66 struct in_addr in;
67 struct timeval timeout;
68 struct stun_header *hdr, *rhdr;
69 struct stun_attrib *attr;
70 struct stun_mapped_addr *addr;
71 struct sockaddr_in saddr, daddr;
72 fd_set fdset;
74 if (!server_ip)
75 return -EINVAL;
77 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
78 if (sock < 0)
79 panic("Cannot obtain socket!\n");
81 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
82 if (ret)
83 panic("Cannot set socket option!\n");
85 saddr.sin_family = PF_INET;
86 saddr.sin_port = htons(transsip_port);
87 saddr.sin_addr.s_addr = INADDR_ANY;
89 ret = bind(sock, (struct sockaddr *) &saddr, sizeof(saddr));
90 if (ret)
91 panic("Cannot bind udp socket!\n");
93 len = REQUEST_LEN;
94 hdr = (struct stun_header *) pkt;
95 hdr->type = htons(BINDING_REQUEST);
96 hdr->len = 0;
97 hdr->magic_cookie = ID_COOKIE_FIELD;
98 hdr->transid[0] = htonl(rand());
99 hdr->transid[1] = htonl(rand());
100 hdr->transid[2] = htonl(rand());
102 daddr.sin_family = PF_INET;
103 daddr.sin_port = htons(server_port);
104 daddr.sin_addr.s_addr = inet_addr(server_ip);
106 ret = sendto(sock, pkt, len, 0, (struct sockaddr *) &daddr,
107 sizeof(daddr));
108 if (ret != len) {
109 whine("Error sending request (%s)!\n", strerror(errno));
110 return -EIO;
113 set_timeout(&timeout, TIMEOUT);
115 FD_ZERO(&fdset);
116 FD_SET(sock, &fdset);
118 ret = select(sock + 1, &fdset, NULL, NULL, &timeout);
119 if (ret <= 0) {
120 whine("STUN server timeout!\n");
121 return -EIO;
124 memset(rpkt, 0, sizeof(rpkt));
125 len = read(sock, rpkt, sizeof(rpkt));
127 close(sock);
129 if (len < REQUEST_LEN) {
130 whine("Bad STUN response (%s)!\n", strerror(errno));
131 return -EIO;
134 rhdr = (struct stun_header *) rpkt;
135 if (ntohs(rhdr->type) != BINDING_RESPONSE) {
136 whine("Wrong STUN response type!\n");
137 return -EIO;
140 if (rhdr->len == 0) {
141 whine("No attributes in STUN response!\n");
142 return -EIO;
145 if (rhdr->magic_cookie != hdr->magic_cookie ||
146 rhdr->transid[0] != hdr->transid[0] ||
147 rhdr->transid[1] != hdr->transid[1] ||
148 rhdr->transid[2] != hdr->transid[2]) {
149 whine("Got wrong STUN transaction id!\n");
150 return -EIO;
153 off = REQUEST_LEN;
154 max = ntohs(rhdr->len) + REQUEST_LEN;
156 while (off + 8 < max) {
157 attr = (struct stun_attrib *) (rpkt + off);
158 if (ntohs(attr->type) != MAPPED_ADDRESS)
159 goto next;
161 addr = (struct stun_mapped_addr *) (rpkt + off + 4);
162 if (addr->family != 0x1)
163 break;
165 in.s_addr = addr->ip;
166 printf("Public mapping %s:%u!\n",
167 inet_ntoa(in), ntohs(addr->port));
168 break;
169 next:
170 off += 4;
171 off += ntohs(attr->len);
174 return 0;
177 int print_stun_probe(char *server, int sport, int tport)
179 char *address;
180 struct hostent *hp;
182 srand(time(NULL));
183 hp = gethostbyname(server);
184 if (!hp)
185 return -EIO;
186 address = inet_ntoa(*(struct in_addr *) hp->h_addr_list[0]);
187 return stun_test(address, sport, tport);