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