Mercurial > pidgin.yaz
annotate src/stun.c @ 11300:dd1a5969b2e5
[gaim-migrate @ 13500]
Fix two C99isms. I recommend -Wdeclaration-after-statement for everyone.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Thu, 18 Aug 2005 06:36:05 +0000 |
parents | 67f874fadb57 |
children | 7d7dd22215ec |
rev | line source |
---|---|
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; | |
11300
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
64 struct ifconf ifc; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
65 struct ifreq *ifr; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
66 struct sockaddr_in *sinptr; |
11225 | 67 |
68 len = recv(source, buffer, 10240, 0); | |
69 | |
70 hdr = (struct stun_header*)buffer; | |
71 if(hdr->transid[0]!=transid[0] || hdr->transid[1]!=transid[1] || hdr->transid[2]!=transid[2] || hdr->transid[3]!=transid[3]) { // wrong transaction | |
72 gaim_debug_info("simple", "got wrong transid\n"); | |
73 return; | |
74 } | |
75 | |
76 tmp = buffer + sizeof(struct stun_header); | |
77 while(buffer+len > tmp) { | |
78 | |
79 attrib = (struct stun_attrib*) tmp; | |
80 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) { | |
81 memcpy(&in.s_addr, tmp+sizeof(struct stun_attrib)+2+2, 4); | |
82 strcpy(nattype.publicip, inet_ntoa(in)); | |
83 } | |
84 tmp += sizeof(struct stun_attrib) + attrib->len; | |
85 } | |
86 gaim_debug_info("simple", "got public ip %s\n",nattype.publicip); | |
87 nattype.status = 2; | |
88 nattype.type = 1; | |
89 | |
90 // is it a NAT? | |
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; | |
11300
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
122 const char *ip = gaim_prefs_get_string("/core/network/stun_ip"); |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
123 int port = gaim_prefs_get_int("/core/network/stun_port"); |
11225 | 124 |
125 if(nattype.status == 1) { // currently discovering | |
126 callbacks = g_slist_append(callbacks, cb); | |
127 return NULL; | |
128 } | |
129 if(nattype.status == 2 || nattype.status == 0) { // already discovered | |
130 cb(&nattype); | |
131 return &nattype; | |
132 } | |
133 | |
134 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | |
135 nattype.status = 0; | |
136 cb(&nattype); | |
137 return &nattype; | |
138 } | |
139 | |
140 addr.sin_family = AF_INET; | |
141 addr.sin_port = htons(12108); | |
142 addr.sin_addr.s_addr = INADDR_ANY; | |
143 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) { | |
144 addr.sin_port = htons(ntohs(addr.sin_port)+1); | |
145 } | |
146 if( ret < 0 ) { | |
147 nattype.status = 0; | |
148 cb(&nattype); | |
149 return &nattype; | |
150 } | |
151 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL); | |
152 | |
153 if(port == 0 || ip == NULL || ip[0] == '\0') return NULL; | |
154 | |
155 addr.sin_family = AF_INET; | |
156 addr.sin_port = htons(port); | |
157 addr.sin_addr.s_addr = inet_addr(ip); | |
158 | |
159 data.type = htons(0x0001); | |
160 data.len = 0; | |
161 transid[0] = data.transid[0] = rand(); | |
162 transid[1] = data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); | |
163 transid[2] = data.transid[2] = rand(); | |
164 transid[3] = data.transid[3] = rand(); | |
165 | |
166 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { | |
167 nattype.status = 0; | |
168 cb(&nattype); | |
169 return &nattype; | |
170 } | |
171 return NULL; | |
172 } |