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