sock: add socket management functions
[netsniff-ng.git] / stun.c
blob4b8dd6297b9316e9a033ade10b0f835eefe19725
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <netdb.h>
15 #include <netinet/in.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <sys/select.h>
21 #include "xmalloc.h"
22 #include "xutils.h"
23 #include "die.h"
24 #include "sock.h"
26 extern int print_stun_probe(char *server, int sport, int tport);
28 #define BINDING_REQUEST 0x0001
29 #define BINDING_RESPONSE 0x0101
31 #define MAPPED_ADDRESS 0x0001
33 #define TIMEOUT 5000
34 #define REQUEST_LEN 20
36 #define ID_COOKIE_FIELD htonl(((int) 'a' << 24) + \
37 ((int) 'c' << 16) + \
38 ((int) 'd' << 8) + \
39 (int) 'c')
41 struct stun_header {
42 uint16_t type;
43 uint16_t len;
44 uint32_t magic_cookie;
45 uint32_t transid[3];
48 struct stun_attrib {
49 uint16_t type;
50 uint16_t len;
51 uint8_t *value;
54 struct stun_mapped_addr {
55 uint8_t none;
56 uint8_t family;
57 uint16_t port;
58 uint32_t ip;
61 static int stun_test(const char *server_ip, int server_port,
62 int tun_port)
64 int ret, sock;
65 uint8_t pkt[256];
66 uint8_t rpkt[256];
67 size_t len, off, max;
68 struct in_addr in;
69 struct timeval timeout;
70 struct stun_header *hdr, *rhdr;
71 struct stun_attrib *attr;
72 struct stun_mapped_addr *addr;
73 struct sockaddr_in saddr, daddr;
74 fd_set fdset;
76 if (!server_ip)
77 return -EINVAL;
79 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
80 if (sock < 0)
81 panic("Cannot obtain socket!\n");
83 set_reuseaddr(sock);
85 saddr.sin_family = PF_INET;
86 saddr.sin_port = htons(tun_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 printf("Error sending request (%s)!\n", strerror(errno));
110 return -EIO;
113 timeout.tv_sec = TIMEOUT / 1000;
114 timeout.tv_usec = (TIMEOUT % 1000) * 1000;
116 FD_ZERO(&fdset);
117 FD_SET(sock, &fdset);
119 ret = select(sock + 1, &fdset, NULL, NULL, &timeout);
120 if (ret <= 0) {
121 printf("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 printf("Bad STUN response (%s)!\n", strerror(errno));
132 return -EIO;
135 rhdr = (struct stun_header *) rpkt;
136 if (ntohs(rhdr->type) != BINDING_RESPONSE) {
137 printf("Wrong STUN response type!\n");
138 return -EIO;
141 if (rhdr->len == 0) {
142 printf("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 printf("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 printf("STUN on %s:%u\n", server, sport);
185 srand(time(NULL));
186 hp = gethostbyname(server);
187 if (!hp)
188 return -EIO;
189 address = inet_ntoa(*(struct in_addr *) hp->h_addr_list[0]);
190 return stun_test(address, sport, tport);