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