comparison src/protocols/gg/libgg.c @ 3717:988485669631

[gaim-migrate @ 3850] Warning fixes and WIN32 ifdef removals committer: Tailor Script <tailor@pidgin.im>
author Herman Bloggs <hermanator12002@yahoo.com>
date Wed, 16 Oct 2002 19:57:03 +0000
parents 9682c0e022c6
children f9b4b1c7d413
comparison
equal deleted inserted replaced
3716:d7e83b4db191 3717:988485669631
1 /* $Id: libgg.c 3753 2002-10-11 03:14:01Z robflynn $ */ 1 /* $Id: libgg.c 3850 2002-10-16 19:57:03Z hermanator $ */
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 *
25 #include <sys/ioctl.h> 25 #include <sys/ioctl.h>
26 #include <sys/wait.h> 26 #include <sys/wait.h>
27 #include <netdb.h> 27 #include <netdb.h>
28 #include <pwd.h> 28 #include <pwd.h>
29 #else 29 #else
30 #include <winsock.h>
31 #include <fcntl.h> 30 #include <fcntl.h>
32 #endif 31 #endif
33 32
34 #include <stdio.h> 33 #include <stdio.h>
35 #include <stdlib.h> 34 #include <stdlib.h>
52 #include "libgg.h" 51 #include "libgg.h"
53 #include "config.h" 52 #include "config.h"
54 #include "gaim.h" 53 #include "gaim.h"
55 #include "proxy.h" 54 #include "proxy.h"
56 55
56 #ifdef _WIN32
57 #include "win32dep.h"
58 #endif
59
57 int gg_debug_level = (GG_DEBUG_NET | GG_DEBUG_TRAFFIC | GG_DEBUG_DUMP | GG_DEBUG_FUNCTION | GG_DEBUG_MISC); 60 int gg_debug_level = (GG_DEBUG_NET | GG_DEBUG_TRAFFIC | GG_DEBUG_DUMP | GG_DEBUG_FUNCTION | GG_DEBUG_MISC);
58 int gg_http_use_proxy = 0; 61 int gg_http_use_proxy = 0;
59 int gg_http_proxy_port = 0; 62 int gg_http_proxy_port = 0;
60 char *gg_http_proxy_host = NULL; 63 char *gg_http_proxy_host = NULL;
61 64
66 69
67 static char rcsid[] 70 static char rcsid[]
68 #ifdef __GNUC__ 71 #ifdef __GNUC__
69 __attribute__ ((unused)) 72 __attribute__ ((unused))
70 #endif 73 #endif
71 = "$Id: libgg.c 3753 2002-10-11 03:14:01Z robflynn $"; 74 = "$Id: libgg.c 3850 2002-10-16 19:57:03Z hermanator $";
72 75
73 #endif 76 #endif
74 77
75 /* 78 /*
76 * fix32() // funkcja wewnętrzna 79 * fix32() // funkcja wewnętrzna
187 return NULL; 190 return NULL;
188 } 191 }
189 192
190 if (sess->recv_left < 1) { 193 if (sess->recv_left < 1) {
191 while (ret != sizeh) { 194 while (ret != sizeh) {
192 #ifndef _WIN32
193 ret = read(sess->fd, &h, sizeh); 195 ret = read(sess->fd, &h, sizeh);
194 gg_debug(GG_DEBUG_MISC, "-- header recv(..., %d) = %d\n", sizeh, ret); 196 gg_debug(GG_DEBUG_MISC, "-- header recv(..., %d) = %d\n", sizeh, ret);
195 if (ret < sizeh) { 197 if (ret < sizeh) {
196 if (errno != EINTR) { 198 if (errno != EINTR) {
197 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno)); 199 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno));
198 return NULL; 200 return NULL;
199 } 201 }
200 } 202 }
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
220 } 203 }
221 204
222 h.type = fix32(h.type); 205 h.type = fix32(h.type);
223 h.length = fix32(h.length); 206 h.length = fix32(h.length);
224 } else { 207 } else {
248 offset = 0; 231 offset = 0;
249 size = h.length; 232 size = h.length;
250 } 233 }
251 234
252 while (size > 0) { 235 while (size > 0) {
253 #ifndef _WIN32
254 ret = read(sess->fd, buf + sizeh + offset, size); 236 ret = read(sess->fd, buf + sizeh + offset, size);
255 #else
256 ret = recv(sess->fd, buf + sizeh + offset, size, 0);
257 #endif
258 gg_debug(GG_DEBUG_MISC, "-- body recv(..., %d) = %d\n", size, ret); 237 gg_debug(GG_DEBUG_MISC, "-- body recv(..., %d) = %d\n", size, ret);
259 if (ret > -1 && ret <= size) { 238 if (ret > -1 && ret <= size) {
260 offset += ret; 239 offset += ret;
261 size -= ret; 240 size -= ret;
262 } else if (ret == -1) { 241 } else if (ret == -1) {
263 #ifndef _WIN32
264 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno)); 242 gg_debug(GG_DEBUG_MISC, "-- errno = %d (%s)\n", errno, strerror(errno));
265 if (errno == EAGAIN) { 243 if (errno == EAGAIN) {
266 gg_debug(GG_DEBUG_MISC, "-- %d bytes received, %d left\n", offset, size); 244 gg_debug(GG_DEBUG_MISC, "-- %d bytes received, %d left\n", offset, size);
267 sess->recv_buf = buf; 245 sess->recv_buf = buf;
268 sess->recv_left = size; 246 sess->recv_left = size;
272 if (errno != EINTR) { 250 if (errno != EINTR) {
273 // errno = EINVAL; 251 // errno = EINVAL;
274 free(buf); 252 free(buf);
275 return NULL; 253 return NULL;
276 } 254 }
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
284 } 255 }
285 } 256 }
286 257
287 sess->recv_left = 0; 258 sess->recv_left = 0;
288 259
350 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) tmp[i]); 321 gg_debug(GG_DEBUG_DUMP, " %.2x", (unsigned char) tmp[i]);
351 gg_debug(GG_DEBUG_DUMP, "\n"); 322 gg_debug(GG_DEBUG_DUMP, "\n");
352 } 323 }
353 324
354 plen = sizeof(struct gg_header) + length + payload_length; 325 plen = sizeof(struct gg_header) + length + payload_length;
355 #ifndef _WIN32
356 if ((res = write(sock, tmp, plen)) < plen) { 326 if ((res = write(sock, tmp, plen)) < plen) {
357 gg_debug(GG_DEBUG_MISC, "-- write() failed. res = %d, errno = %d (%s)\n", res, errno, strerror(errno)); 327 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
363 free(tmp); 328 free(tmp);
364 return -1; 329 return -1;
365 } 330 }
366 331
367 free(tmp); 332 free(tmp);
508 errno = EFAULT; 473 errno = EFAULT;
509 return -1; 474 return -1;
510 } 475 }
511 476
512 if (sess->state != GG_STATE_CONNECTED) { 477 if (sess->state != GG_STATE_CONNECTED) {
513 #ifndef _WIN32
514 errno = ENOTCONN; 478 errno = ENOTCONN;
515 #else
516 WSASetLastError( WSAENOTCONN );
517 #endif
518 return -1; 479 return -1;
519 } 480 }
520 481
521 gg_debug(GG_DEBUG_FUNCTION, "** gg_change_status(..., %d);\n", status); 482 gg_debug(GG_DEBUG_FUNCTION, "** gg_change_status(..., %d);\n", status);
522 483
571 errno = EFAULT; 532 errno = EFAULT;
572 return -1; 533 return -1;
573 } 534 }
574 535
575 if (sess->state != GG_STATE_CONNECTED) { 536 if (sess->state != GG_STATE_CONNECTED) {
576 #ifndef _WIN32
577 errno = ENOTCONN; 537 errno = ENOTCONN;
578 #else
579 WSASetLastError( WSAENOTCONN );
580 #endif
581 return -1; 538 return -1;
582 } 539 }
583 540
584 gg_debug(GG_DEBUG_FUNCTION, "** gg_send_message(..., %d, %u, \"...\");\n", msgclass, recipient); 541 gg_debug(GG_DEBUG_FUNCTION, "** gg_send_message(..., %d, %u, \"...\");\n", msgclass, recipient);
585 542
611 errno = EFAULT; 568 errno = EFAULT;
612 return -1; 569 return -1;
613 } 570 }
614 571
615 if (sess->state != GG_STATE_CONNECTED) { 572 if (sess->state != GG_STATE_CONNECTED) {
616 #ifndef _WIN32
617 errno = ENOTCONN; 573 errno = ENOTCONN;
618 #else
619 WSASetLastError( WSAENOTCONN );
620 #endif
621 return -1; 574 return -1;
622 } 575 }
623 576
624 gg_debug(GG_DEBUG_FUNCTION, "** gg_ping(...);\n"); 577 gg_debug(GG_DEBUG_FUNCTION, "** gg_ping(...);\n");
625 578
674 errno = EFAULT; 627 errno = EFAULT;
675 return -1; 628 return -1;
676 } 629 }
677 630
678 if (sess->state != GG_STATE_CONNECTED) { 631 if (sess->state != GG_STATE_CONNECTED) {
679 #ifndef _WIN32
680 errno = ENOTCONN; 632 errno = ENOTCONN;
681 #else
682 WSASetLastError( WSAENOTCONN );
683 #endif
684 return -1; 633 return -1;
685 } 634 }
686 635
687 gg_debug(GG_DEBUG_FUNCTION, "** gg_notify(..., %d);\n", count); 636 gg_debug(GG_DEBUG_FUNCTION, "** gg_notify(..., %d);\n", count);
688 637
723 errno = EFAULT; 672 errno = EFAULT;
724 return -1; 673 return -1;
725 } 674 }
726 675
727 if (sess->state != GG_STATE_CONNECTED) { 676 if (sess->state != GG_STATE_CONNECTED) {
728 #ifndef _WIN32
729 errno = ENOTCONN; 677 errno = ENOTCONN;
730 #else
731 WSASetLastError( WSAENOTCONN );
732 #endif
733 return -1; 678 return -1;
734 } 679 }
735 680
736 gg_debug(GG_DEBUG_FUNCTION, "** gg_add_notify(..., %u);\n", uin); 681 gg_debug(GG_DEBUG_FUNCTION, "** gg_add_notify(..., %u);\n", uin);
737 682
759 errno = EFAULT; 704 errno = EFAULT;
760 return -1; 705 return -1;
761 } 706 }
762 707
763 if (sess->state != GG_STATE_CONNECTED) { 708 if (sess->state != GG_STATE_CONNECTED) {
764 #ifndef _WIN32
765 errno = ENOTCONN; 709 errno = ENOTCONN;
766 #else
767 WSASetLastError( WSAENOTCONN );
768 #endif
769 return -1; 710 return -1;
770 } 711 }
771 712
772 gg_debug(GG_DEBUG_FUNCTION, "** gg_remove_notify(..., %u);\n", uin); 713 gg_debug(GG_DEBUG_FUNCTION, "** gg_remove_notify(..., %u);\n", uin);
773 714
981 char buf[1024]; 922 char buf[1024];
982 int res, res_size = sizeof(res); 923 int res, res_size = sizeof(res);
983 924
984 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING\n"); 925 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING\n");
985 926
986 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, 927 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, &res, &res_size) || res)) {
987 #ifndef _WIN32
988 &res,
989 #else
990 (char*)&res,
991 #endif
992 &res_size) || res)) {
993 #if 0 928 #if 0
994 struct in_addr *addr = (struct in_addr*) &sess->server_ip; 929 struct in_addr *addr = (struct in_addr*) &sess->server_ip;
995 gg_debug(GG_DEBUG_MISC, "-- http connection failed, errno = %d (%s), trying direct connection\n", res, strerror(res)); 930 gg_debug(GG_DEBUG_MISC, "-- http connection failed, errno = %d (%s), trying direct connection\n", res, strerror(res));
996 if ((sess->fd = gg_connect(addr, GG_DEFAULT_PORT, sess->async)) == -1) { 931 if ((sess->fd = gg_connect(addr, GG_DEFAULT_PORT, sess->async)) == -1) {
997 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); 932 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n");
1033 "Pragma: no-cache\r\n" 968 "Pragma: no-cache\r\n"
1034 "\r\n", sess->uin); 969 "\r\n", sess->uin);
1035 }; 970 };
1036 971
1037 gg_debug(GG_DEBUG_MISC, "=> -----BEGIN-HTTP-QUERY-----\n%s\n=> -----END-HTTP-QUERY-----\n", buf); 972 gg_debug(GG_DEBUG_MISC, "=> -----BEGIN-HTTP-QUERY-----\n%s\n=> -----END-HTTP-QUERY-----\n", buf);
1038 #ifndef _WIN32
1039 if (write(sess->fd, buf, strlen(buf)) < strlen(buf)) { 973 if (write(sess->fd, buf, strlen(buf)) < strlen(buf)) {
1040 #else
1041 if (send(sess->fd, buf, strlen(buf), 0) < strlen(buf)) {
1042 #endif
1043 gg_debug(GG_DEBUG_MISC, "-- sending query failed\n"); 974 gg_debug(GG_DEBUG_MISC, "-- sending query failed\n");
1044 errno = EIO; 975 errno = EIO;
1045 e->type = GG_EVENT_CONN_FAILED; 976 e->type = GG_EVENT_CONN_FAILED;
1046 e->event.failure = GG_FAILURE_WRITING; 977 e->event.failure = GG_FAILURE_WRITING;
1047 sess->state = GG_STATE_IDLE; 978 sess->state = GG_STATE_IDLE;
1139 { 1070 {
1140 int res, res_size = sizeof(res); 1071 int res, res_size = sizeof(res);
1141 1072
1142 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING_GG\n"); 1073 gg_debug(GG_DEBUG_MISC, "== GG_STATE_CONNECTING_GG\n");
1143 1074
1144 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, 1075 if (sess->async && (getsockopt(sess->fd, SOL_SOCKET, SO_ERROR, &res, &res_size) || res)) {
1145 #ifndef _WIN32
1146 &res,
1147 #else
1148 (char*)&res,
1149 #endif
1150 &res_size) || res)) {
1151 struct in_addr *addr = (struct in_addr*) &sess->server_ip; 1076 struct in_addr *addr = (struct in_addr*) &sess->server_ip;
1152 1077
1153 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n"); 1078 gg_debug(GG_DEBUG_MISC, "-- connection failed, trying https connection\n");
1154 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) { 1079 if ((sess->fd = gg_connect(addr, GG_HTTPS_PORT, sess->async)) == -1) {
1155 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno)); 1080 gg_debug(GG_DEBUG_MISC, "-- connection failed, errno = %d (%s)\n", errno, strerror(errno));
1178 char *password = sess->password; 1103 char *password = sess->password;
1179 1104
1180 gg_debug(GG_DEBUG_MISC, "== GG_STATE_READING_KEY\n"); 1105 gg_debug(GG_DEBUG_MISC, "== GG_STATE_READING_KEY\n");
1181 1106
1182 if (!(h = gg_recv_packet(sess))) { 1107 if (!(h = gg_recv_packet(sess))) {
1183 #ifndef _WIN32
1184 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet() failed. errno = %d (%s)\n", errno, strerror(errno)); 1108 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet() failed. errno = %d (%s)\n", errno, strerror(errno));
1185 #else
1186 gg_debug(GG_DEBUG_MISC, "-- gg_recv_packet() failed. errno = %d\n", WSAGetLastError());
1187 #endif
1188 e->type = GG_EVENT_CONN_FAILED; 1109 e->type = GG_EVENT_CONN_FAILED;
1189 e->event.failure = GG_FAILURE_READING; 1110 e->event.failure = GG_FAILURE_READING;
1190 sess->state = GG_STATE_IDLE; 1111 sess->state = GG_STATE_IDLE;
1191 close(sess->fd); 1112 close(sess->fd);
1192 break; 1113 break;