1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
|
/*
* Ayttm
*
* Copyright (C) 2003, the Ayttm team
*
* Ayttm is a derivative of Everybuddy
* Copyright (C) 1998-1999, Torrey Searle
* proxy featured by Seb C.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/* this is a little piece of code to handle proxy connection */
/* it is intended to : 1st handle http proxy, using the CONNECT command
, 2nd provide an easy way to add socks support */
#include "intl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include "libproxy.h"
#include "tcp_util.h"
#include "messages.h"
#ifdef __MINGW32__
#define sleep(a) Sleep(1000*a)
#define bcopy(a,b,c) memcpy(b,a,c)
#define bzero(a,b) memset(a,0,b)
#define ECONNREFUSED WSAECONNREFUSED
#endif
static int proxy_inited = 0;
static char *proxy_realhost = NULL;
static char debug_buff[256];
static int proxy_type = PROXY_NONE;
static char *proxy_host = NULL;
static int proxy_port = 3128;
static int proxy_auth_required = 0;
static char *proxy_auth = NULL;
static char *proxy_user = NULL;
static char *proxy_password = NULL;
/* Prototypes */
static char *encode_proxy_auth_str( const char *user, const char *passwd );
static int ap_base64encode( char *encoded, const char *string, int len );
#define debug_print printf
#ifndef HAVE_GETHOSTBYNAME2
#define gethostbyname2(x, y) gethostbyname(x)
#endif
typedef struct _udp_connection
{
int fd;
struct sockaddr * host;
struct _udp_connection * next;
} udp_connection;
static udp_connection *server_list = NULL;
static struct sockaddr *get_server( int fd )
{
udp_connection * node = NULL;
printf("looking for connection matching %d", fd);
for ( node = server_list; node; node = node->next )
{
if(fd == node->fd )
{
printf("match found\n");
return node->host;
}
}
printf("no match found\n");
return NULL;
}
static int connect_address( unsigned int addy, unsigned short port )
{
int fd;
struct sockaddr_in sin;
sin.sin_addr.s_addr = addy;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd > -1)
{
if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) > -1)
{
sleep(2);
return fd;
}
else
{
perror("connect:" );
}
}
else
{
printf("unable to create socket\n");
}
return -1;
}
/* Call this function if you want to free memory when leaving */
static void proxy_freemem( void )
{
/* dummy function to avoid possibly memory leak */
if (proxy_host != NULL)
free(proxy_host);
if (proxy_realhost != NULL)
free(proxy_realhost);
if ( proxy_auth != NULL )
free( proxy_auth );
return;
}
/* this function is useless if you do use an external method to set
the proxy setting, but it does yet allow to test the code without
having to code the interface */
static void proxy_autoinit( void )
{
if (getenv("HTTPPROXY") != NULL) {
proxy_host=(char *)strdup(getenv("HTTPPROXY"));
if (proxy_host != NULL) {
proxy_port=atoi(getenv("HTTPPROXYPORT"));
proxy_type=PROXY_HTTP;
if (!proxy_port)
proxy_port=3128;
if ( getenv( "HTTPPROXYUSER" ) ) {
char *proxy_user;
char *proxy_password;
proxy_user = getenv( "HTTPPROXYUSER" );
proxy_password = getenv( "HTTPPROXYPASSWORD" );
proxy_auth = encode_proxy_auth_str(proxy_user, proxy_password);
}
#ifdef HAVE_ATEXIT
/* if you do not have atexit, it means that if you don't call
proxy_freemem when leaving, you'll have mem leaks */
atexit(proxy_freemem);
#endif
}
}
else
{
proxy_type=PROXY_NONE;
}
proxy_inited=1;
#ifdef __MINGW32__
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,0),&wsaData);
}
#endif
return;
}
/* external function to use to set the proxy settings */
int proxy_set_proxy( int type, const char *host, int port )
{
proxy_type=type;
if (type != PROXY_NONE) {
proxy_port=0;
if (host != NULL) {
proxy_host=(char *)strdup(host);
proxy_port=port;
}
if (proxy_port == 0)
proxy_port=3128;
#ifdef HAVE_ATEXIT
/* if you do not have atexit, it means that if you don't call
proxy_freemem when leaving, you'll have mem leaks */
atexit(proxy_freemem);
#endif
}
proxy_inited=1;
#ifdef __MINGW32__
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,0),&wsaData);
}
#endif
return(0);
}
/* external function to set the proxy user and proxy pasword */
int proxy_set_auth( const int required, const char *user, const char *passwd )
{
proxy_auth_required = required;
if (required)
{
proxy_auth = encode_proxy_auth_str(user, passwd);
}
else
{
if (proxy_auth)
free(proxy_auth);
proxy_auth = NULL;
}
if (proxy_user)
free(proxy_user);
if (proxy_password)
free(proxy_password);
proxy_user = strdup(user);
proxy_password = strdup(passwd);
return 1;
}
const char *proxy_get_auth( void )
{
return proxy_auth;
}
/* this code is borrowed from cvs 1.10 */
static int proxy_recv_line( int sock, char **resultp )
{
int c;
char *result;
size_t input_index = 0;
size_t result_size = 80;
result = (char *) malloc (result_size);
while (1)
{
char ch;
if (recv (sock, &ch, 1, 0) < 0) {
fprintf (stderr, "recv() error from proxy server\n");
return -1;
}
c = ch;
if (c == EOF)
{
free (result);
/* It's end of file. */
fprintf(stderr, "end of file from server\n");
}
if (c == '\012')
break;
result[input_index++] = c;
while (input_index + 1 >= result_size)
{
result_size *= 2;
result = (char *) realloc (result, result_size);
}
}
if (resultp)
*resultp = result;
/* Terminate it just for kicks, but we *can* deal with embedded NULs. */
result[input_index] = '\0';
if (resultp == NULL)
free (result);
return input_index;
}
int proxy_recv( int s, void * buff, int len, unsigned int flags )
{
#ifdef __MINGW32__
char type;
#else
int type;
#endif
int size;
if( proxy_type == PROXY_SOCKS5 )
getsockopt( s, SOL_SOCKET, SO_TYPE, &type, &size );
if( proxy_type != PROXY_SOCKS5 || type == SOCK_STREAM )
{
return recv(s, buff, len, flags );
}
else
{
const int buff_len = len + 10;
char *tmpbuff = calloc( buff_len, sizeof( char ) );
int mylen = recv( s, tmpbuff, buff_len, flags );
memcpy( buff, tmpbuff+10, len);
free( tmpbuff );
return( mylen - 9 );
}
return( 0 );
}
int proxy_send( int s, const void *msg, int len, unsigned int flags )
{
#ifdef __MINGW32__
char type;
#else
int type;
#endif
int size;
printf("proxy_send\n");
if( proxy_type == PROXY_SOCKS5 )
getsockopt( s, SOL_SOCKET, SO_TYPE, &type, &size );
if( proxy_type != PROXY_SOCKS5 || type == SOCK_STREAM)
{
if( type != SOCK_DGRAM )
printf("type TCP\n");
else
printf("type UDP\n");
return send(s, msg, len, flags);
}
else
{
const int buff_len = len + 10;
char *buff = calloc( buff_len, sizeof( char ) );
struct sockaddr * serv_addr = get_server( s );
int ret;
printf("type UDP %s %d\n",
inet_ntoa((((struct sockaddr_in *)serv_addr)->sin_addr)),
ntohs(((struct sockaddr_in *)serv_addr)->sin_port) );
buff[3] = 0x01;
memcpy(buff+4, &(((struct sockaddr_in *)serv_addr)->sin_addr),4 );
memcpy((buff+8), &(((struct sockaddr_in *)serv_addr)->sin_port), 2);
memcpy((buff+10), msg, len );
ret = send( s, buff, buff_len, flags );
free( buff );
return( ret );
}
return( 0 );
}
struct hostent *proxy_gethostbyname( const char *host )
{
if ( !proxy_inited )
proxy_autoinit();
if ( proxy_type == PROXY_NONE || proxy_type == PROXY_SOCKS5 )
return( gethostbyname( host ) );
if ( proxy_realhost != NULL )
free( proxy_realhost );
/* we keep the real host name for the Connect command */
proxy_realhost = strdup( host );
return( gethostbyname( proxy_host ) );
}
struct hostent *proxy_gethostbyname2( const char *host, int type )
{
if ( !proxy_inited )
proxy_autoinit();
if ( proxy_type == PROXY_NONE || proxy_type == PROXY_SOCKS5 )
return( gethostbyname2( host, type ) );
if ( proxy_realhost != NULL )
free( proxy_realhost );
/* we keep the real host name for the Connect command */
proxy_realhost = strdup( host );
return( gethostbyname2( proxy_host, type ) );
}
/* http://archive.socks.permeo.com/protocol/socks4.protocol */
static int socks4_connect( int sock, struct sockaddr *serv_addr, int addrlen )
{
struct sockaddr_in sa;
int i, packetlen;
struct hostent * hp;
if (!(hp = gethostbyname(proxy_host)))
return -1;
if(proxy_auth && strlen(proxy_auth)) {
packetlen = 9 + strlen(proxy_auth);
} else {
packetlen = 9;
}
/* Clear the structure and setup for SOCKS4 open */
bzero(&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons (proxy_port);
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
/* Connect to the SOCKS4 port and send a connect packet */
if (connect(sock, (struct sockaddr *) &sa, sizeof (sa))!=-1)
{
unsigned short realport;
unsigned char packet[packetlen];
if (!(hp = gethostbyname(proxy_realhost)))
{
return -1;
}
realport = ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
packet[0] = 4; /* Version */
packet[1] = 1; /* CONNECT */
packet[2] = (((unsigned short) realport) >> 8); /* DESTPORT */
packet[3] = (((unsigned short) realport) & 0xff); /* DESTPORT */
packet[4] = (unsigned char) (hp->h_addr_list[0])[0]; /* DESTIP */
packet[5] = (unsigned char) (hp->h_addr_list[0])[1]; /* DESTIP */
packet[6] = (unsigned char) (hp->h_addr_list[0])[2]; /* DESTIP */
packet[7] = (unsigned char) (hp->h_addr_list[0])[3]; /* DESTIP */
if(proxy_auth && strlen(proxy_auth)) {
for (i=0; i < strlen(proxy_user); i++) {
packet[i+8] =
(unsigned char)proxy_user[i]; /* AUTH */
}
}
packet[packetlen-1] = 0; /* END */
printf("Sending \"%s\"\n", packet);
if (write(sock, packet, packetlen) == packetlen)
{
bzero(packet, sizeof(packet));
/* Check response - return as SOCKS4 if its valid */
if (read(sock, packet, 9)>=4)
{
if (packet[1] == 90)
return 0;
else if(packet[1] == 91)
ay_do_error( _("Proxy Error"), _("Socks 4 proxy rejected request for an unknown reason.") );
else if(packet[1] == 92)
ay_do_error( _("Proxy Error"), _("Socks 4 proxy rejected request because it could not connect to our identd.") );
else if(packet[1] == 93)
ay_do_error( _("Proxy Error"), _("Socks 4 proxy rejected request because identd returned a different userid.") );
else {
ay_do_error( _("Proxy Error"), _("Socks 4 proxy rejected request with an RFC-uncompliant error cod.") );
printf("=>>%d\n",packet[1]);
}
} else {
printf("short read %s\n",packet);
}
}
close(sock);
}
return -1;
}
/* http://archive.socks.permeo.com/rfc/rfc1928.txt */
/* http://archive.socks.permeo.com/rfc/rfc1929.txt */
static int socks5_connect( int sockfd, struct sockaddr *serv_addr, int addrlen )
{
int i;
int s =0;
#ifdef __MINGW32__
char type;
#else
int type;
#endif
int size = sizeof(int);
int tcplink = 0;
struct sockaddr_in bind_addr;
struct sockaddr_in sin;
struct hostent * hostinfo;
char buff[530];
int need_auth = 0;
int j;
getsockopt( sockfd, SOL_SOCKET, SO_TYPE, &type, &size );
printf(" %d %d %d \n", SOCK_STREAM, SOCK_DGRAM, type );
bind_addr.sin_addr.s_addr = INADDR_ANY;
bind_addr.sin_family = PF_INET;
bind_addr.sin_port = 0;
if( type == SOCK_DGRAM )
{
size = sizeof(bind_addr);
if(bind(sockfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) < 0 )
{
perror("bind");
}
getsockname( sockfd, (struct sockaddr*)&bind_addr, &size );
}
hostinfo = gethostbyname (proxy_host);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.\n", proxy_host);
return (-1);
}
/* connect to the proxy server */
bcopy(hostinfo->h_addr, (char *)&sin.sin_addr, hostinfo->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(proxy_port);
if( type != SOCK_DGRAM )
{
s = connect(sockfd, (struct sockaddr *)&sin, sizeof(sin));
if( s < 0 )
return s;
}
else
{
#ifdef __MINGW32__
printf("Connecting to %lx on %u\n", inet_addr(proxy_host), proxy_port );
#else
printf("Connecting to %x on %u\n", inet_addr(proxy_host), proxy_port );
#endif
tcplink = connect_address( inet_addr(proxy_host), proxy_port );
printf("We got socket %d\n", tcplink);
}
buff[0] = 0x05; //use socks v5
if(proxy_user && strlen(proxy_user)) {
buff[1] = 0x02; //we support (no authentication & username/pass)
buff[2] = 0x00; //we support the method type "no authentication"
buff[3] = 0x02; //we support the method type "username/passw"
need_auth=1;
} else {
buff[1] = 0x01; //we support (no authentication)
buff[2] = 0x00; //we support the method type "no authentication"
}
if( type != SOCK_DGRAM )
{
int l = 0;
write( sockfd, buff, 3+need_auth );
l = read( sockfd, buff, 2 );
printf("read %d\n",l);
}
else
{
write( tcplink, buff, 3+need_auth );
read( tcplink, buff, 2 );
fprintf(stderr, "We got a response back from the SOCKS server\n");
}
printf("buff[] %d %d proxy_user %s\n",buff[0],buff[1], proxy_user);
if( buff[1] == 0x00 )
need_auth = 0;
else if (buff[1] == 0x02 && proxy_user && strlen(proxy_user))
need_auth = 1;
else {
fprintf(stderr, "No Acceptable Methods");
return -1;
}
printf("need_auth=%d\n",need_auth);
if (need_auth) {
/* subneg start */
buff[0] = 0x01; /* subneg version */
printf("[%d]",buff[0]);
buff[1] = strlen(proxy_user); /* username length */
printf("[%d]",buff[1]);
for (i=0; i < strlen(proxy_user) && i<255; i++) {
buff[i+2] =
proxy_user[i]; /* AUTH */
printf("%c",buff[i+2]);
}
i+=2;
buff[i] = strlen(proxy_password);
printf("[%d]",buff[i]);
i++;
for (j=0; j < strlen(proxy_password) && j<255; j++) {
buff[i+j] =
proxy_password[j]; /* AUTH */
printf("%c",buff[i+j]);
}
i+=(j);
buff[i]=0;
if( type != SOCK_DGRAM )
{
write( sockfd, buff, i );
read( sockfd, buff, 2 );
}
else
{
write( tcplink, buff, i );
read( tcplink, buff, 2 );
fprintf(stderr, "We got a response back from the SOCKS server\n");
}
if (buff[1] != 0) {
ay_do_error( _("Proxy Error"), _("Socks5 proxy refused our authentication.") );
return -1;
}
}
buff[0] = 0x05; //use socks5
buff[1] = ((type == SOCK_STREAM) ? 0x01 : 0x03); //connect
buff[2] = 0x00; //reserved
buff[3] = 0x01; //ipv4 address
if( type != SOCK_DGRAM )
{
memcpy(buff+4, &(((struct sockaddr_in *)serv_addr)->sin_addr),4 );
memcpy((buff+8), &(((struct sockaddr_in *)serv_addr)->sin_port), 2);
}
else
{
buff[4] = 0x00;
buff[5] = 0x00;
buff[6] = 0x00;
buff[7] = 0x00;
memcpy((buff+8), &(((struct sockaddr_in *)&bind_addr)->sin_port), 2);
printf("UDP port %s %d\n", inet_ntoa(((struct sockaddr_in *)serv_addr)->sin_addr),
ntohs(((struct sockaddr_in *)serv_addr)->sin_port));
for( i = 0; i < 8; i++ )
{
printf("%03d ", buff[i] );
}
printf("%d", ntohs(*(unsigned short *)&buff[8]));
printf("\n");
}
if( type != SOCK_DGRAM )
{
write( sockfd, buff, 10 );
read(sockfd, buff, 10);
}
else
{
write( tcplink, buff, 10 );
read( tcplink, buff, 10);
fprintf(stderr, "We got another response from the server!\n");
}
// buff[1] = 0;
if( buff[1] == 0x00 )
{
for( i = 0; i < 8; i++ )
{
printf("%03d ", buff[i] );
}
printf("%d", ntohs(*(unsigned short *)&buff[8]));
printf("\n");
if( type == SOCK_DGRAM )
{
udp_connection *conn = calloc( 1, sizeof(udp_connection) );
struct sockaddr *host = calloc( 1, sizeof(struct sockaddr) );
memcpy(host, serv_addr, sizeof(struct sockaddr_in));
conn->next = server_list;
conn->fd = sockfd;
conn->host = host;
server_list = conn;
printf("adding UDP connection %s %d on fd %d\n",
inet_ntoa((((struct sockaddr_in *)serv_addr)->sin_addr)),
ntohs(((struct sockaddr_in *)serv_addr)->sin_port),
sockfd );
memcpy( &sin.sin_addr, buff+4, 4);
memcpy( &sin.sin_port, buff+8, 2);
sin.sin_family = AF_INET;
printf( "trying to connect to %s on port %d \n",
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port) );
s = connect(sockfd, (struct sockaddr *)&sin, sizeof(sin));
}
printf("libproxy; SOCKS5 connection on %d (%d)\n", sockfd,s );
return s;
}
else
{
for( i = 0; i < 8; i++ )
{
printf("%03d ", buff[i] );
}
printf("%d", ntohs(*(unsigned short *)&buff[8]));
printf("\n");
fprintf(stderr, "SOCKS error number %d\n", buff[1] );
close(sockfd);
return -1;
}
}
static int http_tunnel_init( int sockfd, struct sockaddr *serv_addr, int addrlen )
{
/* step two : do proxy tunneling init */
char cmd[200];
char *inputline = NULL;
unsigned short realport=ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
sprintf(cmd,"CONNECT %s:%d HTTP/1.1\r\n",proxy_realhost,realport);
if ( proxy_auth != NULL ) {
strcat( cmd, "Proxy-Authorization: Basic " );
strcat( cmd, proxy_auth );
strcat( cmd, "\r\n" );
}
strcat( cmd, "\r\n" );
#ifndef DEBUG
sprintf(debug_buff,"<%s>\n",cmd);
debug_print(debug_buff);
#endif
if (send(sockfd,cmd,strlen(cmd),0)<0)
return(-1);
if (proxy_recv_line(sockfd,&inputline) < 0)
return(-1);
#ifndef DEBUG
sprintf(debug_buff,"<%s>\n",inputline);
debug_print(debug_buff);
#endif
if (!strstr(inputline, "200")) {
/* Check if proxy authorization needed */
if ( strstr( inputline, "407" ) ) {
while(proxy_recv_line(sockfd,&inputline) > 0) {
free(inputline);
}
ay_do_error( _("Proxy Error"), _("HTTP proxy error: Authentication required.") );
return( -2 );
}
if ( strstr( inputline, "403" ) ) {
while(proxy_recv_line(sockfd,&inputline) > 0) {
free(inputline);
}
ay_do_error( _("Proxy Error"), _("HTTP proxy error: permission denied.") );
return( -2 );
}
free(inputline);
return(-1);
}
while (strlen(inputline)>1) {
free(inputline);
if (proxy_recv_line(sockfd,&inputline) < 0) {
return(-1);
}
#ifndef DEBUG
sprintf(debug_buff,"<%s>\n",inputline);
debug_print(debug_buff);
#endif
}
free(inputline);
return 0;
}
static int http_connect( int sockfd, struct sockaddr *serv_addr, int addrlen )
{
/* do the tunneling */
/* step one : connect to proxy */
struct sockaddr_in name;
int ret;
struct hostent *hostinfo;
unsigned short shortport = proxy_port;
memset (&name, 0, sizeof (name));
name.sin_family = AF_INET;
name.sin_port = htons (shortport);
hostinfo = gethostbyname (proxy_host);
if (hostinfo == NULL) {
fprintf (stderr, "Unknown host %s.\n", proxy_host);
return (-1);
}
name.sin_addr = *(struct in_addr *) hostinfo->h_addr;
#ifndef DEBUG
sprintf(debug_buff,"Trying to connect ...\n");
debug_print(debug_buff);
#endif
if ((ret = connect(sockfd,(struct sockaddr *)&name,sizeof(name)))<0)
return(ret);
return (http_tunnel_init(sockfd, serv_addr, addrlen));
}
int proxy_connect_host( const char *host, int port, void *cb, void *data, void *scb )
{
struct hostent *hp;
struct sockaddr_in sin;
if ((hp = proxy_gethostbyname((char *)host)) == NULL)
{
ay_do_error( _("Proxy Error"), _("Could not resolve host.") );
perror("ghbn");
return -1;
}
sin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
return proxy_connect(-1, (struct sockaddr *)&sin, sizeof(sin), cb, data, scb);
}
static int conn_ok( int sockfd, void *cb, void *data )
{
ay_socket_callback callback = (ay_socket_callback)cb;
if (callback) {
callback(sockfd, 0, data);
return 0;
} else {
return sockfd;
}
}
static int conn_nok( int sockfd, void *cb, void *data )
{
ay_socket_callback callback = (ay_socket_callback)cb;
if (callback) {
callback(-1, ECONNREFUSED, data);
return 0;
} else {
return -1;
}
}
int proxy_get_proxy( ) {
return proxy_type;
}
int proxy_connect( int sockfd, struct sockaddr *serv_addr, int addrlen, void *cb, void *data, void *scb )
{
int tmp;
ay_socket_callback callback = (ay_socket_callback)cb;
if (!proxy_inited)
proxy_autoinit();
if (sockfd == -1 && (proxy_type != PROXY_NONE || !callback))
sockfd = socket(AF_INET, SOCK_STREAM, 0);
switch (proxy_type) {
case PROXY_NONE: /* No proxy */
{
struct sockaddr_in *sin = (struct sockaddr_in *)serv_addr;
if (callback)
return ay_socket_new_async(
inet_ntoa(sin->sin_addr),
ntohs(sin->sin_port),
callback, data, scb);
else {
if (connect(sockfd, serv_addr, addrlen) == 0) {
return sockfd;
} else {
return -1;
}
}
break;
}
case PROXY_HTTP: /* Http proxy */
if ( (tmp=http_connect(sockfd, serv_addr, addrlen)) >= 0 ) {
return conn_ok(sockfd, callback, data);
} else {
return conn_nok(sockfd, callback, data);
}
break;
case PROXY_SOCKS4: /* SOCKS4 proxy */
if ( (tmp=socks4_connect(sockfd, serv_addr, addrlen)) >= 0 ) {
return conn_ok(sockfd, callback, data);
} else {
return conn_nok(sockfd, callback, data);
}
break;
case PROXY_SOCKS5: /* SOCKS5 proxy */
if ( (tmp=socks5_connect(sockfd, serv_addr, addrlen)) >= 0 ) {
return conn_ok(sockfd, callback, data);
} else {
return conn_nok(sockfd, callback, data);
}
break;
default:
fprintf(stderr,"Unknown proxy type : %d.\n",proxy_type);
break;
}
return(-1);
}
static char *encode_proxy_auth_str( const char *user, const char *passwd )
{
char buff[200];
char buff1[200];
if ( user == NULL )
return NULL;
strcpy( buff, user );
strcat( buff, ":" );
strcat( buff, passwd );
ap_base64encode( buff1, buff, strlen( buff ) );
return strdup( buff1 );
}
/* ====================================================================
* Copyright (c) 1995-1999 The Apache Group. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
/* base64 encoder/decoder. Originally part of main/util.c
* but moved here so that support/ab and ap_sha1.c could
* use it. This meant removing the ap_palloc()s and adding
* ugly 'len' functions, which is quite a nasty cost.
*/
/*#include <string.h>*/
/* aaaack but it's fast and const should make it shared text page. */
static const unsigned char pr2six[256] =
{
#ifndef CHARSET_EBCDIC
/* ASCII table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
#else /*CHARSET_EBCDIC*/
/* EBCDIC table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 64, 64, 64, 64, 64, 64,
64, 35, 36, 37, 38, 39, 40, 41, 42, 43, 64, 64, 64, 64, 64, 64,
64, 64, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 64, 64, 64, 64, 64, 64,
64, 9, 10, 11, 12, 13, 14, 15, 16, 17, 64, 64, 64, 64, 64, 64,
64, 64, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64
#endif /*CHARSET_EBCDIC*/
};
/* Ayttm - We don't use these but they are left in in case we want them later */
#if 0
static int ap_base64decode_binary(unsigned char *bufplain, const char *bufcoded);
static int ap_base64decode_len(const char *bufcoded)
{
int nbytesdecoded;
register const unsigned char *bufin;
register int nprbytes;
bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
nbytesdecoded = ((nprbytes + 3) / 4) * 3;
return nbytesdecoded + 1;
}
static int ap_base64decode(char *bufplain, const char *bufcoded)
{
#ifdef CHARSET_EBCDIC
int i;
#endif /* CHARSET_EBCDIC */
int len;
len = ap_base64decode_binary((unsigned char *) bufplain, bufcoded);
#ifdef CHARSET_EBCDIC
for (i = 0; i < len; i++)
bufplain[i] = os_toebcdic[bufplain[i]];
#endif /* CHARSET_EBCDIC */
bufplain[len] = '\0';
return len;
}
/* This is the same as ap_base64udecode() except on EBCDIC machines, where
* the conversion of the output to ebcdic is left out.
*/
static int ap_base64decode_binary(unsigned char *bufplain,
const char *bufcoded)
{
int nbytesdecoded;
register const unsigned char *bufin;
register unsigned char *bufout;
register int nprbytes;
bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
nbytesdecoded = ((nprbytes + 3) / 4) * 3;
bufout = (unsigned char *) bufplain;
bufin = (const unsigned char *) bufcoded;
while (nprbytes > 4) {
*(bufout++) =
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
*(bufout++) =
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
*(bufout++) =
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
bufin += 4;
nprbytes -= 4;
}
/* Note: (nprbytes == 1) would be an error, so just ingore that case */
if (nprbytes > 1) {
*(bufout++) =
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
}
if (nprbytes > 2) {
*(bufout++) =
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
}
if (nprbytes > 3) {
*(bufout++) =
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
}
nbytesdecoded -= (4 - nprbytes) & 3;
return nbytesdecoded;
}
#endif
static const char basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* Ayttm - We don't use these but they are left in in case we want them later */
#if 0
static int ap_base64encode_len(int len)
{
return ((len + 2) / 3 * 4) + 1;
}
#endif
/* This is the same as ap_base64encode() except on EBCDIC machines, where
* the conversion of the input to ascii is left out.
*/
static int ap_base64encode_binary(char *encoded,
const unsigned char *string, int len)
{
int i;
char *p;
p = encoded;
for (i = 0; i < len - 2; i += 3) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2) |
((int) (string[i + 2] & 0xC0) >> 6)];
*p++ = basis_64[string[i + 2] & 0x3F];
}
if (i < len) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
if (i == (len - 1)) {
*p++ = basis_64[((string[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
*p++ = '\0';
return p - encoded;
}
static int ap_base64encode(char *encoded, const char *string, int len)
{
#ifndef CHARSET_EBCDIC
return ap_base64encode_binary(encoded, (const unsigned char *) string, len);
#else /* CHARSET_EBCDIC */
int i;
char *p;
p = encoded;
for (i = 0; i < len - 2; i += 3) {
*p++ = basis_64[(os_toascii[string[i]] >> 2) & 0x3F];
*p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4) |
((int) (os_toascii[string[i + 1]] & 0xF0) >> 4)];
*p++ = basis_64[((os_toascii[string[i + 1]] & 0xF) << 2) |
((int) (os_toascii[string[i + 2]] & 0xC0) >> 6)];
*p++ = basis_64[os_toascii[string[i + 2]] & 0x3F];
}
if (i < len) {
*p++ = basis_64[(os_toascii[string[i]] >> 2) & 0x3F];
if (i == (len - 1)) {
*p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4) |
((int) (os_toascii[string[i + 1]] & 0xF0) >> 4)];
*p++ = basis_64[((os_toascii[string[i + 1]] & 0xF) << 2)];
}
*p++ = '=';
}
*p++ = '\0';
return p - encoded;
#endif /* CHARSET_EBCDIC */
}
#if 0
int
main( )
{
char *str = "praveen:itisme";
char buff[1000];
char buff1[1000];
ap_base64encode( buff, str, strlen(str) );
printf( "Buff is %s\n", buff );
ap_base64decode( buff1, buff );
return 1;
}
#endif /* 0 */
|