comparison src/protocols/gg/libgg.c @ 3630:9682c0e022c6

[gaim-migrate @ 3753] Yeah this will probably break a lot of shit knowing my luck. But hey, I really don't care what people thnk. committer: Tailor Script <tailor@pidgin.im>
author Rob Flynn <gaim@robflynn.com>
date Fri, 11 Oct 2002 03:14:01 +0000
parents 4b3f17ca66bf
children 988485669631
comparison
equal deleted inserted replaced
3629:afc5bb164c5a 3630:9682c0e022c6
1 /* $Id: libgg.c 2859 2001-12-05 09:48:56Z warmenhoven $ */ 1 /* $Id: libgg.c 3753 2002-10-11 03:14:01Z robflynn $ */
2 2
3 /* 3 /*
4 * (C) Copyright 2001 Wojtek Kaniewski <wojtekka@irc.pl>, 4 * (C) Copyright 2001 Wojtek Kaniewski <wojtekka@irc.pl>,
5 * Robert J. Woźny <speedy@ziew.org> 5 * Robert J. Woźny <speedy@ziew.org>
6 * 6 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software 17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */ 19 */
20 20
21 #include <stdio.h> 21 #ifndef _WIN32
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <sys/socket.h> 22 #include <sys/socket.h>
26 #include <netinet/in.h> 23 #include <netinet/in.h>
27 #include <arpa/inet.h> 24 #include <arpa/inet.h>
28 #include <sys/ioctl.h> 25 #include <sys/ioctl.h>
29 #include <sys/wait.h> 26 #include <sys/wait.h>
27 #include <netdb.h>
28 #include <pwd.h>
29 #else
30 #include <winsock.h>
31 #include <fcntl.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdio.h>
30 #include <sys/time.h> 38 #include <sys/time.h>
31 #include <netdb.h>
32 #include <errno.h> 39 #include <errno.h>
33 #ifndef _AIX 40 #ifndef _AIX
34 # include <string.h> 41 # include <string.h>
35 #endif 42 #endif
36 #include <stdarg.h> 43 #include <stdarg.h>
37 #include <pwd.h>
38 #include <time.h> 44 #include <time.h>
39 #ifdef sun 45 #ifdef sun
40 #include <sys/filio.h> 46 #include <sys/filio.h>
41 #endif 47 #endif
42 #include <glib.h> 48 #include <glib.h>
43 #if G_BYTE_ORDER == G_BIG_ENDIAN 49 #if G_BYTE_ORDER == G_BIG_ENDIAN
44 # define WORDS_BIGENDIAN 1 50 # define WORDS_BIGENDIAN 1
45 #endif 51 #endif
46 #include "libgg.h" 52 #include "libgg.h"
47 #include "config.h" 53 #include "config.h"
48 54 #include "gaim.h"
49 int gg_debug_level = 0; 55 #include "proxy.h"
56
57 int gg_debug_level = (GG_DEBUG_NET | GG_DEBUG_TRAFFIC | GG_DEBUG_DUMP | GG_DEBUG_FUNCTION | GG_DEBUG_MISC);
50 int gg_http_use_proxy = 0; 58 int gg_http_use_proxy = 0;
51 int gg_http_proxy_port = 0; 59 int gg_http_proxy_port = 0;
52 char *gg_http_proxy_host = NULL; 60 char *gg_http_proxy_host = NULL;
53 61
62 /* temp -Herman */
63 static int ping_outstanding = 0;
64
54 #ifndef lint 65 #ifndef lint
55 66
56 static char rcsid[] 67 static char rcsid[]
57 #ifdef __GNUC__ 68 #ifdef __GNUC__
58 __attribute__ ((unused)) 69 __attribute__ ((unused))
59 #endif 70 #endif
60 = "$Id: libgg.c 2859 2001-12-05 09:48:56Z warmenhoven $"; 71 = "$Id: libgg.c 3753 2002-10-11 03:14:01Z robflynn $";
61 72
62 #endif 73 #endif
63 74
64 /* 75 /*
65 * fix32() // funkcja wewnętrzna 76 * fix32() // funkcja wewnętrzna
93 (((x & (unsigned short) 0x00ffU) << 8) | 104 (((x & (unsigned short) 0x00ffU) << 8) |
94 ((x & (unsigned short) 0xff00U) >> 8)); 105 ((x & (unsigned short) 0xff00U) >> 8));
95 #endif 106 #endif
96 } 107 }
97 108
109 #ifndef _WIN32
98 /* 110 /*
99 * gg_resolve() // funkcja wewnętrzna 111 * gg_resolve() // funkcja wewnętrzna
100 * 112 *
101 * tworzy pipe'y, forkuje się i w drugim procesie zaczyna resolvować 113 * tworzy pipe'y, forkuje się i w drugim procesie zaczyna resolvować
102 * podanego hosta. zapisuje w sesji deskryptor pipe'u. jeśli coś tam 114 * podanego hosta. zapisuje w sesji deskryptor pipe'u. jeśli coś tam
147 *fd = pipes[0]; 159 *fd = pipes[0];
148 *pid = res; 160 *pid = res;
149 161
150 return 0; 162 return 0;
151 } 163 }
164 #endif /*!_WIN32*/
152 165
153 /* 166 /*
154 * gg_recv_packet() // funkcja wewnętrzna 167 * gg_recv_packet() // funkcja wewnętrzna
155 * 168 *
156 * odbiera jeden pakiet gg i zwraca wskaźnik do niego. pamięć po nim 169 * odbiera jeden pakiet gg i zwraca wskaźnik do niego. pamięć po nim
163 static void *gg_recv_packet(struct gg_session *sess) 176 static void *gg_recv_packet(struct gg_session *sess)
164 { 177 {
165 struct gg_header h; 178 struct gg_header h;
166 char *buf = NULL; 179 char *buf = NULL;
167 int ret = 0, offset, size = 0; 180 int ret = 0, offset, size = 0;
181 int sizeh = sizeof(struct gg_header);
168 182
169 gg_debug(GG_DEBUG_FUNCTION, "** gg_recv_packet(...);\n"); 183 gg_debug(GG_DEBUG_FUNCTION, "** gg_recv_packet(...);\n");
170 184
171 if (!sess) { 185 if (!sess) {
172 errno = EFAULT; 186 errno = EFAULT;
173 return NULL; 187 return NULL;
174 } 188 }
175 189
176 if (sess->recv_left < 1) { 190 if (sess->recv_left < 1) {
177 while (ret != sizeof(h)) { 191 while (ret != sizeh) {
178 ret = read(sess->fd, &h, sizeof(h)); 192 #ifndef _WIN32
179 gg_debug(GG_DEBUG_MISC, "-- header recv(..., %d) = %d\n", sizeof(h), ret); 193 ret = read(sess->fd, &h, sizeh);
180 if (ret < sizeof(h)) { 194 gg_debug(GG_DEBUG_MISC, "-- header recv(..., %d) = %d\n", sizeh, ret);
195 if (ret < sizeh) {
181 if (errno != EINTR) { 196 if (errno != EINTR) {
182 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno)); 197 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno));
183 return NULL; 198 return NULL;
184 } 199 }
185 } 200 }
201 #else
202 ret = recv(sess->fd, (char*)&h, sizeh, 0);
203 gg_debug(GG_DEBUG_MISC, "-- header recv(..., %d) = %d\n", sizeh, ret);
204 if (ret < sizeh) {
205 /* connection has been gracefully closed */
206 if (ret == 0) {
207 gg_debug(GG_DEBUG_MISC, "Connection has been gracefully closed\n");
208 WSASetLastError(WSAEDISCON);
209 return NULL;
210 }
211 else if (ret == SOCKET_ERROR) {
212 if(WSAGetLastError() != WSAEINTR) {
213 gg_debug(GG_DEBUG_MISC, "-- socket error = %d\n", WSAGetLastError());
214 return NULL;
215 }
216 }
217
218 }
219 #endif
186 } 220 }
187 221
188 h.type = fix32(h.type); 222 h.type = fix32(h.type);
189 h.length = fix32(h.length); 223 h.length = fix32(h.length);
190 } else { 224 } else {
191 memcpy(&h, sess->recv_buf, sizeof(h)); 225 memcpy(&h, sess->recv_buf, sizeh);
192 } 226 }
193 227
194 /* jakieś sensowne limity na rozmiar pakietu */ 228 /* jakieś sensowne limity na rozmiar pakietu */
195 if (h.length < 0 || h.length > 65535) { 229 if (h.length < 0 || h.length > 65535) {
196 gg_debug(GG_DEBUG_MISC, "-- invalid packet length (%d)\n", h.length); 230 gg_debug(GG_DEBUG_MISC, "-- invalid packet length (%d)\n", h.length);
202 gg_debug(GG_DEBUG_MISC, "-- resuming last gg_recv_packet()\n"); 236 gg_debug(GG_DEBUG_MISC, "-- resuming last gg_recv_packet()\n");
203 size = sess->recv_left; 237 size = sess->recv_left;
204 offset = sess->recv_done; 238 offset = sess->recv_done;
205 buf = sess->recv_buf; 239 buf = sess->recv_buf;
206 } else { 240 } else {
207 if (!(buf = malloc(sizeof(h) + h.length + 1))) { 241 if (!(buf = malloc(sizeh + h.length + 1))) {
208 gg_debug(GG_DEBUG_MISC, "-- not enough memory\n"); 242 gg_debug(GG_DEBUG_MISC, "-- not enough memory\n");
209 return NULL; 243 return NULL;
210 } 244 }
211 245
212 memcpy(buf, &h, sizeof(h)); 246 memcpy(buf, &h, sizeh);
213 247
214 offset = 0; 248 offset = 0;
215 size = h.length; 249 size = h.length;
216 } 250 }
217 251
218 while (size > 0) { 252 while (size > 0) {
219 ret = read(sess->fd, buf + sizeof(h) + offset, size); 253 #ifndef _WIN32
254 ret = read(sess->fd, buf + sizeh + offset, size);
255 #else
256 ret = recv(sess->fd, buf + sizeh + offset, size, 0);
257 #endif
220 gg_debug(GG_DEBUG_MISC, "-- body recv(..., %d) = %d\n", size, ret); 258 gg_debug(GG_DEBUG_MISC, "-- body recv(..., %d) = %d\n", size, ret);
221 if (ret > -1 && ret <= size) { 259 if (ret > -1 && ret <= size) {
222 offset += ret; 260 offset += ret;
223 size -= ret; 261 size -= ret;
224 } else if (ret == -1) { 262 } else if (ret == -1) {
263 #ifndef _WIN32
225 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno)); 264 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno));
226 if (errno == EAGAIN) { 265 if (errno == EAGAIN) {
227 gg_debug(GG_DEBUG_MISC, "-- %d bytes received, %d left\n", offset, size); 266 gg_debug(GG_DEBUG_MISC, "-- %d bytes received, %d left\n", offset, size);
228 sess->recv_buf = buf; 267 sess->recv_buf = buf;
229 sess->recv_left = size; 268 sess->recv_left = size;
233 if (errno != EINTR) { 272 if (errno != EINTR) {
234 // errno = EINVAL; 273 // errno = EINVAL;
235 free(buf); 274 free(buf);
236 return NULL; 275 return NULL;
237 } 276 }
277 #else
278 gg_debug(GG_DEBUG_MISC, "-- errno = %d\n", WSAGetLastError());
279 if (WSAGetLastError()!= WSAEINTR) {
280 free(buf);
281 return NULL;
282 }
283 #endif
238 } 284 }
239 } 285 }
240 286
241 sess->recv_left = 0; 287 sess->recv_left = 0;
242 288
243 if ((gg_debug_level & GG_DEBUG_DUMP)) { 289 if ((gg_debug_level & GG_DEBUG_DUMP)) {
244 int i; 290 int i;
245 291
246 gg_debug(GG_DEBUG_DUMP, ">> received packet (type=%.2x):", h.type); 292 gg_debug(GG_DEBUG_DUMP, ">> received packet (type=%.2x):", h.type);
247 for (i = 0; i < sizeof(h) + h.length; i++) 293 for (i = 0; i < sizeh + h.length; i++)
248 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) buf[i]); 294 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) buf[i]);
249 gg_debug(GG_DEBUG_DUMP, "\n"); 295 gg_debug(GG_DEBUG_DUMP, "\n");
250 } 296 }
251 297
252 return buf; 298 return buf;
304 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) tmp[i]); 350 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) tmp[i]);
305 gg_debug(GG_DEBUG_DUMP, "\n"); 351 gg_debug(GG_DEBUG_DUMP, "\n");
306 } 352 }
307 353
308 plen = sizeof(struct gg_header) + length + payload_length; 354 plen = sizeof(struct gg_header) + length + payload_length;
309 355 #ifndef _WIN32
310 if ((res = write(sock, tmp, plen)) < plen) { 356 if ((res = write(sock, tmp, plen)) < plen) {
311 gg_debug(GG_DEBUG_MISC, "-- write() failed. res = %d, errno = %d (%s)\n", res, errno, strerror(errno)); 357 gg_debug(GG_DEBUG_MISC, "-- write() failed. res = %d, errno = %d (%s)\n", res, errno, strerror(errno));
358 #else
359 if ((res = send(sock, tmp, plen, 0)) < plen) {
360 gg_debug(GG_DEBUG_MISC, "-- send() failed. res = %d, errno = %d\n",
361 res, (res == SOCKET_ERROR) ? WSAGetLastError() : 0);
362 #endif
312 free(tmp); 363 free(tmp);
313 return -1; 364 return -1;
314 } 365 }
315 366
316 free(tmp); 367 free(tmp);
317 return 0; 368 return 0;
318 } 369 }
319 370
320 371 #ifndef _WIN32
321 /* 372 /*
322 * gg_login() 373 * gg_login()
323 * 374 *
324 * rozpoczyna procedurę łączenia się z serwerem. resztę obsłguje się przez 375 * rozpoczyna procedurę łączenia się z serwerem. resztę obsłguje się przez
325 * gg_watch_event. 376 * gg_watch_event.
417 } 468 }
418 } 469 }
419 470
420 return sess; 471 return sess;
421 } 472 }
473 #endif /*!_WIN32*/
422 474
423 /* 475 /*
424 * gg_free_session() 476 * gg_free_session()
425 * 477 *
426 * zwalnia pamięć zajmowaną przez opis sesji. 478 * zwalnia pamięć zajmowaną przez opis sesji.
456 errno = EFAULT; 508 errno = EFAULT;
457 return -1; 509 return -1;
458 } 510 }
459 511
460 if (sess->state != GG_STATE_CONNECTED) { 512 if (sess->state != GG_STATE_CONNECTED) {
513 #ifndef _WIN32
461 errno = ENOTCONN; 514 errno = ENOTCONN;
515 #else
516 WSASetLastError( WSAENOTCONN );
517 #endif
462 return -1; 518 return -1;
463 } 519 }
464 520
465 gg_debug(GG_DEBUG_FUNCTION, "** gg_change_status(..., %d);\n", status); 521 gg_debug(GG_DEBUG_FUNCTION, "** gg_change_status(..., %d);\n", status);
466 522
515 errno = EFAULT; 571 errno = EFAULT;
516 return -1; 572 return -1;
517 } 573 }
518 574
519 if (sess->state != GG_STATE_CONNECTED) { 575 if (sess->state != GG_STATE_CONNECTED) {
576 #ifndef _WIN32
520 errno = ENOTCONN; 577 errno = ENOTCONN;
578 #else
579 WSASetLastError( WSAENOTCONN );
580 #endif
521 return -1; 581 return -1;
522 } 582 }
523 583
524 gg_debug(GG_DEBUG_FUNCTION, "** gg_send_message(..., %d, %u, \"...\");\n", msgclass, recipient); 584 gg_debug(GG_DEBUG_FUNCTION, "** gg_send_message(..., %d, %u, \"...\");\n", msgclass, recipient);
525 585
551 errno = EFAULT; 611 errno = EFAULT;
552 return -1; 612 return -1;
553 } 613 }
554 614
555 if (sess->state != GG_STATE_CONNECTED) { 615 if (sess->state != GG_STATE_CONNECTED) {
616 #ifndef _WIN32
556 errno = ENOTCONN; 617 errno = ENOTCONN;
618 #else
619 WSASetLastError( WSAENOTCONN );
620 #endif
557 return -1; 621 return -1;
558 } 622 }
559 623
560 gg_debug(GG_DEBUG_FUNCTION, "** gg_ping(...);\n"); 624 gg_debug(GG_DEBUG_FUNCTION, "** gg_ping(...);\n");
561 625
562 return gg_send_packet(sess->fd, GG_PING, NULL, 0, NULL, 0); 626 if(ping_outstanding) {
627 debug_printf("Trying to send ping, when we havn't been ponged on last ping\n");
628 return 1;
629 }
630 else {
631 ping_outstanding = 1;
632 return gg_send_packet(sess->fd, GG_PING, NULL, 0, NULL, 0);
633 }
563 } 634 }
564 635
565 /* 636 /*
566 * gg_free_event() 637 * gg_free_event()
567 * 638 *
603 errno = EFAULT; 674 errno = EFAULT;
604 return -1; 675 return -1;
605 } 676 }
606 677
607 if (sess->state != GG_STATE_CONNECTED) { 678 if (sess->state != GG_STATE_CONNECTED) {
679 #ifndef _WIN32
608 errno = ENOTCONN; 680 errno = ENOTCONN;
681 #else
682 WSASetLastError( WSAENOTCONN );
683 #endif
609 return -1; 684 return -1;
610 } 685 }
611 686
612 gg_debug(GG_DEBUG_FUNCTION, "** gg_notify(..., %d);\n", count); 687 gg_debug(GG_DEBUG_FUNCTION, "** gg_notify(..., %d);\n", count);
613 688
648 errno = EFAULT; 723 errno = EFAULT;
649 return -1; 724 return -1;
650 } 725 }
651 726
652 if (sess->state != GG_STATE_CONNECTED) { 727 if (sess->state != GG_STATE_CONNECTED) {
728 #ifndef _WIN32
653 errno = ENOTCONN; 729 errno = ENOTCONN;
730 #else
731 WSASetLastError( WSAENOTCONN );
732 #endif
654 return -1; 733 return -1;
655 } 734 }
656 735
657 gg_debug(GG_DEBUG_FUNCTION, "** gg_add_notify(..., %u);\n", uin); 736 gg_debug(GG_DEBUG_FUNCTION, "** gg_add_notify(..., %u);\n", uin);
658 737
680 errno = EFAULT; 759 errno = EFAULT;
681 return -1; 760 return -1;
682 } 761 }
683 762
684 if (sess->state != GG_STATE_CONNECTED) { 763 if (sess->state != GG_STATE_CONNECTED) {
764 #ifndef _WIN32
685 errno = ENOTCONN; 765 errno = ENOTCONN;
766 #else
767 WSASetLastError( WSAENOTCONN );
768 #endif
686 return -1; 769 return -1;
687 } 770 }
688 771
689 gg_debug(GG_DEBUG_FUNCTION, "** gg_remove_notify(..., %u);\n", uin); 772 gg_debug(GG_DEBUG_FUNCTION, "** gg_remove_notify(..., %u);\n", uin);
690 773
783 } 866 }
784 } 867 }
785 868
786 if (h->type == GG_PONG) { 869 if (h->type == GG_PONG) {
787 gg_debug(GG_DEBUG_MISC, "-- received a pong\n"); 870 gg_debug(GG_DEBUG_MISC, "-- received a pong\n");
788 871 ping_outstanding = 0;
789 sess->last_pong = time(NULL); 872 sess->last_pong = time(NULL);
790 } 873 }
791 874
792 free(h); 875 free(h);
793 876
810 */ 893 */
811 struct gg_event *gg_watch_fd(struct gg_session *sess) 894 struct gg_event *gg_watch_fd(struct gg_session *sess)
812 { 895 {
813 struct gg_event *e; 896 struct gg_event *e;
814 int res = 0; 897 int res = 0;
898 #ifndef _WIN32
815 int port; 899 int port;
900 #endif
816 901
817 if (!sess) { 902 if (!sess) {
818 errno = EFAULT; 903 errno = EFAULT;
819 return NULL; 904 return NULL;
820 } 905 }
827 } 912 }
828 913
829 e->type = GG_EVENT_NONE; 914 e->type = GG_EVENT_NONE;
830 915
831 switch (sess->state) { 916 switch (sess->state) {
917 #ifndef _WIN32
918 /* Apparantly we will never be in this state as long as we are
919 using proxy_connect instead of gg_login - Herman */
832 case GG_STATE_RESOLVING: 920 case GG_STATE_RESOLVING:
833 { 921 {
834 struct in_addr a; 922 struct in_addr a;
835 923
836 gg_debug(GG_DEBUG_MISC, "== GG_STATE_RESOLVING\n"); 924 gg_debug(GG_DEBUG_MISC, "== GG_STATE_RESOLVING\n");
885 sess->check = GG_CHECK_WRITE; 973 sess->check = GG_CHECK_WRITE;
886 } 974 }
887 975
888 break; 976 break;
889 } 977 }
890 978 #endif /* !_WIN32 */
891 case GG_STATE_CONNECTING: 979 case GG_STATE_CONNECTING:
892 { 980 {
893 char buf[1024]; 981 char buf[1024];
894 int res, res_size = sizeof(res); 982 int res, res_size = sizeof(res);
895 983
896 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING\n"); 984 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING\n");
897 985
898 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, &res, &res_size) || res)) { 986 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR,
987 #ifndef _WIN32
988 &res,
989 #else
990 (char*)&res,
991 #endif
992 &res_size) || res)) {
993 #if 0
899 struct in_addr *addr = (struct in_addr*) &sess->server_ip; 994 struct in_addr *addr = (struct in_addr*) &sess->server_ip;
900 gg_debug(GG_DEBUG_MISC, "-- http connection failed, errno = %d (%s), trying direct connection\n", res, strerror(res)); 995 gg_debug(GG_DEBUG_MISC, "-- http connection failed, errno = %d (%s), trying direct connection\n", res, strerror(res));
901
902 if ((sess->fd = gg_connect(addr, GG_DEFAULT_PORT, sess->async)) == -1) { 996 if ((sess->fd = gg_connect(addr, GG_DEFAULT_PORT, sess->async)) == -1) {
903 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); 997 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n");
904 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) { 998 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) {
905 gg_debug(GG_DEBUG_MISC, "-- connect() failed. errno = %d (%s)\n", errno, strerror(errno)); 999 gg_debug(GG_DEBUG_MISC, "-- connect() failed. errno = %d (%s)\n", errno, strerror(errno));
906 1000
911 } 1005 }
912 } 1006 }
913 1007
914 sess->state = GG_STATE_CONNECTING_GG; 1008 sess->state = GG_STATE_CONNECTING_GG;
915 sess->check = GG_CHECK_WRITE; 1009 sess->check = GG_CHECK_WRITE;
1010 #else
1011 gg_debug(GG_DEBUG_MISC, "-- http connection failed, errno = %d\n", res);
1012 e->type = GG_EVENT_CONN_FAILED;
1013 e->event.failure = GG_FAILURE_CONNECTING;
1014 sess->state = GG_STATE_IDLE;
1015 #endif
916 break; 1016 break;
917 } 1017 }
918 1018
919 gg_debug(GG_DEBUG_MISC, "-- http connection succeded, sending query\n"); 1019 gg_debug(GG_DEBUG_MISC, "-- http connection succeded, sending query\n");
920 1020
921 if (gg_http_use_proxy) { 1021 if (gg_http_use_proxy) {
922 snprintf(buf, sizeof(buf) - 1, 1022 g_snprintf(buf, sizeof(buf) - 1,
923 "GET http://" GG_APPMSG_HOST "/appsvc/appmsg.asp?fmnumber=%lu HTTP/1.0\r\n" 1023 "GET http://" GG_APPMSG_HOST "/appsvc/appmsg.asp?fmnumber=%lu HTTP/1.0\r\n"
924 "Host: " GG_APPMSG_HOST "\r\n" 1024 "Host: " GG_APPMSG_HOST "\r\n"
925 "User-Agent: " GG_HTTP_USERAGENT "\r\n" 1025 "User-Agent: " GG_HTTP_USERAGENT "\r\n"
926 "Pragma: no-cache\r\n" 1026 "Pragma: no-cache\r\n"
927 "\r\n", sess->uin); 1027 "\r\n", sess->uin);
928 } else { 1028 } else {
929 snprintf(buf, sizeof(buf) - 1, 1029 g_snprintf(buf, sizeof(buf) - 1,
930 "GET /appsvc/appmsg.asp?fmnumber=%lu HTTP/1.0\r\n" 1030 "GET /appsvc/appmsg.asp?fmnumber=%lu HTTP/1.0\r\n"
931 "Host: " GG_APPMSG_HOST "\r\n" 1031 "Host: " GG_APPMSG_HOST "\r\n"
932 "User-Agent: " GG_HTTP_USERAGENT "\r\n" 1032 "User-Agent: " GG_HTTP_USERAGENT "\r\n"
933 "Pragma: no-cache\r\n" 1033 "Pragma: no-cache\r\n"
934 "\r\n", sess->uin); 1034 "\r\n", sess->uin);
935 }; 1035 };
936 1036
937 gg_debug(GG_DEBUG_MISC, "=> -----BEGIN-HTTP-QUERY-----\n%s\n=> -----END-HTTP-QUERY-----\n", buf); 1037 gg_debug(GG_DEBUG_MISC, "=> -----BEGIN-HTTP-QUERY-----\n%s\n=> -----END-HTTP-QUERY-----\n", buf);
938 1038 #ifndef _WIN32
939 if (write(sess->fd, buf, strlen(buf)) < strlen(buf)) { 1039 if (write(sess->fd, buf, strlen(buf)) < strlen(buf)) {
1040 #else
1041 if (send(sess->fd, buf, strlen(buf), 0) < strlen(buf)) {
1042 #endif
940 gg_debug(GG_DEBUG_MISC, "-- sending query failed\n"); 1043 gg_debug(GG_DEBUG_MISC, "-- sending query failed\n");
941
942 errno = EIO; 1044 errno = EIO;
943 e->type = GG_EVENT_CONN_FAILED; 1045 e->type = GG_EVENT_CONN_FAILED;
944 e->event.failure = GG_FAILURE_WRITING; 1046 e->event.failure = GG_FAILURE_WRITING;
945 sess->state = GG_STATE_IDLE; 1047 sess->state = GG_STATE_IDLE;
946 break; 1048 break;
1008 port = atoi(tmp+1); 1110 port = atoi(tmp+1);
1009 } 1111 }
1010 1112
1011 a.s_addr = inet_addr(host); 1113 a.s_addr = inet_addr(host);
1012 sess->server_ip = a.s_addr; 1114 sess->server_ip = a.s_addr;
1013 1115 #if 0
1014 if ((sess->fd = gg_connect(&a, port, sess->async)) == -1) { 1116 /* We need to watch this non-blocking socket so lets use proxy_connect
1117 in gg.c - Herman */
1118 if((sess->fd = gg_connect(&a, port, sess->assync)) == -1) {
1015 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); 1119 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n");
1016 if ((sess->fd = gg_connect(&a, GG_HTTPS_PORT, sess->async)) == -1) { 1120 if ((sess->fd = gg_connect(&a, GG_HTTPS_PORT, sess->async)) == -1) {
1017 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno)); 1121 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno));
1018 1122
1019 e->type = GG_EVENT_CONN_FAILED; 1123 e->type = GG_EVENT_CONN_FAILED;
1020 e->event.failure = GG_FAILURE_CONNECTING; 1124 e->event.failure = GG_FAILURE_CONNECTING;
1021 sess->state = GG_STATE_IDLE; 1125 sess->state = GG_STATE_IDLE;
1022 break; 1126 break;
1023 } 1127 }
1024 } 1128 }
1025 1129 #else
1130 sess->port = port;
1131 #endif
1026 sess->state = GG_STATE_CONNECTING_GG; 1132 sess->state = GG_STATE_CONNECTING_GG;
1027 sess->check = GG_CHECK_WRITE; 1133 sess->check = GG_CHECK_WRITE;
1028 1134
1029 break; 1135 break;
1030 } 1136 }
1033 { 1139 {
1034 int res, res_size = sizeof(res); 1140 int res, res_size = sizeof(res);
1035 1141
1036 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING_GG\n"); 1142 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING_GG\n");
1037 1143
1038 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, &res, &res_size) || res)) { 1144 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR,
1145 #ifndef _WIN32
1146 &res,
1147 #else
1148 (char*)&res,
1149 #endif
1150 &res_size) || res)) {
1039 struct in_addr *addr = (struct in_addr*) &sess->server_ip; 1151 struct in_addr *addr = (struct in_addr*) &sess->server_ip;
1040 1152
1041 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); 1153 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n");
1042 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) { 1154 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) {
1043 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno)); 1155 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno));
1066 char *password = sess->password; 1178 char *password = sess->password;
1067 1179
1068 gg_debug(GG_DEBUG_MISC, "== GG_STATE_READING_KEY\n"); 1180 gg_debug(GG_DEBUG_MISC, "== GG_STATE_READING_KEY\n");
1069 1181
1070 if (!(h = gg_recv_packet(sess))) { 1182 if (!(h = gg_recv_packet(sess))) {
1183 #ifndef _WIN32
1071 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet() failed. errno = %d (%s)\n", errno, strerror(errno)); 1184 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet() failed. errno = %d (%s)\n", errno, strerror(errno));
1072 1185 #else
1186 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet() failed. errno = %d\n", WSAGetLastError());
1187 #endif
1073 e->type = GG_EVENT_CONN_FAILED; 1188 e->type = GG_EVENT_CONN_FAILED;
1074 e->event.failure = GG_FAILURE_READING; 1189 e->event.failure = GG_FAILURE_READING;
1075 sess->state = GG_STATE_IDLE; 1190 sess->state = GG_STATE_IDLE;
1076 close(sess->fd); 1191 close(sess->fd);
1077 break; 1192 break;