11225
|
1 /**
|
|
2 * @file stun.c STUN (RFC3489) Implementation
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Gaim is the legal property of its developers, whose names are too numerous
|
|
8 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
9 * source distribution.
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2 of the License, or
|
|
14 * (at your option) any later version.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
24 *
|
|
25 * TODO: currently only detects if there is a NAT and not the type of NAT
|
|
26 */
|
|
27
|
|
28 #include <sys/socket.h>
|
|
29 #include <ifaddrs.h>
|
|
30 #include <net/if.h>
|
|
31 #include <sys/ioctl.h>
|
|
32 #include <resolv.h>
|
|
33
|
|
34 #include "internal.h"
|
|
35
|
|
36 #include "debug.h"
|
|
37 #include "account.h"
|
|
38 #include "stun.h"
|
|
39 #include "prefs.h"
|
|
40
|
|
41 struct stun_nattype nattype = {-1, 0, "\0"};
|
|
42
|
|
43 static gint transid[4];
|
|
44
|
|
45 static GSList *callbacks = 0;
|
|
46 static int fd = -1;
|
|
47 gint incb = -1;
|
|
48
|
|
49 static void do_callbacks() {
|
|
50 while(callbacks) {
|
|
51 StunCallback cb = callbacks->data;
|
|
52 cb(&nattype);
|
|
53 callbacks = g_slist_remove(callbacks, cb);
|
|
54 }
|
|
55 }
|
|
56
|
|
57 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) {
|
|
58 char buffer[10240];
|
|
59 char *tmp;
|
|
60 int len;
|
|
61 struct in_addr in;
|
|
62 struct stun_attrib *attrib;
|
|
63 struct stun_header *hdr;
|
|
64
|
|
65 len = recv(source, buffer, 10240, 0);
|
|
66
|
|
67 hdr = (struct stun_header*)buffer;
|
|
68 if(hdr->transid[0]!=transid[0] || hdr->transid[1]!=transid[1] || hdr->transid[2]!=transid[2] || hdr->transid[3]!=transid[3]) { // wrong transaction
|
|
69 gaim_debug_info("simple", "got wrong transid\n");
|
|
70 return;
|
|
71 }
|
|
72
|
|
73 tmp = buffer + sizeof(struct stun_header);
|
|
74 while(buffer+len > tmp) {
|
|
75
|
|
76 attrib = (struct stun_attrib*) tmp;
|
|
77 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) {
|
|
78 memcpy(&in.s_addr, tmp+sizeof(struct stun_attrib)+2+2, 4);
|
|
79 strcpy(nattype.publicip, inet_ntoa(in));
|
|
80 }
|
|
81 tmp += sizeof(struct stun_attrib) + attrib->len;
|
|
82 }
|
|
83 gaim_debug_info("simple", "got public ip %s\n",nattype.publicip);
|
|
84 nattype.status = 2;
|
|
85 nattype.type = 1;
|
|
86
|
|
87 // is it a NAT?
|
|
88 struct ifconf ifc;
|
|
89 struct ifreq *ifr;
|
|
90 struct sockaddr_in *sinptr;
|
|
91
|
|
92 ifc.ifc_len = sizeof(buffer);
|
|
93 ifc.ifc_req = (struct ifreq *) buffer;
|
|
94 ioctl(source, SIOCGIFCONF, &ifc);
|
|
95
|
|
96 tmp = buffer;
|
|
97 while(tmp < buffer + ifc.ifc_len) {
|
|
98 ifr = (struct ifreq *) tmp;
|
|
99 len = sizeof(struct sockaddr);
|
|
100
|
|
101 tmp += sizeof(ifr->ifr_name) + len;
|
|
102
|
|
103 if(ifr->ifr_addr.sa_family == AF_INET) {
|
|
104 // we only care about ipv4 interfaces
|
|
105 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
|
|
106 if(sinptr->sin_addr.s_addr == in.s_addr) {
|
|
107 // no NAT
|
|
108 gaim_debug_info("simple","no nat");
|
|
109 nattype.type = 0;
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113
|
|
114 do_callbacks();
|
|
115 gaim_input_remove(incb);
|
|
116 }
|
|
117
|
|
118 struct stun_nattype *gaim_stun_discover(StunCallback cb) {
|
|
119 struct sockaddr_in addr;
|
|
120 struct stun_header data;
|
|
121 int ret = 0;
|
|
122
|
|
123 if(nattype.status == 1) { // currently discovering
|
|
124 callbacks = g_slist_append(callbacks, cb);
|
|
125 return NULL;
|
|
126 }
|
|
127 if(nattype.status == 2 || nattype.status == 0) { // already discovered
|
|
128 cb(&nattype);
|
|
129 return &nattype;
|
|
130 }
|
|
131
|
|
132 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
133 nattype.status = 0;
|
|
134 cb(&nattype);
|
|
135 return &nattype;
|
|
136 }
|
|
137
|
|
138 addr.sin_family = AF_INET;
|
|
139 addr.sin_port = htons(12108);
|
|
140 addr.sin_addr.s_addr = INADDR_ANY;
|
|
141 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) {
|
|
142 addr.sin_port = htons(ntohs(addr.sin_port)+1);
|
|
143 }
|
|
144 if( ret < 0 ) {
|
|
145 nattype.status = 0;
|
|
146 cb(&nattype);
|
|
147 return &nattype;
|
|
148 }
|
|
149 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL);
|
|
150
|
|
151 char *ip = gaim_prefs_get_string("/core/network/stun_ip");
|
|
152 int port = gaim_prefs_get_int("/core/network/stun_port");
|
|
153
|
|
154 if(port == 0 || ip == NULL || ip[0] == '\0') return NULL;
|
|
155
|
|
156 addr.sin_family = AF_INET;
|
|
157 addr.sin_port = htons(port);
|
|
158 addr.sin_addr.s_addr = inet_addr(ip);
|
|
159
|
|
160 data.type = htons(0x0001);
|
|
161 data.len = 0;
|
|
162 transid[0] = data.transid[0] = rand();
|
|
163 transid[1] = data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
|
|
164 transid[2] = data.transid[2] = rand();
|
|
165 transid[3] = data.transid[3] = rand();
|
|
166
|
|
167 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
|
|
168 nattype.status = 0;
|
|
169 cb(&nattype);
|
|
170 return &nattype;
|
|
171 }
|
|
172 return NULL;
|
|
173 }
|