comparison src/protocols/oscar/conn.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 e941bfbacf7c
children 07283934dedd
comparison
equal deleted inserted replaced
3716:d7e83b4db191 3717:988485669631
12 12
13 #ifndef _WIN32 13 #ifndef _WIN32
14 #include <netdb.h> 14 #include <netdb.h>
15 #include <sys/socket.h> 15 #include <sys/socket.h>
16 #include <netinet/in.h> 16 #include <netinet/in.h>
17 #else 17 #endif
18 #include <winsock.h> 18
19 #ifdef _WIN32
20 #include "win32dep.h"
19 #endif 21 #endif
20 22
21 /* 23 /*
22 * In OSCAR, every connection has a set of SNAC groups associated 24 * In OSCAR, every connection has a set of SNAC groups associated
23 * with it. These are the groups that you can send over this connection 25 * with it. These are the groups that you can send over this connection
317 */ 319 */
318 faim_export void aim_conn_close(aim_conn_t *deadconn) 320 faim_export void aim_conn_close(aim_conn_t *deadconn)
319 { 321 {
320 322
321 if (deadconn->fd >= 3) 323 if (deadconn->fd >= 3)
322 #ifndef _WIN32
323 close(deadconn->fd); 324 close(deadconn->fd);
324 #else
325 closesocket(deadconn->fd);
326 #endif
327 deadconn->fd = -1; 325 deadconn->fd = -1;
328 if (deadconn->handlerlist) 326 if (deadconn->handlerlist)
329 aim_clearhandlers(deadconn); 327 aim_clearhandlers(deadconn);
330 if (deadconn->type == AIM_CONN_TYPE_RENDEZVOUS) 328 if (deadconn->type == AIM_CONN_TYPE_RENDEZVOUS)
331 aim_conn_close_rend((aim_session_t *)deadconn->sessv, deadconn); 329 aim_conn_close_rend((aim_session_t *)deadconn->sessv, deadconn);
433 sa.sin_family = hp->h_addrtype; 431 sa.sin_family = hp->h_addrtype;
434 432
435 fd = socket(hp->h_addrtype, SOCK_STREAM, 0); 433 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
436 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { 434 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
437 faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n"); 435 faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n");
438 #ifndef _WIN32
439 close(fd); 436 close(fd);
440 #else
441 closesocket(fd);
442 #endif
443 return -1; 437 return -1;
444 } 438 }
445 439
446 i = 0; 440 i = 0;
447 buf[0] = 0x05; /* SOCKS version 5 */ 441 buf[0] = 0x05; /* SOCKS version 5 */
453 } else { 447 } else {
454 buf[1] = 0x01; 448 buf[1] = 0x01;
455 buf[2] = 0x00; 449 buf[2] = 0x00;
456 i = 3; 450 i = 3;
457 } 451 }
458 #ifndef _WIN32
459 if (write(fd, buf, i) < i) { 452 if (write(fd, buf, i) < i) {
460 #else
461 if (send(fd, buf, i, 0) < i) {
462 #endif
463 *statusret = errno; 453 *statusret = errno;
464 #ifndef _WIN32
465 close(fd); 454 close(fd);
466 #else
467 closesocket(fd);
468 #endif
469 return -1; 455 return -1;
470 } 456 }
471 #ifndef _WIN32
472 if (read(fd, buf, 2) < 2) { 457 if (read(fd, buf, 2) < 2) {
473 #else
474 if (recv(fd, buf, 2, 0) < 2) {
475 #endif
476 *statusret = errno; 458 *statusret = errno;
477 #ifndef _WIN32
478 close(fd); 459 close(fd);
479 #else
480 closesocket(fd);
481 #endif
482 return -1; 460 return -1;
483 } 461 }
484 462
485 if ((buf[0] != 0x05) || (buf[1] == 0xff)) { 463 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
486 *statusret = EINVAL; 464 *statusret = EINVAL;
487 #ifndef _WIN32
488 close(fd); 465 close(fd);
489 #else
490 closesocket(fd);
491 #endif
492 return -1; 466 return -1;
493 } 467 }
494 468
495 /* check if we're doing username authentication */ 469 /* check if we're doing username authentication */
496 if (buf[1] == 0x02) { 470 if (buf[1] == 0x02) {
497 i = aimutil_put8(buf, 0x01); /* version 1 */ 471 i = aimutil_put8(buf, 0x01); /* version 1 */
498 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username)); 472 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username));
499 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username)); 473 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username));
500 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password)); 474 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password));
501 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password)); 475 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password));
502 #ifndef _WIN32
503 if (write(fd, buf, i) < i) { 476 if (write(fd, buf, i) < i) {
504 #else
505 if (send(fd, buf, i, 0) < i) {
506 #endif
507 *statusret = errno; 477 *statusret = errno;
508 #ifndef _WIN32
509 close(fd); 478 close(fd);
510 #else
511 closesocket(fd);
512 #endif
513 return -1; 479 return -1;
514 } 480 }
515 #ifndef _WIN32
516 if (read(fd, buf, 2) < 2) { 481 if (read(fd, buf, 2) < 2) {
517 #else
518 if (recv(fd, buf, 2, 0) < 2) {
519 #endif
520 *statusret = errno; 482 *statusret = errno;
521 #ifndef _WIN32
522 close(fd); 483 close(fd);
523 #else
524 closesocket(fd);
525 #endif
526 return -1; 484 return -1;
527 } 485 }
528 if ((buf[0] != 0x01) || (buf[1] != 0x00)) { 486 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
529 *statusret = EINVAL; 487 *statusret = EINVAL;
530 #ifndef _WIN32
531 close(fd); 488 close(fd);
532 #else
533 closesocket(fd);
534 #endif
535 return -1; 489 return -1;
536 } 490 }
537 } 491 }
538 492
539 i = aimutil_put8(buf, 0x05); 493 i = aimutil_put8(buf, 0x05);
541 i += aimutil_put8(buf+i, 0x00); /* reserved */ 495 i += aimutil_put8(buf+i, 0x00); /* reserved */
542 i += aimutil_put8(buf+i, 0x03); /* address type: host name */ 496 i += aimutil_put8(buf+i, 0x03); /* address type: host name */
543 i += aimutil_put8(buf+i, strlen(host)); 497 i += aimutil_put8(buf+i, strlen(host));
544 i += aimutil_putstr(buf+i, host, strlen(host)); 498 i += aimutil_putstr(buf+i, host, strlen(host));
545 i += aimutil_put16(buf+i, port); 499 i += aimutil_put16(buf+i, port);
546 #ifndef _WIN32 500
547 if (write(fd, buf, i) < i) { 501 if (write(fd, buf, i) < i) {
548 #else
549 if (send(fd, buf, i, 0) < i) {
550 #endif
551 *statusret = errno; 502 *statusret = errno;
552 #ifndef _WIN32
553 close(fd); 503 close(fd);
554 #else
555 closesocket(fd);
556 #endif
557 return -1; 504 return -1;
558 } 505 }
559 #ifndef _WIN32 506
560 if (read(fd, buf, 10) < 10) { 507 if (read(fd, buf, 10) < 10) {
561 #else
562 if (recv(fd, buf, 10, 0) < 10) {
563 #endif
564 *statusret = errno; 508 *statusret = errno;
565 #ifndef _WIN32
566 close(fd); 509 close(fd);
567 #else
568 closesocket(fd);
569 #endif
570 return -1; 510 return -1;
571 } 511 }
572 if ((buf[0] != 0x05) || (buf[1] != 0x00)) { 512 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
573 *statusret = EINVAL; 513 *statusret = EINVAL;
574 #ifndef _WIN32
575 close(fd); 514 close(fd);
576 #else
577 closesocket(fd);
578 #endif
579 return -1; 515 return -1;
580 } 516 }
581 517
582 } else { /* connecting directly */ 518 } else { /* connecting directly */
583 struct sockaddr_in sa; 519 struct sockaddr_in sa;
584 struct hostent *hp; 520 struct hostent *hp;
585 #ifdef _WIN32 521
586 int imode = 1;
587 int w_errno = 0;
588 #endif
589 if (!(hp = gethostbyname(host))) { 522 if (!(hp = gethostbyname(host))) {
590 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR); 523 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
591 return -1; 524 return -1;
592 } 525 }
593 526
597 sa.sin_family = hp->h_addrtype; 530 sa.sin_family = hp->h_addrtype;
598 531
599 fd = socket(hp->h_addrtype, SOCK_STREAM, 0); 532 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
600 533
601 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) 534 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT)
602 #ifndef _WIN32
603 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */ 535 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */
604 #else 536
605 ioctlsocket(fd, FIONBIO, (unsigned long *)&imode);
606 #endif
607 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { 537 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
608 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) { 538 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) {
609 #ifndef _WIN32
610 if ((errno == EINPROGRESS) || (errno == EINTR)) { 539 if ((errno == EINPROGRESS) || (errno == EINTR)) {
611 #else
612 w_errno = WSAGetLastError();
613 if ((w_errno == WSAEINPROGRESS) || (w_errno == WSAEINTR)) {
614 #endif
615 if (statusret) 540 if (statusret)
616 *statusret |= AIM_CONN_STATUS_INPROGRESS; 541 *statusret |= AIM_CONN_STATUS_INPROGRESS;
617 return fd; 542 return fd;
618 } 543 }
619 } 544 }
620 #ifndef _WIN32
621 close(fd); 545 close(fd);
622 #else
623 closesocket(fd);
624 #endif
625 fd = -1; 546 fd = -1;
626 } 547 }
627 } 548 }
628 return fd; 549 return fd;
629 } 550 }
1071 faim_export int aim_conn_completeconnect(aim_session_t *sess, aim_conn_t *conn) 992 faim_export int aim_conn_completeconnect(aim_session_t *sess, aim_conn_t *conn)
1072 { 993 {
1073 fd_set fds, wfds; 994 fd_set fds, wfds;
1074 struct timeval tv; 995 struct timeval tv;
1075 int res; 996 int res;
1076 #ifndef _WIN32
1077 int error = ETIMEDOUT; 997 int error = ETIMEDOUT;
1078 #else 998
1079 int error = 0;
1080 #endif
1081 aim_rxcallback_t userfunc; 999 aim_rxcallback_t userfunc;
1082 1000
1083 if (!conn || (conn->fd == -1)) 1001 if (!conn || (conn->fd == -1))
1084 return -1; 1002 return -1;
1085 1003
1092 FD_SET(conn->fd, &wfds); 1010 FD_SET(conn->fd, &wfds);
1093 tv.tv_sec = 0; 1011 tv.tv_sec = 0;
1094 tv.tv_usec = 0; 1012 tv.tv_usec = 0;
1095 1013
1096 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) { 1014 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) {
1097 #ifndef _WIN32
1098 error = errno; 1015 error = errno;
1099 #else
1100 error = WSAGetLastError();
1101 #endif
1102 aim_conn_close(conn); 1016 aim_conn_close(conn);
1103 errno = error; 1017 errno = error;
1104 return -1; 1018 return -1;
1105 } else if (res == 0) { 1019 } else if (res == 0) {
1106 faimdprintf(sess, 0, "aim_conn_completeconnect: false alarm on %d\n", conn->fd); 1020 faimdprintf(sess, 0, "aim_conn_completeconnect: false alarm on %d\n", conn->fd);
1107 return 0; /* hasn't really completed yet... */ 1021 return 0; /* hasn't really completed yet... */
1108 } 1022 }
1109 1023
1110 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) { 1024 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) {
1111 int len = sizeof(error); 1025 int len = sizeof(error);
1112 #ifndef _WIN32
1113 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) 1026 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
1114 error = errno; 1027 error = errno;
1115 #else
1116 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0)
1117 error = WSAGetLastError();
1118 #endif
1119 } 1028 }
1120 1029
1121 if (error) { 1030 if (error) {
1122 aim_conn_close(conn); 1031 aim_conn_close(conn);
1123 errno = error; 1032 errno = error;
1124 return -1; 1033 return -1;
1125 } 1034 }
1126 #ifndef _WIN32
1127 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */ 1035 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */
1128 #endif 1036
1129 conn->status &= ~AIM_CONN_STATUS_INPROGRESS; 1037 conn->status &= ~AIM_CONN_STATUS_INPROGRESS;
1130 1038
1131 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE))) 1039 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE)))
1132 userfunc(sess, NULL, conn); 1040 userfunc(sess, NULL, conn);
1133 1041