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