Mercurial > pidgin
annotate src/protocols/gg/libgg.c @ 7386:414c701ef1ff
[gaim-migrate @ 7981]
Code cleanup to get rid of all warnings for AMD64 from augustus at linuxhardware dot org
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Thu, 30 Oct 2003 22:27:36 +0000 |
parents | 9860b81548d8 |
children | ce90b119b103 |
rev | line source |
---|---|
6247 | 1 /* $Id: libgg.c 6741 2003-07-20 19:11:13Z thekingant $ */ |
2393 | 2 |
3 /* | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
4 * (C) Copyright 2001 Wojtek Kaniewski <wojtekka@irc.pl>, |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
5 * Robert J. Wo¼ny <speedy@ziew.org> |
2393 | 6 * |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License Version 2 as | |
9 * published by the Free Software Foundation. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 */ | |
20 | |
4016 | 21 #include <sys/types.h> |
3630 | 22 #ifndef _WIN32 |
2393 | 23 #include <sys/socket.h> |
24 #include <netinet/in.h> | |
25 #include <arpa/inet.h> | |
26 #include <sys/ioctl.h> | |
27 #include <sys/wait.h> | |
3630 | 28 #include <netdb.h> |
29 #include <pwd.h> | |
30 #else | |
31 #include <fcntl.h> | |
32 #endif | |
33 | |
34 #include <stdio.h> | |
35 #include <stdlib.h> | |
36 #include <unistd.h> | |
37 #include <stdio.h> | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
38 #include <sys/time.h> |
2393 | 39 #include <errno.h> |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
40 #ifndef _AIX |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
41 # include <string.h> |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
42 #endif |
2393 | 43 #include <stdarg.h> |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
44 #include <time.h> |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
45 #ifdef sun |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
46 #include <sys/filio.h> |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
47 #endif |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
48 #include <glib.h> |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
49 #if G_BYTE_ORDER == G_BIG_ENDIAN |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
50 # define WORDS_BIGENDIAN 1 |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
51 #endif |
2393 | 52 #include "libgg.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
53 |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
54 #include "internal.h" |
3630 | 55 #include "proxy.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
56 #include "debug.h" |
2393 | 57 |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
58 #ifdef _WIN32 |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
59 #include "win32dep.h" |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
60 #endif |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
61 |
3630 | 62 int gg_debug_level = (GG_DEBUG_NET | GG_DEBUG_TRAFFIC | GG_DEBUG_DUMP | GG_DEBUG_FUNCTION | GG_DEBUG_MISC); |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
63 int gg_http_use_proxy = 0; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
64 int gg_http_proxy_port = 0; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
65 char *gg_http_proxy_host = NULL; |
2393 | 66 |
3630 | 67 /* temp -Herman */ |
68 static int ping_outstanding = 0; | |
69 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
70 #ifndef lint |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
71 |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
72 static char rcsid[] |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
73 #ifdef __GNUC__ |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
74 __attribute__ ((unused)) |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
75 #endif |
6247 | 76 = "$Id: libgg.c 6741 2003-07-20 19:11:13Z thekingant $"; |
2393 | 77 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
78 #endif |
2393 | 79 |
80 /* | |
81 * fix32() // funkcja wewnźtrzna | |
82 * | |
83 * dla maszyn big-endianowych zamienia kolejno¶ę bajtów w ,,long''ach. | |
84 */ | |
85 static inline unsigned long fix32(unsigned long x) | |
86 { | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
87 #ifndef WORDS_BIGENDIAN |
2393 | 88 return x; |
89 #else | |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
90 return (unsigned long) |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
91 (((x & (unsigned long) 0x000000ffU) << 24) | |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
92 ((x & (unsigned long) 0x0000ff00U) << 8) | |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
93 ((x & (unsigned long) 0x00ff0000U) >> 8) | |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
94 ((x & (unsigned long) 0xff000000U) >> 24)); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
95 #endif |
2393 | 96 } |
97 | |
98 /* | |
99 * fix16() // funkcja wewnźtrzna | |
100 * | |
101 * dla maszyn big-endianowych zamienia kolejno¶ę bajtów w ,,short''ach. | |
102 */ | |
4298 | 103 |
104 /* not currently used | |
2393 | 105 static inline unsigned short fix16(unsigned short x) |
106 { | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
107 #ifndef WORDS_BIGENDIAN |
2393 | 108 return x; |
109 #else | |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
110 return (unsigned short) |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
111 (((x & (unsigned short) 0x00ffU) << 8) | |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
112 ((x & (unsigned short) 0xff00U) >> 8)); |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
113 #endif |
2393 | 114 } |
4298 | 115 */ |
2393 | 116 |
3630 | 117 #ifndef _WIN32 |
2393 | 118 /* |
119 * gg_resolve() // funkcja wewnźtrzna | |
120 * | |
121 * tworzy pipe'y, forkuje siź i w drugim procesie zaczyna resolvowaę | |
122 * podanego hosta. zapisuje w sesji deskryptor pipe'u. je¶li co¶ tam | |
123 * bździe gotowego, znaczy, æe moæna wczytaę ,,struct in_addr''. je¶li | |
124 * nie znajdzie, zwraca INADDR_NONE. | |
125 * | |
126 * - fd - wska¼nik gdzie wrzucię deskryptor, | |
127 * - pid - gdzie wrzucię pid dzieciaka, | |
128 * - hostname - nazwa hosta do zresolvowania. | |
129 * | |
130 * zwraca 0 je¶li uda³o siź odpalię proces lub -1 w przypadku b³źdu. | |
131 */ | |
132 int gg_resolve(int *fd, int *pid, char *hostname) | |
133 { | |
134 int pipes[2], res; | |
135 struct in_addr a; | |
136 | |
137 gg_debug(GG_DEBUG_FUNCTION, "** gg_resolve(..., \"%s\");\n", hostname); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
138 |
2393 | 139 if (!fd | !pid) { |
140 errno = EFAULT; | |
141 return -1; | |
142 } | |
143 | |
144 if (pipe(pipes) == -1) | |
145 return -1; | |
146 | |
147 if ((res = fork()) == -1) | |
148 return -1; | |
149 | |
150 if (!res) { | |
151 if ((a.s_addr = inet_addr(hostname)) == INADDR_NONE) { | |
152 struct hostent *he; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
153 |
2393 | 154 if (!(he = gethostbyname(hostname))) |
155 a.s_addr = INADDR_NONE; | |
156 else | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
157 memcpy((char*) &a, he->h_addr, sizeof(a)); |
2393 | 158 } |
159 | |
160 write(pipes[1], &a, sizeof(a)); | |
161 | |
162 exit(0); | |
163 } | |
164 | |
165 close(pipes[1]); | |
166 | |
167 *fd = pipes[0]; | |
168 *pid = res; | |
169 | |
170 return 0; | |
171 } | |
3630 | 172 #endif /*!_WIN32*/ |
2393 | 173 |
174 /* | |
175 * gg_recv_packet() // funkcja wewnźtrzna | |
176 * | |
177 * odbiera jeden pakiet gg i zwraca wska¼nik do niego. pamiźę po nim | |
178 * wypada³oby uwolnię. | |
179 * | |
180 * - sock - po³±czony socket. | |
181 * | |
182 * je¶li wyst±pi³ b³±d, zwraca NULL. reszta w errno. | |
183 */ | |
184 static void *gg_recv_packet(struct gg_session *sess) | |
185 { | |
186 struct gg_header h; | |
187 char *buf = NULL; | |
188 int ret = 0, offset, size = 0; | |
3630 | 189 int sizeh = sizeof(struct gg_header); |
2393 | 190 |
191 gg_debug(GG_DEBUG_FUNCTION, "** gg_recv_packet(...);\n"); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
192 |
2393 | 193 if (!sess) { |
194 errno = EFAULT; | |
195 return NULL; | |
196 } | |
197 | |
198 if (sess->recv_left < 1) { | |
3630 | 199 while (ret != sizeh) { |
200 ret = read(sess->fd, &h, sizeh); | |
201 gg_debug(GG_DEBUG_MISC, "-- header recv(..., %d) = %d\n", sizeh, ret); | |
202 if (ret < sizeh) { | |
2393 | 203 if (errno != EINTR) { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
204 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno)); |
2393 | 205 return NULL; |
206 } | |
207 } | |
208 } | |
209 | |
210 h.type = fix32(h.type); | |
211 h.length = fix32(h.length); | |
212 } else { | |
3630 | 213 memcpy(&h, sess->recv_buf, sizeh); |
2393 | 214 } |
215 | |
216 /* jakie¶ sensowne limity na rozmiar pakietu */ | |
217 if (h.length < 0 || h.length > 65535) { | |
218 gg_debug(GG_DEBUG_MISC, "-- invalid packet length (%d)\n", h.length); | |
219 errno = ERANGE; | |
220 return NULL; | |
221 } | |
222 | |
223 if (sess->recv_left > 0) { | |
224 gg_debug(GG_DEBUG_MISC, "-- resuming last gg_recv_packet()\n"); | |
225 size = sess->recv_left; | |
226 offset = sess->recv_done; | |
227 buf = sess->recv_buf; | |
228 } else { | |
3630 | 229 if (!(buf = malloc(sizeh + h.length + 1))) { |
2393 | 230 gg_debug(GG_DEBUG_MISC, "-- not enough memory\n"); |
231 return NULL; | |
232 } | |
233 | |
3630 | 234 memcpy(buf, &h, sizeh); |
2393 | 235 |
236 offset = 0; | |
237 size = h.length; | |
238 } | |
239 | |
240 while (size > 0) { | |
3630 | 241 ret = read(sess->fd, buf + sizeh + offset, size); |
2393 | 242 gg_debug(GG_DEBUG_MISC, "-- body recv(..., %d) = %d\n", size, ret); |
243 if (ret > -1 && ret <= size) { | |
244 offset += ret; | |
245 size -= ret; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
246 } else if (ret == -1) { |
2393 | 247 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno)); |
248 if (errno == EAGAIN) { | |
249 gg_debug(GG_DEBUG_MISC, "-- %d bytes received, %d left\n", offset, size); | |
250 sess->recv_buf = buf; | |
251 sess->recv_left = size; | |
252 sess->recv_done = offset; | |
253 return NULL; | |
254 } | |
255 if (errno != EINTR) { | |
6063 | 256 /* errno = EINVAL; */ |
2393 | 257 free(buf); |
258 return NULL; | |
259 } | |
260 } | |
261 } | |
262 | |
263 sess->recv_left = 0; | |
264 | |
265 if ((gg_debug_level & GG_DEBUG_DUMP)) { | |
266 int i; | |
267 | |
268 gg_debug(GG_DEBUG_DUMP, ">> received packet (type=%.2x):", h.type); | |
3630 | 269 for (i = 0; i < sizeh + h.length; i++) |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
270 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) buf[i]); |
2393 | 271 gg_debug(GG_DEBUG_DUMP, "\n"); |
272 } | |
273 | |
274 return buf; | |
275 } | |
276 | |
277 /* | |
278 * gg_send_packet() // funkcja wewnźtrzna | |
279 * | |
280 * konstruuje pakiet i wysy³a go w do serwera. | |
281 * | |
282 * - sock - po³±czony socket, | |
283 * - type - typ pakietu, | |
284 * - packet - wska¼nik do struktury pakietu, | |
285 * - length - d³ugo¶ę struktury pakietu, | |
286 * - payload - dodatkowy tekst doklejany do pakietu (np. wiadomo¶ę), | |
287 * - payload_length - d³ugo¶ę dodatkowego tekstu. | |
288 * | |
289 * je¶li posz³o dobrze, zwraca 0. w przypadku b³źdu -1. je¶li errno=ENOMEM, | |
290 * zabrak³o pamiźci. inaczej by³ b³±d przy wysy³aniu pakietu. dla errno=0 | |
291 * nie wys³ano ca³ego pakietu. | |
292 */ | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
293 static int gg_send_packet(int sock, int type, void *packet, int length, void *payload, int payload_length) |
2393 | 294 { |
295 struct gg_header *h; | |
296 int res, plen; | |
297 char *tmp; | |
298 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
299 gg_debug(GG_DEBUG_FUNCTION, "** gg_send_packet(0x%.2x, %d, %d);\n", type, length, payload_length); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
300 |
2393 | 301 if (length < 0 || payload_length < 0) { |
302 gg_debug(GG_DEBUG_MISC, "-- invalid packet/payload length\n"); | |
303 errno = ERANGE; | |
304 return -1; | |
305 } | |
306 | |
307 if (!(tmp = malloc(sizeof(struct gg_header) + length + payload_length))) { | |
308 gg_debug(GG_DEBUG_MISC, "-- not enough memory\n"); | |
309 return -1; | |
310 } | |
311 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
312 h = (struct gg_header*) tmp; |
2393 | 313 h->type = fix32(type); |
314 h->length = fix32(length + payload_length); | |
315 | |
316 if (packet) | |
317 memcpy(tmp + sizeof(struct gg_header), packet, length); | |
318 if (payload) | |
319 memcpy(tmp + sizeof(struct gg_header) + length, payload, payload_length); | |
320 | |
321 if ((gg_debug_level & GG_DEBUG_DUMP)) { | |
322 int i; | |
323 | |
324 gg_debug(GG_DEBUG_DUMP, "%%%% sending packet (type=%.2x):", fix32(h->type)); | |
325 for (i = 0; i < sizeof(struct gg_header) + fix32(h->length); i++) | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
326 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) tmp[i]); |
2393 | 327 gg_debug(GG_DEBUG_DUMP, "\n"); |
328 } | |
329 | |
330 plen = sizeof(struct gg_header) + length + payload_length; | |
331 if ((res = write(sock, tmp, plen)) < plen) { | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
332 gg_debug(GG_DEBUG_MISC, "-- write() failed. res = %d, errno = %d (%s)\n", res, errno, strerror(errno)); |
2393 | 333 free(tmp); |
334 return -1; | |
335 } | |
336 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
337 free(tmp); |
2393 | 338 return 0; |
339 } | |
340 | |
3630 | 341 #ifndef _WIN32 |
2393 | 342 /* |
343 * gg_login() | |
344 * | |
345 * rozpoczyna procedurź ³±czenia siź z serwerem. resztź obs³guje siź przez | |
346 * gg_watch_event. | |
347 * | |
348 * - uin - numerek usera, | |
349 * - password - jego hase³ko, | |
350 * - async - ma byę asynchronicznie? | |
351 * | |
352 * UWAGA! program musi obs³uæyę SIGCHLD, je¶li ³±czy siź asynchronicznie, | |
353 * æeby zrobię pogrzeb zmar³emu procesowi resolvera. | |
354 * | |
355 * w przypadku b³źdu zwraca NULL, je¶li idzie dobrze (async) albo posz³o | |
356 * dobrze (sync), zwróci wska¼nik do zaalokowanej struktury `gg_session'. | |
357 */ | |
358 struct gg_session *gg_login(uin_t uin, char *password, int async) | |
359 { | |
360 struct gg_session *sess; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
361 char *hostname; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
362 int port; |
2393 | 363 |
364 gg_debug(GG_DEBUG_FUNCTION, "** gg_login(%u, \"...\", %d);\n", uin, async); | |
365 | |
366 if (!(sess = malloc(sizeof(*sess)))) | |
367 return NULL; | |
368 | |
369 sess->uin = uin; | |
370 if (!(sess->password = strdup(password))) { | |
371 free(sess); | |
372 return NULL; | |
373 } | |
374 sess->state = GG_STATE_RESOLVING; | |
375 sess->check = GG_CHECK_READ; | |
376 sess->async = async; | |
377 sess->seq = 0; | |
378 sess->recv_left = 0; | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
379 sess->last_pong = 0; |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
380 sess->server_ip = 0; |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
381 sess->initial_status = 0; |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
382 sess->type = GG_SESSION_GG; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
383 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
384 if (gg_http_use_proxy) { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
385 hostname = gg_http_proxy_host; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
386 port = gg_http_proxy_port; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
387 } else { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
388 hostname = GG_APPMSG_HOST; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
389 port = GG_APPMSG_PORT; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
390 }; |
2393 | 391 |
392 if (async) { | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
393 if (gg_resolve(&sess->fd, &sess->pid, hostname)) { |
2393 | 394 gg_debug(GG_DEBUG_MISC, "-- resolving failed\n"); |
395 free(sess); | |
396 return NULL; | |
397 } | |
398 } else { | |
399 struct in_addr a; | |
400 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
401 if ((a.s_addr = inet_addr(hostname)) == INADDR_NONE) { |
2393 | 402 struct hostent *he; |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
403 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
404 if (!(he = gethostbyname(hostname))) { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
405 gg_debug(GG_DEBUG_MISC, "-- host %s not found\n", hostname); |
2393 | 406 free(sess); |
407 return NULL; | |
408 } else | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
409 memcpy((char*) &a, he->h_addr, sizeof(a)); |
2393 | 410 } |
411 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
412 if (!(sess->fd = gg_connect(&a, port, 0)) == -1) { |
2393 | 413 gg_debug(GG_DEBUG_MISC, "-- connection failed\n"); |
414 free(sess); | |
415 return NULL; | |
416 } | |
417 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
418 sess->state = GG_STATE_CONNECTING; |
2393 | 419 |
420 while (sess->state != GG_STATE_CONNECTED) { | |
421 struct gg_event *e; | |
422 | |
423 if (!(e = gg_watch_fd(sess))) { | |
424 gg_debug(GG_DEBUG_MISC, "-- some nasty error in gg_watch_fd()\n"); | |
425 free(sess); | |
426 return NULL; | |
427 } | |
428 | |
429 if (e->type == GG_EVENT_CONN_FAILED) { | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
430 errno = EACCES; |
2393 | 431 gg_debug(GG_DEBUG_MISC, "-- could not login\n"); |
432 gg_free_event(e); | |
433 free(sess); | |
434 return NULL; | |
435 } | |
436 | |
437 gg_free_event(e); | |
438 } | |
439 } | |
440 | |
441 return sess; | |
442 } | |
3630 | 443 #endif /*!_WIN32*/ |
2393 | 444 |
445 /* | |
446 * gg_free_session() | |
447 * | |
448 * zwalnia pamiźę zajmowan± przez opis sesji. | |
449 * | |
450 * - sess - opis sesji. | |
451 * | |
452 * nie zwraca niczego, bo i po co? | |
453 */ | |
454 void gg_free_session(struct gg_session *sess) | |
455 { | |
456 if (!sess) | |
457 return; | |
458 | |
459 free(sess->password); | |
460 free(sess); | |
461 } | |
462 | |
463 /* | |
464 * gg_change_status() | |
465 * | |
466 * zmienia status uæytkownika. przydatne do /away i /busy oraz /quit. | |
467 * | |
468 * - sess - opis sesji, | |
469 * - status - nowy status uæytkownika. | |
470 * | |
471 * je¶li wys³a³ pakiet zwraca 0, je¶li nie uda³o siź, zwraca -1. | |
472 */ | |
473 int gg_change_status(struct gg_session *sess, int status) | |
474 { | |
475 struct gg_new_status p; | |
476 | |
477 if (!sess) { | |
478 errno = EFAULT; | |
479 return -1; | |
480 } | |
481 | |
482 if (sess->state != GG_STATE_CONNECTED) { | |
483 errno = ENOTCONN; | |
484 return -1; | |
485 } | |
486 | |
487 gg_debug(GG_DEBUG_FUNCTION, "** gg_change_status(..., %d);\n", status); | |
488 | |
489 p.status = fix32(status); | |
490 | |
491 return gg_send_packet(sess->fd, GG_NEW_STATUS, &p, sizeof(p), NULL, 0); | |
492 } | |
493 | |
494 /* | |
495 * gg_logoff() | |
496 * | |
497 * wylogowuje uæytkownika i zamyka po³±czenie. | |
498 * | |
499 * - sock - deskryptor socketu. | |
500 * | |
501 * nie zwraca b³źdów. skoro siź æegnamy, to olewamy wszystko. | |
502 */ | |
503 void gg_logoff(struct gg_session *sess) | |
504 { | |
505 if (!sess) | |
506 return; | |
507 | |
508 gg_debug(GG_DEBUG_FUNCTION, "** gg_logoff(...);\n"); | |
509 | |
510 if (sess->state == GG_STATE_CONNECTED) | |
511 gg_change_status(sess, GG_STATUS_NOT_AVAIL); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
512 |
2393 | 513 if (sess->fd) { |
514 shutdown(sess->fd, 2); | |
515 close(sess->fd); | |
516 } | |
517 } | |
518 | |
519 /* | |
520 * gg_send_message() | |
521 * | |
522 * wysy³a wiadomo¶ę do innego uæytkownika. zwraca losowy numer | |
523 * sekwencyjny, który moæna olaę albo wykorzystaę do potwierdzenia. | |
524 * | |
525 * - sess - opis sesji, | |
526 * - msgclass - rodzaj wiadomo¶ci, | |
527 * - recipient - numer adresata, | |
528 * - message - tre¶ę wiadomo¶ci. | |
529 * | |
530 * w przypadku b³źdu zwraca -1, inaczej numer sekwencyjny. | |
531 */ | |
6063 | 532 int gg_send_message(struct gg_session *sess, int msgclass, uin_t recipient, char *message) |
2393 | 533 { |
534 struct gg_send_msg s; | |
535 | |
536 if (!sess) { | |
537 errno = EFAULT; | |
538 return -1; | |
539 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
540 |
2393 | 541 if (sess->state != GG_STATE_CONNECTED) { |
542 errno = ENOTCONN; | |
543 return -1; | |
544 } | |
545 | |
546 gg_debug(GG_DEBUG_FUNCTION, "** gg_send_message(..., %d, %u, \"...\");\n", msgclass, recipient); | |
547 | |
548 s.recipient = fix32(recipient); | |
549 if (!sess->seq) | |
550 sess->seq = 0x01740000 | (rand() & 0xffff); | |
551 s.seq = fix32(sess->seq); | |
552 s.msgclass = fix32(msgclass); | |
553 sess->seq += (rand() % 0x300) + 0x300; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
554 |
2393 | 555 if (gg_send_packet(sess->fd, GG_SEND_MSG, &s, sizeof(s), message, strlen(message) + 1) == -1) |
556 return -1; | |
557 | |
558 return fix32(s.seq); | |
559 } | |
560 | |
561 /* | |
562 * gg_ping() | |
563 * | |
564 * wysy³a do serwera pakiet typu yeah-i'm-still-alive. | |
565 * | |
566 * - sess - zgadnij. | |
567 * | |
568 * je¶li nie powiod³o siź wys³anie pakietu, zwraca -1. otherwise 0. | |
569 */ | |
570 int gg_ping(struct gg_session *sess) | |
571 { | |
572 if (!sess) { | |
573 errno = EFAULT; | |
574 return -1; | |
575 } | |
576 | |
577 if (sess->state != GG_STATE_CONNECTED) { | |
578 errno = ENOTCONN; | |
579 return -1; | |
580 } | |
581 | |
582 gg_debug(GG_DEBUG_FUNCTION, "** gg_ping(...);\n"); | |
3630 | 583 |
584 if(ping_outstanding) { | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
585 gaim_debug(GAIM_DEBUG_INFO, "gg", |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
586 "Trying to send ping, when we havn't been ponged on last ping\n"); |
3630 | 587 return 1; |
588 } | |
589 else { | |
590 ping_outstanding = 1; | |
591 return gg_send_packet(sess->fd, GG_PING, NULL, 0, NULL, 0); | |
592 } | |
2393 | 593 } |
594 | |
595 /* | |
596 * gg_free_event() | |
597 * | |
598 * zwalnia pamiźę zajmowan± przez informacjź o zdarzeniu | |
599 * | |
600 * - event - wska¼nik do informacji o zdarzeniu | |
601 * | |
602 * nie ma czego zwracaę. | |
603 */ | |
604 void gg_free_event(struct gg_event *e) | |
605 { | |
606 if (!e) | |
607 return; | |
608 if (e->type == GG_EVENT_MSG) | |
609 free(e->event.msg.message); | |
610 if (e->type == GG_EVENT_NOTIFY) | |
611 free(e->event.notify); | |
612 free(e); | |
613 } | |
614 | |
615 /* | |
616 * gg_notify() | |
617 * | |
618 * wysy³a serwerowi listź ludków, za którymi tźsknimy. | |
619 * | |
620 * - sess - identyfikator sesji, | |
621 * - userlist - wska¼nik do tablicy numerów, | |
622 * - count - ilo¶ę numerków. | |
623 * | |
624 * je¶li uda³o siź, zwraca 0. je¶li b³±d, dostajemy -1. | |
625 */ | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
626 int gg_notify(struct gg_session *sess, uin_t *userlist, int count) |
2393 | 627 { |
628 struct gg_notify *n; | |
629 uin_t *u; | |
630 int i, res = 0; | |
631 | |
632 if (!sess) { | |
633 errno = EFAULT; | |
634 return -1; | |
635 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
636 |
2393 | 637 if (sess->state != GG_STATE_CONNECTED) { |
638 errno = ENOTCONN; | |
639 return -1; | |
640 } | |
641 | |
642 gg_debug(GG_DEBUG_FUNCTION, "** gg_notify(..., %d);\n", count); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
643 |
2393 | 644 if (!userlist || !count) |
645 return 0; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
646 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
647 if (!(n = (struct gg_notify*) malloc(sizeof(*n) * count))) |
2393 | 648 return -1; |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
649 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
650 for (u = userlist, i = 0; i < count; u++, i++) { |
2393 | 651 n[i].uin = fix32(*u); |
652 n[i].dunno1 = 3; | |
653 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
654 |
2393 | 655 if (gg_send_packet(sess->fd, GG_NOTIFY, n, sizeof(*n) * count, NULL, 0) == -1) |
656 res = -1; | |
657 | |
658 free(n); | |
659 | |
660 return res; | |
661 } | |
662 | |
663 /* | |
664 * gg_add_notify() | |
665 * | |
666 * dodaje w locie do listy ukochanych dany numerek. | |
667 * | |
668 * - sess - identyfikator sesji, | |
669 * - uin - numerek ukochanej. | |
670 * | |
671 * je¶li uda³o siź wys³aę, daje 0. inaczej -1. | |
672 */ | |
673 int gg_add_notify(struct gg_session *sess, uin_t uin) | |
674 { | |
675 struct gg_add_remove a; | |
676 | |
677 if (!sess) { | |
678 errno = EFAULT; | |
679 return -1; | |
680 } | |
681 | |
682 if (sess->state != GG_STATE_CONNECTED) { | |
683 errno = ENOTCONN; | |
684 return -1; | |
685 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
686 |
2393 | 687 gg_debug(GG_DEBUG_FUNCTION, "** gg_add_notify(..., %u);\n", uin); |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
688 |
2393 | 689 a.uin = fix32(uin); |
690 a.dunno1 = 3; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
691 |
2393 | 692 return gg_send_packet(sess->fd, GG_ADD_NOTIFY, &a, sizeof(a), NULL, 0); |
693 } | |
694 | |
695 /* | |
696 * gg_remove_notify() | |
697 * | |
698 * w locie usuwa z listy zainteresowanych. | |
699 * | |
700 * - sess - id sesji, | |
701 * - uin - numerek. | |
702 * | |
703 * zwraca -1 je¶li by³ b³±d, 0 je¶li siź uda³o wys³aę pakiet. | |
704 */ | |
705 int gg_remove_notify(struct gg_session *sess, uin_t uin) | |
706 { | |
707 struct gg_add_remove a; | |
708 | |
709 if (!sess) { | |
710 errno = EFAULT; | |
711 return -1; | |
712 } | |
713 | |
714 if (sess->state != GG_STATE_CONNECTED) { | |
715 errno = ENOTCONN; | |
716 return -1; | |
717 } | |
718 | |
719 gg_debug(GG_DEBUG_FUNCTION, "** gg_remove_notify(..., %u);\n", uin); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
720 |
2393 | 721 a.uin = fix32(uin); |
722 a.dunno1 = 3; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
723 |
2393 | 724 return gg_send_packet(sess->fd, GG_REMOVE_NOTIFY, &a, sizeof(a), NULL, 0); |
725 } | |
726 | |
727 /* | |
728 * gg_watch_fd_connected() // funkcja wewnźtrzna | |
729 * | |
730 * patrzy na socketa, odbiera pakiet i wype³nia strukturź zdarzenia. | |
731 * | |
732 * - sock - lalala, trudno zgadn±ę. | |
733 * | |
734 * je¶li b³±d -1, je¶li dobrze 0. | |
735 */ | |
736 static int gg_watch_fd_connected(struct gg_session *sess, struct gg_event *e) | |
737 { | |
738 struct gg_header *h; | |
739 void *p; | |
740 | |
741 if (!sess) { | |
742 errno = EFAULT; | |
743 return -1; | |
744 } | |
745 | |
746 gg_debug(GG_DEBUG_FUNCTION, "** gg_watch_fd_connected(...);\n"); | |
747 | |
748 if (!(h = gg_recv_packet(sess))) { | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
749 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet failed. errno = %d (%d)\n", errno, strerror(errno)); |
2393 | 750 return -1; |
751 } | |
752 | |
6247 | 753 p = (void *)h + sizeof(struct gg_header); |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
754 |
2393 | 755 if (h->type == GG_RECV_MSG) { |
756 struct gg_recv_msg *r = p; | |
757 | |
758 gg_debug(GG_DEBUG_MISC, "-- received a message\n"); | |
759 | |
760 if (h->length >= sizeof(*r)) { | |
761 e->type = GG_EVENT_MSG; | |
762 e->event.msg.msgclass = fix32(r->msgclass); | |
763 e->event.msg.sender = fix32(r->sender); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
764 e->event.msg.message = strdup((char*) r + sizeof(*r)); |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
765 e->event.msg.time = fix32(r->time); |
2393 | 766 } |
767 } | |
768 | |
769 if (h->type == GG_NOTIFY_REPLY) { | |
770 struct gg_notify_reply *n = p; | |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
771 int count, i; |
2393 | 772 |
773 gg_debug(GG_DEBUG_MISC, "-- received a notify reply\n"); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
774 |
2393 | 775 e->type = GG_EVENT_NOTIFY; |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
776 if (!(e->event.notify = (void*) malloc(h->length + 2 * sizeof(*n)))) { |
2393 | 777 gg_debug(GG_DEBUG_MISC, "-- not enough memory\n"); |
778 free(h); | |
779 return -1; | |
780 } | |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
781 count = h->length / sizeof(*n); |
2393 | 782 memcpy(e->event.notify, p, h->length); |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
783 e->event.notify[count].uin = 0; |
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
784 for (i = 0; i < count; i++) { |
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
785 e->event.notify[i].uin = fix32(e->event.notify[i].uin); |
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
786 e->event.notify[i].status = fix32(e->event.notify[i].status); |
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
787 } |
2393 | 788 } |
789 | |
790 if (h->type == GG_STATUS) { | |
791 struct gg_status *s = p; | |
792 | |
793 gg_debug(GG_DEBUG_MISC, "-- received a status change\n"); | |
794 | |
795 if (h->length >= sizeof(*s)) { | |
796 e->type = GG_EVENT_STATUS; | |
797 memcpy(&e->event.status, p, h->length); | |
2399
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
798 e->event.status.uin = fix32(e->event.status.uin); |
b2926d21f067
[gaim-migrate @ 2412]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
799 e->event.status.status = fix32(e->event.status.status); |
2393 | 800 } |
801 } | |
802 | |
803 if (h->type == GG_SEND_MSG_ACK) { | |
804 struct gg_send_msg_ack *s = p; | |
805 | |
806 gg_debug(GG_DEBUG_MISC, "-- received a message ack\n"); | |
807 | |
808 if (h->length >= sizeof(*s)) { | |
809 e->type = GG_EVENT_ACK; | |
810 e->event.ack.status = fix32(s->status); | |
811 e->event.ack.recipient = fix32(s->recipient); | |
812 e->event.ack.seq = fix32(s->seq); | |
813 } | |
814 } | |
815 | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
816 if (h->type == GG_PONG) { |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
817 gg_debug(GG_DEBUG_MISC, "-- received a pong\n"); |
3630 | 818 ping_outstanding = 0; |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
819 sess->last_pong = time(NULL); |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
820 } |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
821 |
2393 | 822 free(h); |
823 | |
824 return 0; | |
825 } | |
826 | |
827 /* | |
828 * gg_watch_fd() | |
829 * | |
830 * funkcja wywo³ywana, gdy co¶ siź stanie na obserwowanym deskryptorze. | |
831 * zwraca klientowi informacjź o tym, co siź dzieje. | |
832 * | |
833 * - sess - identyfikator sesji. | |
834 * | |
835 * zwraca wska¼nik do struktury gg_event, któr± trzeba zwolnię pó¼niej | |
836 * za pomoc± gg_free_event(). jesli rodzaj zdarzenia jest równy | |
837 * GG_EVENT_NONE, naleæy je olaę kompletnie. je¶li zwróci³o NULL, | |
838 * sta³o siź co¶ niedobrego -- albo brak³o pamiźci albo zerwa³o | |
839 * po³±czenie albo co¶ takiego. | |
840 */ | |
841 struct gg_event *gg_watch_fd(struct gg_session *sess) | |
842 { | |
843 struct gg_event *e; | |
844 int res = 0; | |
3630 | 845 #ifndef _WIN32 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
846 int port; |
3630 | 847 #endif |
2393 | 848 |
849 if (!sess) { | |
850 errno = EFAULT; | |
851 return NULL; | |
852 } | |
853 | |
854 gg_debug(GG_DEBUG_FUNCTION, "** gg_watch_fd(...);\n"); | |
855 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
856 if (!(e = (void*) malloc(sizeof(*e)))) { |
2393 | 857 gg_debug(GG_DEBUG_MISC, "-- not enough memory\n"); |
858 return NULL; | |
859 } | |
860 | |
861 e->type = GG_EVENT_NONE; | |
862 | |
863 switch (sess->state) { | |
3630 | 864 #ifndef _WIN32 |
865 /* Apparantly we will never be in this state as long as we are | |
5681
46d7ad0dfa26
[gaim-migrate @ 6100]
Christian Hammond <chipx86@chipx86.com>
parents:
4298
diff
changeset
|
866 using gaim_proxy_connect instead of gg_login - Herman */ |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
867 case GG_STATE_RESOLVING: |
2393 | 868 { |
869 struct in_addr a; | |
870 | |
871 gg_debug(GG_DEBUG_MISC, "== GG_STATE_RESOLVING\n"); | |
872 | |
873 if (read(sess->fd, &a, sizeof(a)) < sizeof(a) || a.s_addr == INADDR_NONE) { | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
874 gg_debug(GG_DEBUG_MISC, "-- resolving failed\n"); |
2393 | 875 |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
876 errno = ENOENT; |
2393 | 877 e->type = GG_EVENT_CONN_FAILED; |
878 e->event.failure = GG_FAILURE_RESOLVING; | |
879 sess->state = GG_STATE_IDLE; | |
880 | |
881 close(sess->fd); | |
882 | |
883 break; | |
884 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
885 |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
886 sess->server_ip = a.s_addr; |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
887 |
2393 | 888 close(sess->fd); |
889 | |
890 waitpid(sess->pid, NULL, 0); | |
891 | |
892 gg_debug(GG_DEBUG_MISC, "-- resolved, now connecting\n"); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
893 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
894 if (gg_http_use_proxy) { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
895 port = gg_http_proxy_port; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
896 } else { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
897 port = GG_APPMSG_PORT; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
898 }; |
2393 | 899 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
900 if ((sess->fd = gg_connect(&a, port, sess->async)) == -1) { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
901 struct in_addr *addr = (struct in_addr*) &sess->server_ip; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
902 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
903 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying direct connection\n"); |
2393 | 904 |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
905 if ((sess->fd = gg_connect(addr, GG_DEFAULT_PORT, sess->async)) == -1) { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
906 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
907 if ((sess->fd = gg_connect(&a, GG_HTTPS_PORT, sess->async)) == -1) { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
908 gg_debug(GG_DEBUG_MISC, "-- connect() failed. errno = %d (%s)\n", errno, strerror(errno)); |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
909 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
910 e->type = GG_EVENT_CONN_FAILED; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
911 e->event.failure = GG_FAILURE_CONNECTING; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
912 sess->state = GG_STATE_IDLE; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
913 break; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
914 } |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
915 } |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
916 sess->state = GG_STATE_CONNECTING_GG; |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
917 sess->check = GG_CHECK_WRITE; |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
918 } else { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
919 sess->state = GG_STATE_CONNECTING; |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
920 sess->check = GG_CHECK_WRITE; |
2393 | 921 } |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
922 |
2393 | 923 break; |
924 } | |
3630 | 925 #endif /* !_WIN32 */ |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
926 case GG_STATE_CONNECTING: |
2393 | 927 { |
928 char buf[1024]; | |
6063 | 929 unsigned int res, res_size = sizeof(res); |
2393 | 930 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
931 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING\n"); |
2393 | 932 |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
933 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, &res, &res_size) || res)) { |
3630 | 934 #if 0 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
935 struct in_addr *addr = (struct in_addr*) &sess->server_ip; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
936 gg_debug(GG_DEBUG_MISC, "-- http connection failed, errno = %d (%s), trying direct connection\n", res, strerror(res)); |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
937 if ((sess->fd = gg_connect(addr, GG_DEFAULT_PORT, sess->async)) == -1) { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
938 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
939 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) { |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
940 gg_debug(GG_DEBUG_MISC, "-- connect() failed. errno = %d (%s)\n", errno, strerror(errno)); |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
941 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
942 e->type = GG_EVENT_CONN_FAILED; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
943 e->event.failure = GG_FAILURE_CONNECTING; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
944 sess->state = GG_STATE_IDLE; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
945 break; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
946 } |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
947 } |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
948 |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
949 sess->state = GG_STATE_CONNECTING_GG; |
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
950 sess->check = GG_CHECK_WRITE; |
3630 | 951 #else |
952 gg_debug(GG_DEBUG_MISC, "-- http connection failed, errno = %d\n", res); | |
953 e->type = GG_EVENT_CONN_FAILED; | |
954 e->event.failure = GG_FAILURE_CONNECTING; | |
955 sess->state = GG_STATE_IDLE; | |
956 #endif | |
2393 | 957 break; |
958 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
959 |
2393 | 960 gg_debug(GG_DEBUG_MISC, "-- http connection succeded, sending query\n"); |
961 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
962 if (gg_http_use_proxy) { |
3630 | 963 g_snprintf(buf, sizeof(buf) - 1, |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
964 "GET http://" GG_APPMSG_HOST "/appsvc/appmsg.asp?fmnumber=%lu HTTP/1.0\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
965 "Host: " GG_APPMSG_HOST "\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
966 "User-Agent: " GG_HTTP_USERAGENT "\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
967 "Pragma: no-cache\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
968 "\r\n", sess->uin); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
969 } else { |
3630 | 970 g_snprintf(buf, sizeof(buf) - 1, |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
971 "GET /appsvc/appmsg.asp?fmnumber=%lu HTTP/1.0\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
972 "Host: " GG_APPMSG_HOST "\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
973 "User-Agent: " GG_HTTP_USERAGENT "\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
974 "Pragma: no-cache\r\n" |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
975 "\r\n", sess->uin); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
976 }; |
2393 | 977 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
978 gg_debug(GG_DEBUG_MISC, "=> -----BEGIN-HTTP-QUERY-----\n%s\n=> -----END-HTTP-QUERY-----\n", buf); |
2393 | 979 if (write(sess->fd, buf, strlen(buf)) < strlen(buf)) { |
980 gg_debug(GG_DEBUG_MISC, "-- sending query failed\n"); | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
981 errno = EIO; |
2393 | 982 e->type = GG_EVENT_CONN_FAILED; |
983 e->event.failure = GG_FAILURE_WRITING; | |
984 sess->state = GG_STATE_IDLE; | |
985 break; | |
986 } | |
987 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
988 sess->state = GG_STATE_READING_DATA; |
2393 | 989 sess->check = GG_CHECK_READ; |
990 | |
991 break; | |
992 } | |
993 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
994 case GG_STATE_READING_DATA: |
2393 | 995 { |
996 char buf[1024], *tmp, *host; | |
997 int port = GG_DEFAULT_PORT; | |
998 struct in_addr a; | |
999 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1000 gg_debug(GG_DEBUG_MISC, "== GG_STATE_READING_DATA\n"); |
2393 | 1001 |
1002 gg_read_line(sess->fd, buf, sizeof(buf) - 1); | |
1003 gg_chomp(buf); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1004 |
2393 | 1005 gg_debug(GG_DEBUG_TRAFFIC, "-- got http response (%s)\n", buf); |
1006 if (strncmp(buf, "HTTP/1.", 7) || strncmp(buf + 9, "200", 3)) { | |
1007 gg_debug(GG_DEBUG_MISC, "-- but that's not what we've expected\n"); | |
1008 | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1009 errno = EINVAL; |
2393 | 1010 e->type = GG_EVENT_CONN_FAILED; |
1011 e->event.failure = GG_FAILURE_INVALID; | |
1012 sess->state = GG_STATE_IDLE; | |
1013 break; | |
1014 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1015 |
2393 | 1016 while (strcmp(buf, "\r\n") && strcmp(buf, "")) |
1017 gg_read_line(sess->fd, buf, sizeof(buf) - 1); | |
1018 | |
1019 gg_read_line(sess->fd, buf, sizeof(buf) - 1); | |
1020 gg_chomp(buf); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1021 |
2393 | 1022 close(sess->fd); |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1023 |
2393 | 1024 gg_debug(GG_DEBUG_TRAFFIC, "-- received http data (%s)\n", buf); |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1025 |
2393 | 1026 tmp = buf; |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1027 |
2393 | 1028 while (*tmp && *tmp != ' ') |
1029 tmp++; | |
1030 while (*tmp && *tmp == ' ') | |
1031 tmp++; | |
1032 while (*tmp && *tmp != ' ') | |
1033 tmp++; | |
1034 while (*tmp && *tmp == ' ') | |
1035 tmp++; | |
1036 while (*tmp && *tmp != ' ') | |
1037 tmp++; | |
1038 while (*tmp && *tmp == ' ') | |
1039 tmp++; | |
1040 host = tmp; | |
1041 while (*tmp && *tmp != ' ') | |
1042 tmp++; | |
1043 *tmp = 0; | |
1044 | |
1045 if ((tmp = strchr(host, ':'))) { | |
1046 *tmp = 0; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1047 port = atoi(tmp+1); |
2393 | 1048 } |
1049 | |
1050 a.s_addr = inet_addr(host); | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1051 sess->server_ip = a.s_addr; |
3630 | 1052 #if 0 |
5681
46d7ad0dfa26
[gaim-migrate @ 6100]
Christian Hammond <chipx86@chipx86.com>
parents:
4298
diff
changeset
|
1053 /* We need to watch this non-blocking socket so lets use gaim_proxy_connect |
3630 | 1054 in gg.c - Herman */ |
1055 if((sess->fd = gg_connect(&a, port, sess->assync)) == -1) { | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1056 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1057 if ((sess->fd = gg_connect(&a, GG_HTTPS_PORT, sess->async)) == -1) { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1058 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno)); |
2393 | 1059 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1060 e->type = GG_EVENT_CONN_FAILED; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1061 e->event.failure = GG_FAILURE_CONNECTING; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1062 sess->state = GG_STATE_IDLE; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1063 break; |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1064 } |
2393 | 1065 } |
3630 | 1066 #else |
1067 sess->port = port; | |
1068 #endif | |
2393 | 1069 sess->state = GG_STATE_CONNECTING_GG; |
1070 sess->check = GG_CHECK_WRITE; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1071 |
2393 | 1072 break; |
1073 } | |
1074 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1075 case GG_STATE_CONNECTING_GG: |
2393 | 1076 { |
6063 | 1077 unsigned int res, res_size = sizeof(res); |
2393 | 1078 |
1079 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING_GG\n"); | |
1080 | |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
1081 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, &res, &res_size) || res)) { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1082 struct in_addr *addr = (struct in_addr*) &sess->server_ip; |
2393 | 1083 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1084 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1085 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1086 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno)); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1087 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1088 e->type = GG_EVENT_CONN_FAILED; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1089 e->event.failure = GG_FAILURE_CONNECTING; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1090 sess->state = GG_STATE_IDLE; |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1091 break; |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1092 } |
2393 | 1093 } |
1094 | |
1095 gg_debug(GG_DEBUG_MISC, "-- connected\n"); | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1096 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1097 sess->state = GG_STATE_READING_KEY; |
2393 | 1098 sess->check = GG_CHECK_READ; |
1099 | |
1100 break; | |
1101 } | |
1102 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1103 case GG_STATE_READING_KEY: |
2393 | 1104 { |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1105 struct gg_header *h; |
2393 | 1106 struct gg_welcome *w; |
1107 struct gg_login l; | |
1108 unsigned int hash; | |
1109 char *password = sess->password; | |
1110 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1111 gg_debug(GG_DEBUG_MISC, "== GG_STATE_READING_KEY\n"); |
2393 | 1112 |
1113 if (!(h = gg_recv_packet(sess))) { | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1114 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet() failed. errno = %d (%s)\n", errno, strerror(errno)); |
2393 | 1115 e->type = GG_EVENT_CONN_FAILED; |
1116 e->event.failure = GG_FAILURE_READING; | |
1117 sess->state = GG_STATE_IDLE; | |
1118 close(sess->fd); | |
1119 break; | |
1120 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1121 |
2393 | 1122 if (h->type != GG_WELCOME) { |
1123 gg_debug(GG_DEBUG_MISC, "-- invalid packet received\n"); | |
1124 | |
1125 free(h); | |
1126 close(sess->fd); | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1127 errno = EINVAL; |
2393 | 1128 e->type = GG_EVENT_CONN_FAILED; |
1129 e->event.failure = GG_FAILURE_INVALID; | |
1130 sess->state = GG_STATE_IDLE; | |
1131 break; | |
1132 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1133 |
6247 | 1134 w = (struct gg_welcome *)((void *)h + sizeof(struct gg_header)); |
2393 | 1135 w->key = fix32(w->key); |
1136 | |
1137 for (hash = 1; *password; password++) | |
1138 hash *= (*password) + 1; | |
1139 hash *= w->key; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1140 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1141 gg_debug(GG_DEBUG_DUMP, "%%%% klucz serwera %.4x, hash has³a %.8x\n", w->key, hash); |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1142 |
2393 | 1143 free(h); |
1144 | |
1145 free(sess->password); | |
1146 sess->password = NULL; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1147 |
2393 | 1148 l.uin = fix32(sess->uin); |
1149 l.hash = fix32(hash); | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1150 l.status = fix32(sess->initial_status ? sess->initial_status : GG_STATUS_AVAIL); |
2393 | 1151 l.dunno = fix32(0x0b); |
1152 l.local_ip = 0; | |
1153 l.local_port = 0; | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1154 |
2393 | 1155 gg_debug(GG_DEBUG_TRAFFIC, "-- sending GG_LOGIN packet\n"); |
1156 | |
1157 if (gg_send_packet(sess->fd, GG_LOGIN, &l, sizeof(l), NULL, 0) == -1) { | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1158 gg_debug(GG_DEBUG_TRAFFIC, "-- oops, failed. errno = %d (%s)\n", errno, strerror(errno)); |
2393 | 1159 |
1160 close(sess->fd); | |
1161 e->type = GG_EVENT_CONN_FAILED; | |
1162 e->event.failure = GG_FAILURE_WRITING; | |
1163 sess->state = GG_STATE_IDLE; | |
1164 break; | |
1165 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1166 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1167 sess->state = GG_STATE_READING_REPLY; |
2393 | 1168 |
1169 break; | |
1170 } | |
1171 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1172 case GG_STATE_READING_REPLY: |
2393 | 1173 { |
1174 struct gg_header *h; | |
1175 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1176 gg_debug(GG_DEBUG_MISC, "== GG_STATE_READING_REPLY\n"); |
2393 | 1177 |
1178 if (!(h = gg_recv_packet(sess))) { | |
1179 gg_debug(GG_DEBUG_MISC, "-- recv_packet failed\n"); | |
1180 e->type = GG_EVENT_CONN_FAILED; | |
1181 e->event.failure = GG_FAILURE_READING; | |
1182 sess->state = GG_STATE_IDLE; | |
1183 close(sess->fd); | |
1184 break; | |
1185 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1186 |
2393 | 1187 if (h->type == GG_LOGIN_OK) { |
1188 gg_debug(GG_DEBUG_MISC, "-- login succeded\n"); | |
1189 e->type = GG_EVENT_CONN_SUCCESS; | |
1190 sess->state = GG_STATE_CONNECTED; | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1191 free(h); |
2393 | 1192 break; |
1193 } | |
1194 | |
1195 if (h->type == GG_LOGIN_FAILED) { | |
1196 gg_debug(GG_DEBUG_MISC, "-- login failed\n"); | |
1197 e->event.failure = GG_FAILURE_PASSWORD; | |
1198 errno = EACCES; | |
1199 } else { | |
1200 gg_debug(GG_DEBUG_MISC, "-- invalid packet\n"); | |
1201 e->event.failure = GG_FAILURE_INVALID; | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1202 errno = EINVAL; |
2393 | 1203 } |
1204 | |
1205 e->type = GG_EVENT_CONN_FAILED; | |
1206 sess->state = GG_STATE_IDLE; | |
1207 close(sess->fd); | |
2792
9123abd0db92
[gaim-migrate @ 2805]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2595
diff
changeset
|
1208 free(h); |
2393 | 1209 |
1210 break; | |
1211 } | |
1212 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1213 case GG_STATE_CONNECTED: |
2393 | 1214 { |
1215 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTED\n"); | |
1216 | |
1217 if ((res = gg_watch_fd_connected(sess, e)) == -1) { | |
1218 | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1219 gg_debug(GG_DEBUG_MISC, "-- watch_fd_connected failed. errno = %d (%s)\n", errno, strerror(errno)); |
2393 | 1220 |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1221 if (errno == EAGAIN) { |
2393 | 1222 e->type = GG_EVENT_NONE; |
1223 res = 0; | |
1224 } else | |
1225 res = -1; | |
1226 } | |
1227 break; | |
1228 } | |
1229 } | |
1230 | |
1231 if (res == -1) { | |
1232 free(e); | |
1233 e = NULL; | |
1234 } | |
1235 | |
1236 return e; | |
1237 } | |
2846
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1238 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1239 /* |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1240 * Local variables: |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1241 * c-indentation-style: k&r |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1242 * c-basic-offset: 8 |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1243 * indent-tabs-mode: notnil |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1244 * End: |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1245 * |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1246 * vim: shiftwidth=8: |
4b3f17ca66bf
[gaim-migrate @ 2859]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2792
diff
changeset
|
1247 */ |