Add stdint.h include into STUN module implementation.
[novacoin.git] / src / stun.cpp
1 /*
2  * Get External IP address by STUN protocol
3  *
4  * Based on project Minimalistic STUN client "ministun"
5  * https://code.google.com/p/ministun/
6  *
7  * This program is free software, distributed under the terms of
8  * the GNU General Public License Version 2. See the LICENSE file
9  * at the top of the source tree.
10  *
11  * STUN is described in RFC3489 and it is based on the exchange
12  * of UDP packets between a client and one or more servers to
13  * determine the externally visible address (and port) of the client
14  * once it has gone through the NAT boxes that connect it to the
15  * outside.
16  * The simplest request packet is just the header defined in
17  * struct stun_header, and from the response we may just look at
18  * one attribute, STUN_MAPPED_ADDRESS, that we find in the response.
19  * By doing more transactions with different server addresses we
20  * may determine more about the behaviour of the NAT boxes, of
21  * course - the details are in the RFC.
22  *
23  * All STUN packets start with a simple header made of a type,
24  * length (excluding the header) and a 16-byte random transaction id.
25  * Following the header we may have zero or more attributes, each
26  * structured as a type, length and a value (whose format depends
27  * on the type, but often contains addresses).
28  * Of course all fields are in network format.
29  */
30
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #ifdef WIN32
37 #include <winsock2.h>
38 #else
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <netdb.h>
43 #endif
44 #include <unistd.h>
45 #include <time.h>
46 #include <errno.h>
47
48
49 #include "ministun.h"
50
51 /*---------------------------------------------------------------------*/
52
53 struct StunSrv {
54     char     name[30];
55     uint16_t port;
56 };
57
58 /*---------------------------------------------------------------------*/
59 static const int StunSrvListQty = 263; // Must be PRIME!!!!!
60
61 static struct StunSrv StunSrvList[263] = {
62     {"23.21.150.121", 3478},
63     {"iphone-stun.strato-iphone.de", 3478},
64     {"numb.viagenie.ca", 3478},
65     {"s1.taraba.net", 3478},
66     {"s2.taraba.net", 3478},
67     {"stun.12connect.com", 3478},
68     {"stun.12voip.com", 3478},
69     {"stun.1und1.de", 3478},
70     {"stun.2talk.co.nz", 3478},
71     {"stun.2talk.com", 3478},
72     {"stun.3clogic.com", 3478},
73     {"stun.3cx.com", 3478},
74     {"stun.a-mm.tv", 3478},
75     {"stun.aa.net.uk", 3478},
76     {"stun.acrobits.cz", 3478},
77     {"stun.actionvoip.com", 3478},
78     {"stun.advfn.com", 3478},
79     {"stun.aeta-audio.com", 3478},
80     {"stun.aeta.com", 3478},
81     {"stun.alltel.com.au", 3478},
82     {"stun.altar.com.pl", 3478},
83     {"stun.annatel.net", 3478},
84     {"stun.arbuz.ru", 3478},
85     {"stun.avigora.com", 3478},
86     {"stun.avigora.fr", 3478},
87     {"stun.awa-shima.com", 3478},
88     {"stun.awt.be", 3478},
89     {"stun.b2b2c.ca", 3478},
90     {"stun.bahnhof.net", 3478},
91     {"stun.barracuda.com", 3478},
92     {"stun.bluesip.net", 3478},
93     {"stun.bmwgs.cz", 3478},
94     {"stun.botonakis.com", 3478},
95     {"stun.budgetphone.nl", 3478},
96     {"stun.budgetsip.com", 3478},
97     {"stun.cablenet-as.net", 3478},
98     {"stun.callromania.ro", 3478},
99     {"stun.callwithus.com", 3478},
100     {"stun.cbsys.net", 3478},
101     {"stun.chathelp.ru", 3478},
102     {"stun.cheapvoip.com", 3478},
103     {"stun.ciktel.com", 3478},
104     {"stun.cloopen.com", 3478},
105     {"stun.colouredlines.com.au", 3478},
106     {"stun.comfi.com", 3478},
107     {"stun.commpeak.com", 3478},
108     {"stun.comtube.com", 3478},
109     {"stun.comtube.ru", 3478},
110     {"stun.cope.es", 3478},
111     {"stun.counterpath.com", 3478},
112     {"stun.counterpath.net", 3478},
113     {"stun.cryptonit.net", 3478},
114     {"stun.darioflaccovio.it", 3478},
115     {"stun.datamanagement.it", 3478},
116     {"stun.dcalling.de", 3478},
117     {"stun.decanet.fr", 3478},
118     {"stun.demos.ru", 3478},
119     {"stun.develz.org", 3478},
120     {"stun.dingaling.ca", 3478},
121     {"stun.doublerobotics.com", 3478},
122     {"stun.drogon.net", 3478},
123     {"stun.duocom.es", 3478},
124     {"stun.dus.net", 3478},
125     {"stun.e-fon.ch", 3478},
126     {"stun.easybell.de", 3478},
127     {"stun.easycall.pl", 3478},
128     {"stun.easyvoip.com", 3478},
129     {"stun.efficace-factory.com", 3478},
130     {"stun.einsundeins.com", 3478},
131     {"stun.einsundeins.de", 3478},
132     {"stun.ekiga.net", 3478},
133     {"stun.epygi.com", 3478},
134     {"stun.etoilediese.fr", 3478},
135     {"stun.eyeball.com", 3478},
136     {"stun.faktortel.com.au", 3478},
137     {"stun.freecall.com", 3478},
138     {"stun.freeswitch.org", 3478},
139     {"stun.freevoipdeal.com", 3478},
140     {"stun.fuzemeeting.com", 3478},
141     {"stun.gmx.de", 3478},
142     {"stun.gmx.net", 3478},
143     {"stun.gradwell.com", 3478},
144     {"stun.halonet.pl", 3478},
145     {"stun.hoiio.com", 3478},
146     {"stun.hosteurope.de", 3478},
147     {"stun.ideasip.com", 3478},
148     {"stun.imesh.com", 3478},
149     {"stun.infra.net", 3478},
150     {"stun.internetcalls.com", 3478},
151     {"stun.intervoip.com", 3478},
152     {"stun.ipcomms.net", 3478},
153     {"stun.ipfire.org", 3478},
154     {"stun.ippi.fr", 3478},
155     {"stun.ipshka.com", 3478},
156     {"stun.iptel.org", 3478},
157     {"stun.irian.at", 3478},
158     {"stun.it1.hr", 3478},
159     {"stun.ivao.aero", 3478},
160     {"stun.jappix.com", 3478},
161     {"stun.jumblo.com", 3478},
162     {"stun.justvoip.com", 3478},
163     {"stun.kanet.ru", 3478},
164     {"stun.kiwilink.co.nz", 3478},
165     {"stun.kundenserver.de", 3478},
166     {"stun.l.google.com", 19302},
167     {"stun.linea7.net", 3478},
168     {"stun.linphone.org", 3478},
169     {"stun.liveo.fr", 3478},
170     {"stun.lowratevoip.com", 3478},
171     {"stun.lugosoft.com", 3478},
172     {"stun.lundimatin.fr", 3478},
173     {"stun.magnet.ie", 3478},
174     {"stun.manle.com", 3478},
175     {"stun.mgn.ru", 3478},
176     {"stun.mit.de", 3478},
177     {"stun.mitake.com.tw", 3478},
178     {"stun.miwifi.com", 3478},
179     {"stun.modulus.gr", 3478},
180     {"stun.mozcom.com", 3478},
181     {"stun.myvoiptraffic.com", 3478},
182     {"stun.mywatson.it", 3478},
183     {"stun.nas.net", 3478},
184     {"stun.neotel.co.za", 3478},
185     {"stun.netappel.com", 3478},
186     {"stun.netappel.fr", 3478},
187     {"stun.netgsm.com.tr", 3478},
188     {"stun.nfon.net", 3478},
189     {"stun.noblogs.org", 3478},
190     {"stun.noc.ams-ix.net", 3478},
191     {"stun.node4.co.uk", 3478},
192     {"stun.nonoh.net", 3478},
193     {"stun.nottingham.ac.uk", 3478},
194     {"stun.nova.is", 3478},
195     {"stun.nventure.com", 3478},
196     {"stun.on.net.mk", 3478},
197     {"stun.ooma.com", 3478},
198     {"stun.ooonet.ru", 3478},
199     {"stun.outland-net.de", 3478},
200     {"stun.ozekiphone.com", 3478},
201     {"stun.patlive.com", 3478},
202     {"stun.personal-voip.de", 3478},
203     {"stun.petcube.com", 3478},
204     {"stun.phone.com", 3478},
205     {"stun.phoneserve.com", 3478},
206     {"stun.pjsip.org", 3478},
207     {"stun.poivy.com", 3478},
208     {"stun.powerpbx.org", 3478},
209     {"stun.powervoip.com", 3478},
210     {"stun.ppdi.com", 3478},
211     {"stun.prizee.com", 3478},
212     {"stun.qq.com", 3478},
213     {"stun.qvod.com", 3478},
214     {"stun.rackco.com", 3478},
215     {"stun.rapidnet.de", 3478},
216     {"stun.rb-net.com", 3478},
217     {"stun.remote-learner.net", 3478},
218     {"stun.rixtelecom.se", 3478},
219     {"stun.rockenstein.de", 3478},
220     {"stun.rolmail.net", 3478},
221     {"stun.rounds.com", 3478},
222     {"stun.rynga.com", 3478},
223     {"stun.samsungsmartcam.com", 3478},
224     {"stun.schlund.de", 3478},
225     {"stun.services.mozilla.com", 3478},
226     {"stun.sigmavoip.com", 3478},
227     {"stun.sip.us", 3478},
228     {"stun.sipdiscount.com", 3478},
229     {"stun.sipgate.net", 10000},
230     {"stun.sipgate.net", 3478},
231     {"stun.siplogin.de", 3478},
232     {"stun.sipnet.net", 3478},
233     {"stun.sipnet.ru", 3478},
234     {"stun.siportal.it", 3478},
235     {"stun.sippeer.dk", 3478},
236     {"stun.siptraffic.com", 3478},
237     {"stun.skylink.ru", 3478},
238     {"stun.sma.de", 3478},
239     {"stun.smartvoip.com", 3478},
240     {"stun.smsdiscount.com", 3478},
241     {"stun.snafu.de", 3478},
242     {"stun.softjoys.com", 3478},
243     {"stun.solcon.nl", 3478},
244     {"stun.solnet.ch", 3478},
245     {"stun.sonetel.com", 3478},
246     {"stun.sonetel.net", 3478},
247     {"stun.sovtest.ru", 3478},
248     {"stun.speedy.com.ar", 3478},
249     {"stun.spokn.com", 3478},
250     {"stun.srce.hr", 3478},
251     {"stun.ssl7.net", 3478},
252     {"stun.stunprotocol.org", 3478},
253     {"stun.symform.com", 3478},
254     {"stun.symplicity.com", 3478},
255     {"stun.sysadminman.net", 3478},
256     {"stun.t-online.de", 3478},
257     {"stun.tagan.ru", 3478},
258     {"stun.tatneft.ru", 3478},
259     {"stun.teachercreated.com", 3478},
260     {"stun.tel.lu", 3478},
261     {"stun.telbo.com", 3478},
262     {"stun.telefacil.com", 3478},
263     {"stun.tis-dialog.ru", 3478},
264     {"stun.tng.de", 3478},
265     {"stun.twt.it", 3478},
266     {"stun.u-blox.com", 3478},
267     {"stun.ucallweconn.net", 3478},
268     {"stun.ucsb.edu", 3478},
269     {"stun.ucw.cz", 3478},
270     {"stun.uls.co.za", 3478},
271     {"stun.unseen.is", 3478},
272     {"stun.usfamily.net", 3478},
273     {"stun.veoh.com", 3478},
274     {"stun.vidyo.com", 3478},
275     {"stun.vipgroup.net", 3478},
276     {"stun.virtual-call.com", 3478},
277     {"stun.viva.gr", 3478},
278     {"stun.vivox.com", 3478},
279     {"stun.vline.com", 3478},
280     {"stun.vo.lu", 3478},
281     {"stun.vodafone.ro", 3478},
282     {"stun.voicetrading.com", 3478},
283     {"stun.voip.aebc.com", 3478},
284     {"stun.voip.blackberry.com", 3478},
285     {"stun.voip.eutelia.it", 3478},
286     {"stun.voiparound.com", 3478},
287     {"stun.voipblast.com", 3478},
288     {"stun.voipbuster.com", 3478},
289     {"stun.voipbusterpro.com", 3478},
290     {"stun.voipcheap.co.uk", 3478},
291     {"stun.voipcheap.com", 3478},
292     {"stun.voipfibre.com", 3478},
293     {"stun.voipgain.com", 3478},
294     {"stun.voipgate.com", 3478},
295     {"stun.voipinfocenter.com", 3478},
296     {"stun.voipplanet.nl", 3478},
297     {"stun.voippro.com", 3478},
298     {"stun.voipraider.com", 3478},
299     {"stun.voipstunt.com", 3478},
300     {"stun.voipwise.com", 3478},
301     {"stun.voipzoom.com", 3478},
302     {"stun.voxgratia.org", 3478},
303     {"stun.voxox.com", 3478},
304     {"stun.voys.nl", 3478},
305     {"stun.voztele.com", 3478},
306     {"stun.vyke.com", 3478},
307     {"stun.webcalldirect.com", 3478},
308     {"stun.whoi.edu", 3478},
309     {"stun.wifirst.net", 3478},
310     {"stun.wwdl.net", 3478},
311     {"stun.xs4all.nl", 3478},
312     {"stun.xtratelecom.es", 3478},
313     {"stun.yesss.at", 3478},
314     {"stun.zadarma.com", 3478},
315     {"stun.zadv.com", 3478},
316     {"stun.zoiper.com", 3478},
317     {"stun1.faktortel.com.au", 3478},
318     {"stun1.l.google.com", 19302},
319     {"stun1.voiceeclipse.net", 3478},
320     {"stun2.l.google.com", 19302},
321     {"stun3.l.google.com", 19302},
322     {"stun4.l.google.com", 19302},
323     {"stunserver.org", 3478},
324     {"stun.antisip.com",    3478}
325 };
326
327
328 /* wrapper to send an STUN message */
329 static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
330 {
331     return sendto(s, (const char *)resp, ntohs(resp->msglen) + sizeof(*resp), 0,
332                   (struct sockaddr *)dst, sizeof(*dst));
333 }
334
335 /* helper function to generate a random request id */
336 static uint64_t randfiller;
337 static void stun_req_id(struct stun_header *req)
338 {
339     const uint64_t *S_block = (const uint64_t *)StunSrvList;
340     req->id.id[0] |= 0x55555555;
341     req->id.id[1] &= 0x55555555;
342     req->id.id[2] |= 0x55555555;
343     req->id.id[3] &= 0x55555555;
344     register char x = 20;
345     do {
346         uint32_t s_elm = S_block[(uint8_t)randfiller];
347         randfiller ^= (randfiller << 5) | (randfiller >> (64 - 5));
348         randfiller += s_elm ^ x;
349         req->id.id[x & 3] ^= randfiller + (randfiller >> 13);
350     } while(--x);
351 }
352
353 /* callback type to be invoked on stun responses. */
354 typedef int (stun_cb_f)(struct stun_attr *attr, void *arg);
355
356 /* handle an incoming STUN message.
357  *
358  * Do some basic sanity checks on packet size and content,
359  * try to extract a bit of information, and possibly reply.
360  * At the moment this only processes BIND requests, and returns
361  * the externally visible address of the request.
362  * If a callback is specified, invoke it with the attribute.
363  */
364 static int stun_handle_packet(int s, struct sockaddr_in *src,
365                               unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
366 {
367     struct stun_header *hdr = (struct stun_header *)data;
368     struct stun_attr *attr;
369     int ret = len;
370     unsigned int x;
371
372     /* On entry, 'len' is the length of the udp payload. After the
373    * initial checks it becomes the size of unprocessed options,
374    * while 'data' is advanced accordingly.
375    */
376     if (len < sizeof(struct stun_header))
377         return -20;
378
379     len -= sizeof(struct stun_header);
380     data += sizeof(struct stun_header);
381     x = ntohs(hdr->msglen); /* len as advertised in the message */
382     if(x < len)
383         len = x;
384
385     while (len) {
386         if (len < sizeof(struct stun_attr)) {
387             ret = -21;
388             break;
389         }
390         attr = (struct stun_attr *)data;
391         /* compute total attribute length */
392         x = ntohs(attr->len) + sizeof(struct stun_attr);
393         if (x > len) {
394             ret = -22;
395             break;
396         }
397         stun_cb(attr, arg);
398         //if (stun_process_attr(&st, attr)) {
399         //  ret = -23;
400         //  break;
401         // }
402         /* Clear attribute id: in case previous entry was a string,
403      * this will act as the terminator for the string.
404      */
405         attr->attr = 0;
406         data += x;
407         len -= x;
408     } // while
409     /* Null terminate any string.
410    * XXX NOTE, we write past the size of the buffer passed by the
411    * caller, so this is potentially dangerous. The only thing that
412    * saves us is that usually we read the incoming message in a
413    * much larger buffer
414    */
415     *data = '\0';
416
417     /* Now prepare to generate a reply, which at the moment is done
418    * only for properly formed (len == 0) STUN_BINDREQ messages.
419    */
420
421     return ret;
422 }
423
424 /* Extract the STUN_MAPPED_ADDRESS from the stun response.
425  * This is used as a callback for stun_handle_response
426  * when called from stun_request.
427  */
428 static int stun_get_mapped(struct stun_attr *attr, void *arg)
429 {
430     struct stun_addr *addr = (struct stun_addr *)(attr + 1);
431     struct sockaddr_in *sa = (struct sockaddr_in *)arg;
432
433     if (ntohs(attr->attr) != STUN_MAPPED_ADDRESS || ntohs(attr->len) != 8)
434         return 1; /* not us. */
435     sa->sin_port = addr->port;
436     sa->sin_addr.s_addr = addr->addr;
437     return 0;
438 }
439
440 /*---------------------------------------------------------------------*/
441
442 static int StunRequest2(int sock, struct sockaddr_in *server, struct sockaddr_in *mapped) {
443
444     struct stun_header *req;
445     unsigned char reqdata[1024];
446
447     req = (struct stun_header *)reqdata;
448     stun_req_id(req);
449     int reqlen = 0;
450     req->msgtype = 0;
451     req->msglen = 0;
452     req->msglen = htons(reqlen);
453     req->msgtype = htons(STUN_BINDREQ);
454
455     unsigned char reply_buf[1024];
456     fd_set rfds;
457     struct timeval to = { STUN_TIMEOUT, 0 };
458     struct sockaddr_in src;
459 #ifdef WIN32
460     int srclen;
461 #else
462     socklen_t srclen;
463 #endif
464
465     int res = stun_send(sock, server, req);
466     if(res < 0)
467         return -10;
468     FD_ZERO(&rfds);
469     FD_SET(sock, &rfds);
470     res = select(sock + 1, &rfds, NULL, NULL, &to);
471     if (res <= 0)  /* timeout or error */
472         return -11;
473     memset(&src, 0, sizeof(src));
474     srclen = sizeof(src);
475     /* XXX pass -1 in the size, because stun_handle_packet might
476    * write past the end of the buffer.
477    */
478     res = recvfrom(sock, (char *)reply_buf, sizeof(reply_buf) - 1,
479                    0, (struct sockaddr *)&src, &srclen);
480     if (res <= 0)
481         return -12;
482     memset(mapped, 0, sizeof(struct sockaddr_in));
483     return stun_handle_packet(sock, &src, reply_buf, res, stun_get_mapped, mapped);
484 } // StunRequest2
485
486 /*---------------------------------------------------------------------*/
487 static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapped) {
488     struct hostent *hostinfo = gethostbyname(host);
489     if(hostinfo == NULL)
490         return -1;
491
492     struct sockaddr_in server, client;
493     memset(&server, 0, sizeof(server));
494     memset(&client, 0, sizeof(client));
495     server.sin_family = client.sin_family = AF_INET;
496
497     server.sin_addr = *(struct in_addr*) hostinfo->h_addr;
498     server.sin_port = htons(port);
499
500     int sock = socket(AF_INET, SOCK_DGRAM, 0);
501     if(sock < 0)
502         return -2;
503
504     client.sin_addr.s_addr = htonl(INADDR_ANY);
505
506     int rc = -3;
507     if(bind(sock, (struct sockaddr*)&client, sizeof(client)) >= 0)
508         rc = StunRequest2(sock, &server, mapped);
509
510     close(sock);
511     return rc;
512 } // StunRequest
513
514 /*---------------------------------------------------------------------*/
515 // Input: two random values (pos, step) for generate uniuqe way over server
516 // list
517 // Output: populate struct struct mapped
518 // Retval:
519
520 int GetExternalIPbySTUN(uint64_t rnd, struct sockaddr_in *mapped, const char **srv) {
521     randfiller    = rnd;
522     uint16_t pos  = rnd;
523     uint16_t step;
524     do {
525         rnd = (rnd >> 8) | 0xff00000000000000LL;
526         step = rnd % StunSrvListQty;
527     } while(step == 0);
528
529     uint16_t attempt;
530     for(attempt = 1; attempt < StunSrvListQty * 2; attempt++) {
531         pos = (pos + step) % StunSrvListQty;
532         int rc = StunRequest(*srv = StunSrvList[pos].name, StunSrvList[pos].port, mapped);
533         if(rc >= 0)
534             return attempt;
535         // fprintf(stderr, "Lookup: %s:%u\t%s\t%d\n", StunSrvList[pos].name,
536         // StunSrvList[pos].port, inet_ntoa(mapped->sin_addr), rc);
537     }
538     return -1;
539 }