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