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.
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>
26 #define BINDING_REQUEST 0x0001
27 #define BINDING_RESPONSE 0x0101
29 #define MAPPED_ADDRESS 0x0001
32 #define REQUEST_LEN 20
34 #define ID_COOKIE_FIELD htonl(((int) 'a' << 24) + \
42 uint32_t magic_cookie
;
52 struct stun_mapped_addr
{
59 static int stun_test(const char *server_ip
, int server_port
,
62 int ret
, sock
, set
= 1;
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
;
77 sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
79 panic("Cannot obtain socket!\n");
81 ret
= setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &set
, sizeof(set
));
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
));
91 panic("Cannot bind udp socket!\n");
94 hdr
= (struct stun_header
*) pkt
;
95 hdr
->type
= htons(BINDING_REQUEST
);
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
,
109 whine("Error sending request (%s)!\n", strerror(errno
));
113 set_timeout(&timeout
, TIMEOUT
);
116 FD_SET(sock
, &fdset
);
118 ret
= select(sock
+ 1, &fdset
, NULL
, NULL
, &timeout
);
120 whine("STUN server timeout!\n");
124 memset(rpkt
, 0, sizeof(rpkt
));
125 len
= read(sock
, rpkt
, sizeof(rpkt
));
129 if (len
< REQUEST_LEN
) {
130 whine("Bad STUN response (%s)!\n", strerror(errno
));
134 rhdr
= (struct stun_header
*) rpkt
;
135 if (ntohs(rhdr
->type
) != BINDING_RESPONSE
) {
136 whine("Wrong STUN response type!\n");
140 if (rhdr
->len
== 0) {
141 whine("No attributes in STUN response!\n");
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");
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
)
161 addr
= (struct stun_mapped_addr
*) (rpkt
+ off
+ 4);
162 if (addr
->family
!= 0x1)
165 in
.s_addr
= addr
->ip
;
166 printf("Public mapping %s:%u!\n",
167 inet_ntoa(in
), ntohs(addr
->port
));
171 off
+= ntohs(attr
->len
);
177 int print_stun_probe(char *server
, int sport
, int tport
)
183 hp
= gethostbyname(server
);
186 address
= inet_ntoa(*(struct in_addr
*) hp
->h_addr_list
[0]);
187 return stun_test(address
, sport
, tport
);