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.
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>
27 #define BINDING_REQUEST 0x0001
28 #define BINDING_RESPONSE 0x0101
30 #define MAPPED_ADDRESS 0x0001
33 #define REQUEST_LEN 20
35 #define ID_COOKIE_FIELD htonl(((int) 'a' << 24) + \
43 uint32_t magic_cookie
;
53 struct stun_mapped_addr
{
60 static int stun_test(const char *server_ip
, int server_port
,
63 int ret
, sock
, set
= 1;
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
;
78 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
80 panic("Cannot obtain socket!\n");
82 ret
= setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &set
, sizeof(set
));
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
));
92 panic("Cannot bind udp socket!\n");
95 hdr
= (struct stun_header
*) pkt
;
96 hdr
->type
= htons(BINDING_REQUEST
);
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
,
110 whine("Error sending request (%s)!\n", strerror(errno
));
114 set_timeout(&timeout
, TIMEOUT
);
117 FD_SET(sock
, &fdset
);
119 ret
= select(sock
+ 1, &fdset
, NULL
, NULL
, &timeout
);
121 whine("STUN server timeout!\n");
125 memset(rpkt
, 0, sizeof(rpkt
));
126 len
= read(sock
, rpkt
, sizeof(rpkt
));
130 if (len
< REQUEST_LEN
) {
131 whine("Bad STUN response (%s)!\n", strerror(errno
));
135 rhdr
= (struct stun_header
*) rpkt
;
136 if (ntohs(rhdr
->type
) != BINDING_RESPONSE
) {
137 whine("Wrong STUN response type!\n");
141 if (rhdr
->len
== 0) {
142 whine("No attributes in STUN response!\n");
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");
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
)
162 addr
= (struct stun_mapped_addr
*) (rpkt
+ off
+ 4);
163 if (addr
->family
!= 0x1)
166 in
.s_addr
= addr
->ip
;
167 printf("Public mapping %s:%u!\n",
168 inet_ntoa(in
), ntohs(addr
->port
));
172 off
+= ntohs(attr
->len
);
178 int print_stun_probe(char *server
, int sport
, int tport
)
184 hp
= gethostbyname(server
);
187 address
= inet_ntoa(*(struct in_addr
*) hp
->h_addr_list
[0]);
188 return stun_test(address
, sport
, tport
);