ifpps: Remove unused 'forks' member from struct ifstat
[netsniff-ng.git] / stun.c
blob4ea4eac28eb37611673bba2358e3133425d92933
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <unistd.h>
8 #include <netdb.h>
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <sys/select.h>
15 #include "xmalloc.h"
16 #include "die.h"
17 #include "sock.h"
18 #include "stun.h"
20 #define BINDING_REQUEST 0x0001
21 #define BINDING_RESPONSE 0x0101
23 #define MAPPED_ADDRESS 0x0001
25 #define TIMEOUT 5000
26 #define REQUEST_LEN 20
28 #define ID_COOKIE_FIELD htonl(((int) 'a' << 24) + \
29 ((int) 'c' << 16) + \
30 ((int) 'd' << 8) + \
31 (int) 'c')
33 struct stun_header {
34 uint16_t type;
35 uint16_t len;
36 uint32_t magic_cookie;
37 uint32_t transid[3];
40 struct stun_attrib {
41 uint16_t type;
42 uint16_t len;
43 uint8_t *value;
46 struct stun_mapped_addr {
47 uint8_t none;
48 uint8_t family;
49 uint16_t port;
50 uint32_t ip;
53 static int stun_test(const char *server_ip, int server_port,
54 int tun_port)
56 int ret, sock;
57 uint8_t pkt[256];
58 uint8_t rpkt[256];
59 size_t len, off, max;
60 struct in_addr in;
61 struct timeval timeout;
62 struct stun_header *hdr, *rhdr;
63 struct stun_attrib *attr;
64 struct stun_mapped_addr *addr;
65 struct sockaddr_in saddr, daddr;
66 fd_set fdset;
68 if (!server_ip)
69 return -EINVAL;
71 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
72 if (sock < 0)
73 panic("Cannot obtain socket!\n");
75 set_reuseaddr(sock);
77 saddr.sin_family = PF_INET;
78 saddr.sin_port = htons(tun_port);
79 saddr.sin_addr.s_addr = INADDR_ANY;
81 ret = bind(sock, (struct sockaddr *) &saddr, sizeof(saddr));
82 if (ret)
83 panic("Cannot bind udp socket!\n");
85 len = REQUEST_LEN;
86 hdr = (struct stun_header *) pkt;
87 hdr->type = htons(BINDING_REQUEST);
88 hdr->len = 0;
89 hdr->magic_cookie = ID_COOKIE_FIELD;
90 hdr->transid[0] = htonl(rand());
91 hdr->transid[1] = htonl(rand());
92 hdr->transid[2] = htonl(rand());
94 daddr.sin_family = PF_INET;
95 daddr.sin_port = htons(server_port);
96 daddr.sin_addr.s_addr = inet_addr(server_ip);
98 ret = sendto(sock, pkt, len, 0, (struct sockaddr *) &daddr,
99 sizeof(daddr));
100 if (ret != len) {
101 printf("Error sending request (%s)!\n", strerror(errno));
102 return -EIO;
105 timeout.tv_sec = TIMEOUT / 1000;
106 timeout.tv_usec = (TIMEOUT % 1000) * 1000;
108 FD_ZERO(&fdset);
109 FD_SET(sock, &fdset);
111 ret = select(sock + 1, &fdset, NULL, NULL, &timeout);
112 if (ret <= 0) {
113 printf("STUN server timeout!\n");
114 return -EIO;
117 memset(rpkt, 0, sizeof(rpkt));
118 len = read(sock, rpkt, sizeof(rpkt));
120 close(sock);
122 if (len < REQUEST_LEN) {
123 printf("Bad STUN response (%s)!\n", strerror(errno));
124 return -EIO;
127 rhdr = (struct stun_header *) rpkt;
128 if (ntohs(rhdr->type) != BINDING_RESPONSE) {
129 printf("Wrong STUN response type!\n");
130 return -EIO;
133 if (rhdr->len == 0) {
134 printf("No attributes in STUN response!\n");
135 return -EIO;
138 if (rhdr->magic_cookie != hdr->magic_cookie ||
139 rhdr->transid[0] != hdr->transid[0] ||
140 rhdr->transid[1] != hdr->transid[1] ||
141 rhdr->transid[2] != hdr->transid[2]) {
142 printf("Got wrong STUN transaction id!\n");
143 return -EIO;
146 off = REQUEST_LEN;
147 max = ntohs(rhdr->len) + REQUEST_LEN;
149 while (off + 8 < max) {
150 attr = (struct stun_attrib *) (rpkt + off);
151 if (ntohs(attr->type) != MAPPED_ADDRESS)
152 goto next;
154 addr = (struct stun_mapped_addr *) (rpkt + off + 4);
155 if (addr->family != 0x1)
156 break;
158 in.s_addr = addr->ip;
159 printf("Public mapping %s:%u!\n",
160 inet_ntoa(in), ntohs(addr->port));
161 break;
162 next:
163 off += 4;
164 off += ntohs(attr->len);
167 return 0;
170 int print_stun_probe(char *server, int sport, int tport)
172 char *address;
173 struct hostent *hp;
175 printf("STUN on %s:%u\n", server, sport);
177 srand(time(NULL));
178 hp = gethostbyname(server);
179 if (!hp)
180 return -EIO;
181 address = inet_ntoa(*(struct in_addr *) hp->h_addr_list[0]);
182 return stun_test(address, sport, tport);