[b7d3cc34] | 1 | /* |
---|
| 2 | * gaim |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
---|
| 5 | * Copyright (C) 2002-2004, Wilmer van der Gaast, Jelmer Vernooij |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | * (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /* this is a little piece of code to handle proxy connection */ |
---|
| 24 | /* it is intended to : 1st handle http proxy, using the CONNECT command |
---|
| 25 | , 2nd provide an easy way to add socks support */ |
---|
| 26 | |
---|
| 27 | #define BITLBEE_CORE |
---|
| 28 | #include <stdio.h> |
---|
| 29 | #include <stdlib.h> |
---|
| 30 | #include <string.h> |
---|
| 31 | #include <sys/types.h> |
---|
| 32 | #ifndef _WIN32 |
---|
| 33 | #include <sys/socket.h> |
---|
| 34 | #include <netdb.h> |
---|
| 35 | #include <netinet/in.h> |
---|
| 36 | #include <arpa/inet.h> |
---|
| 37 | #include <unistd.h> |
---|
| 38 | #else |
---|
| 39 | #include "sock.h" |
---|
| 40 | #define ETIMEDOUT WSAETIMEDOUT |
---|
| 41 | #define EINPROGRESS WSAEINPROGRESS |
---|
| 42 | #endif |
---|
| 43 | #include <fcntl.h> |
---|
| 44 | #include <errno.h> |
---|
| 45 | #include "nogaim.h" |
---|
| 46 | #include "proxy.h" |
---|
| 47 | |
---|
| 48 | #define GAIM_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) |
---|
| 49 | #define GAIM_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL) |
---|
| 50 | #define GAIM_ERR_COND (G_IO_HUP | G_IO_ERR | G_IO_NVAL) |
---|
| 51 | |
---|
| 52 | char proxyhost[128] = ""; |
---|
| 53 | int proxyport = 0; |
---|
| 54 | int proxytype = PROXY_NONE; |
---|
| 55 | char proxyuser[128] = ""; |
---|
| 56 | char proxypass[128] = ""; |
---|
| 57 | |
---|
| 58 | struct PHB { |
---|
| 59 | GaimInputFunction func, proxy_func; |
---|
| 60 | gpointer data, proxy_data; |
---|
| 61 | char *host; |
---|
| 62 | int port; |
---|
| 63 | int fd; |
---|
| 64 | gint inpa; |
---|
| 65 | }; |
---|
| 66 | |
---|
| 67 | typedef struct _GaimIOClosure { |
---|
| 68 | GaimInputFunction function; |
---|
| 69 | guint result; |
---|
| 70 | gpointer data; |
---|
| 71 | } GaimIOClosure; |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | |
---|
[c3ffa45] | 75 | static struct sockaddr_in *gaim_gethostbyname(const char *host, int port) |
---|
[b7d3cc34] | 76 | { |
---|
| 77 | static struct sockaddr_in sin; |
---|
| 78 | |
---|
| 79 | if (!inet_aton(host, &sin.sin_addr)) { |
---|
| 80 | struct hostent *hp; |
---|
| 81 | if (!(hp = gethostbyname(host))) { |
---|
| 82 | return NULL; |
---|
| 83 | } |
---|
| 84 | memset(&sin, 0, sizeof(struct sockaddr_in)); |
---|
| 85 | memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); |
---|
| 86 | sin.sin_family = hp->h_addrtype; |
---|
| 87 | } else |
---|
| 88 | sin.sin_family = AF_INET; |
---|
| 89 | sin.sin_port = htons(port); |
---|
| 90 | |
---|
| 91 | return &sin; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | static void gaim_io_destroy(gpointer data) |
---|
| 95 | { |
---|
| 96 | g_free(data); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
---|
| 100 | { |
---|
| 101 | GaimIOClosure *closure = data; |
---|
| 102 | GaimInputCondition gaim_cond = 0; |
---|
| 103 | |
---|
| 104 | if (condition & GAIM_READ_COND) |
---|
| 105 | gaim_cond |= GAIM_INPUT_READ; |
---|
| 106 | if (condition & GAIM_WRITE_COND) |
---|
| 107 | gaim_cond |= GAIM_INPUT_WRITE; |
---|
| 108 | // if (condition & GAIM_ERR_COND) |
---|
| 109 | // fprintf( stderr, "ERROR! fd=%d\n", g_io_channel_unix_get_fd( source ) ); |
---|
| 110 | |
---|
| 111 | closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); |
---|
| 112 | |
---|
| 113 | return TRUE; |
---|
| 114 | } |
---|
| 115 | |
---|
[6caa56a] | 116 | static void gaim_io_connected(gpointer data, gint source, GaimInputCondition cond) |
---|
[b7d3cc34] | 117 | { |
---|
| 118 | struct PHB *phb = data; |
---|
| 119 | unsigned int len; |
---|
| 120 | int error = ETIMEDOUT; |
---|
| 121 | len = sizeof(error); |
---|
| 122 | |
---|
| 123 | #ifndef _WIN32 |
---|
| 124 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 125 | closesocket(source); |
---|
| 126 | gaim_input_remove(phb->inpa); |
---|
| 127 | if( phb->proxy_func ) |
---|
| 128 | phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ); |
---|
| 129 | else { |
---|
| 130 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 131 | g_free(phb); |
---|
| 132 | } |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | #endif |
---|
[701acdd4] | 136 | sock_make_blocking(source); |
---|
[b7d3cc34] | 137 | gaim_input_remove(phb->inpa); |
---|
| 138 | if( phb->proxy_func ) |
---|
| 139 | phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ); |
---|
| 140 | else { |
---|
| 141 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 142 | g_free(phb); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
[c3ffa45] | 146 | static int proxy_connect_none(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 147 | { |
---|
| 148 | struct sockaddr_in *sin; |
---|
| 149 | int fd = -1; |
---|
| 150 | |
---|
| 151 | if (!(sin = gaim_gethostbyname(host, port))) { |
---|
| 152 | g_free(phb); |
---|
| 153 | return -1; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) { |
---|
| 157 | g_free(phb); |
---|
| 158 | return -1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | sock_make_nonblocking(fd); |
---|
| 162 | |
---|
| 163 | if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) { |
---|
| 164 | if (sockerr_again()) { |
---|
[6caa56a] | 165 | phb->inpa = gaim_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb); |
---|
[b7d3cc34] | 166 | phb->fd = fd; |
---|
| 167 | } else { |
---|
| 168 | closesocket(fd); |
---|
| 169 | g_free(phb); |
---|
| 170 | return -1; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | return fd; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | /* Connecting to HTTP proxies */ |
---|
| 179 | |
---|
| 180 | #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established" |
---|
| 181 | #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" |
---|
| 182 | |
---|
| 183 | static void http_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
| 184 | { |
---|
| 185 | int nlc = 0; |
---|
| 186 | int pos = 0; |
---|
| 187 | struct PHB *phb = data; |
---|
| 188 | char inputline[8192]; |
---|
| 189 | |
---|
| 190 | gaim_input_remove(phb->inpa); |
---|
| 191 | |
---|
| 192 | while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
| 193 | if (inputline[pos - 1] == '\n') |
---|
| 194 | nlc++; |
---|
| 195 | else if (inputline[pos - 1] != '\r') |
---|
| 196 | nlc = 0; |
---|
| 197 | } |
---|
| 198 | inputline[pos] = '\0'; |
---|
| 199 | |
---|
| 200 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
| 201 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
| 202 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 203 | g_free(phb->host); |
---|
| 204 | g_free(phb); |
---|
| 205 | return; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | close(source); |
---|
| 209 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 210 | g_free(phb->host); |
---|
| 211 | g_free(phb); |
---|
| 212 | return; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | static void http_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
| 216 | { |
---|
| 217 | char cmd[384]; |
---|
| 218 | struct PHB *phb = data; |
---|
| 219 | unsigned int len; |
---|
| 220 | int error = ETIMEDOUT; |
---|
| 221 | if (phb->inpa > 0) |
---|
| 222 | gaim_input_remove(phb->inpa); |
---|
| 223 | len = sizeof(error); |
---|
| 224 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 225 | close(source); |
---|
| 226 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 227 | g_free(phb->host); |
---|
| 228 | g_free(phb); |
---|
| 229 | return; |
---|
| 230 | } |
---|
[701acdd4] | 231 | sock_make_blocking(source); |
---|
[b7d3cc34] | 232 | |
---|
| 233 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
| 234 | phb->host, phb->port); |
---|
| 235 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 236 | close(source); |
---|
| 237 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 238 | g_free(phb->host); |
---|
| 239 | g_free(phb); |
---|
| 240 | return; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | if (proxyuser && *proxyuser) { |
---|
| 244 | char *t1, *t2; |
---|
| 245 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
| 246 | t2 = tobase64(t1); |
---|
| 247 | g_free(t1); |
---|
| 248 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
| 249 | g_free(t2); |
---|
| 250 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 251 | close(source); |
---|
| 252 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 253 | g_free(phb->host); |
---|
| 254 | g_free(phb); |
---|
| 255 | return; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
| 260 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 261 | close(source); |
---|
| 262 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 263 | g_free(phb->host); |
---|
| 264 | g_free(phb); |
---|
| 265 | return; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, http_canread, phb); |
---|
| 269 | } |
---|
| 270 | |
---|
[c3ffa45] | 271 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 272 | { |
---|
| 273 | phb->host = g_strdup(host); |
---|
| 274 | phb->port = port; |
---|
| 275 | phb->proxy_func = http_canwrite; |
---|
| 276 | phb->proxy_data = phb; |
---|
| 277 | |
---|
| 278 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | /* Connecting to SOCKS4 proxies */ |
---|
| 283 | |
---|
| 284 | static void s4_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
| 285 | { |
---|
| 286 | unsigned char packet[12]; |
---|
| 287 | struct PHB *phb = data; |
---|
| 288 | |
---|
| 289 | gaim_input_remove(phb->inpa); |
---|
| 290 | |
---|
| 291 | memset(packet, 0, sizeof(packet)); |
---|
| 292 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
| 293 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 294 | g_free(phb->host); |
---|
| 295 | g_free(phb); |
---|
| 296 | return; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | close(source); |
---|
| 300 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 301 | g_free(phb->host); |
---|
| 302 | g_free(phb); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | static void s4_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
| 306 | { |
---|
| 307 | unsigned char packet[12]; |
---|
| 308 | struct hostent *hp; |
---|
| 309 | struct PHB *phb = data; |
---|
| 310 | unsigned int len; |
---|
| 311 | int error = ETIMEDOUT; |
---|
| 312 | if (phb->inpa > 0) |
---|
| 313 | gaim_input_remove(phb->inpa); |
---|
| 314 | len = sizeof(error); |
---|
| 315 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 316 | close(source); |
---|
| 317 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 318 | g_free(phb->host); |
---|
| 319 | g_free(phb); |
---|
| 320 | return; |
---|
| 321 | } |
---|
[701acdd4] | 322 | sock_make_blocking(source); |
---|
[b7d3cc34] | 323 | |
---|
| 324 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
| 325 | if (!(hp = gethostbyname(phb->host))) { |
---|
| 326 | close(source); |
---|
| 327 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 328 | g_free(phb->host); |
---|
| 329 | g_free(phb); |
---|
| 330 | return; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | packet[0] = 4; |
---|
| 334 | packet[1] = 1; |
---|
| 335 | packet[2] = phb->port >> 8; |
---|
| 336 | packet[3] = phb->port & 0xff; |
---|
| 337 | packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; |
---|
| 338 | packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; |
---|
| 339 | packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; |
---|
| 340 | packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; |
---|
| 341 | packet[8] = 0; |
---|
| 342 | if (write(source, packet, 9) != 9) { |
---|
| 343 | close(source); |
---|
| 344 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 345 | g_free(phb->host); |
---|
| 346 | g_free(phb); |
---|
| 347 | return; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s4_canread, phb); |
---|
| 351 | } |
---|
| 352 | |
---|
[c3ffa45] | 353 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 354 | { |
---|
| 355 | phb->host = g_strdup(host); |
---|
| 356 | phb->port = port; |
---|
| 357 | phb->proxy_func = s4_canwrite; |
---|
| 358 | phb->proxy_data = phb; |
---|
| 359 | |
---|
| 360 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | |
---|
| 364 | /* Connecting to SOCKS5 proxies */ |
---|
| 365 | |
---|
| 366 | static void s5_canread_again(gpointer data, gint source, GaimInputCondition cond) |
---|
| 367 | { |
---|
| 368 | unsigned char buf[512]; |
---|
| 369 | struct PHB *phb = data; |
---|
| 370 | |
---|
| 371 | gaim_input_remove(phb->inpa); |
---|
| 372 | |
---|
| 373 | if (read(source, buf, 10) < 10) { |
---|
| 374 | close(source); |
---|
| 375 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 376 | g_free(phb->host); |
---|
| 377 | g_free(phb); |
---|
| 378 | return; |
---|
| 379 | } |
---|
| 380 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
| 381 | close(source); |
---|
| 382 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 383 | g_free(phb->host); |
---|
| 384 | g_free(phb); |
---|
| 385 | return; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 389 | g_free(phb->host); |
---|
| 390 | g_free(phb); |
---|
| 391 | return; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | static void s5_sendconnect(gpointer data, gint source) |
---|
| 395 | { |
---|
| 396 | unsigned char buf[512]; |
---|
| 397 | struct PHB *phb = data; |
---|
| 398 | int hlen = strlen(phb->host); |
---|
| 399 | |
---|
| 400 | buf[0] = 0x05; |
---|
| 401 | buf[1] = 0x01; /* CONNECT */ |
---|
| 402 | buf[2] = 0x00; /* reserved */ |
---|
| 403 | buf[3] = 0x03; /* address type -- host name */ |
---|
| 404 | buf[4] = hlen; |
---|
| 405 | memcpy(buf + 5, phb->host, hlen); |
---|
| 406 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
| 407 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
| 408 | |
---|
| 409 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
| 410 | close(source); |
---|
| 411 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 412 | g_free(phb->host); |
---|
| 413 | g_free(phb); |
---|
| 414 | return; |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb); |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | static void s5_readauth(gpointer data, gint source, GaimInputCondition cond) |
---|
| 421 | { |
---|
| 422 | unsigned char buf[512]; |
---|
| 423 | struct PHB *phb = data; |
---|
| 424 | |
---|
| 425 | gaim_input_remove(phb->inpa); |
---|
| 426 | |
---|
| 427 | if (read(source, buf, 2) < 2) { |
---|
| 428 | close(source); |
---|
| 429 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 430 | g_free(phb->host); |
---|
| 431 | g_free(phb); |
---|
| 432 | return; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
| 436 | close(source); |
---|
| 437 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 438 | g_free(phb->host); |
---|
| 439 | g_free(phb); |
---|
| 440 | return; |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | s5_sendconnect(phb, source); |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | static void s5_canread(gpointer data, gint source, GaimInputCondition cond) |
---|
| 447 | { |
---|
| 448 | unsigned char buf[512]; |
---|
| 449 | struct PHB *phb = data; |
---|
| 450 | |
---|
| 451 | gaim_input_remove(phb->inpa); |
---|
| 452 | |
---|
| 453 | if (read(source, buf, 2) < 2) { |
---|
| 454 | close(source); |
---|
| 455 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 456 | g_free(phb->host); |
---|
| 457 | g_free(phb); |
---|
| 458 | return; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
| 462 | close(source); |
---|
| 463 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 464 | g_free(phb->host); |
---|
| 465 | g_free(phb); |
---|
| 466 | return; |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | if (buf[1] == 0x02) { |
---|
| 470 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
| 471 | buf[0] = 0x01; /* version 1 */ |
---|
| 472 | buf[1] = i; |
---|
| 473 | memcpy(buf + 2, proxyuser, i); |
---|
| 474 | buf[2 + i] = j; |
---|
| 475 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
| 476 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
| 477 | close(source); |
---|
| 478 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 479 | g_free(phb->host); |
---|
| 480 | g_free(phb); |
---|
| 481 | return; |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_readauth, phb); |
---|
| 485 | } else { |
---|
| 486 | s5_sendconnect(phb, source); |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | static void s5_canwrite(gpointer data, gint source, GaimInputCondition cond) |
---|
| 491 | { |
---|
| 492 | unsigned char buf[512]; |
---|
| 493 | int i; |
---|
| 494 | struct PHB *phb = data; |
---|
| 495 | unsigned int len; |
---|
| 496 | int error = ETIMEDOUT; |
---|
| 497 | if (phb->inpa > 0) |
---|
| 498 | gaim_input_remove(phb->inpa); |
---|
| 499 | len = sizeof(error); |
---|
| 500 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 501 | close(source); |
---|
| 502 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 503 | g_free(phb->host); |
---|
| 504 | g_free(phb); |
---|
| 505 | return; |
---|
| 506 | } |
---|
[701acdd4] | 507 | sock_make_blocking(source); |
---|
[b7d3cc34] | 508 | |
---|
| 509 | i = 0; |
---|
| 510 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
| 511 | if (proxyuser[0]) { |
---|
| 512 | buf[1] = 0x02; /* two methods */ |
---|
| 513 | buf[2] = 0x00; /* no authentication */ |
---|
| 514 | buf[3] = 0x02; /* username/password authentication */ |
---|
| 515 | i = 4; |
---|
| 516 | } else { |
---|
| 517 | buf[1] = 0x01; |
---|
| 518 | buf[2] = 0x00; |
---|
| 519 | i = 3; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | if (write(source, buf, i) < i) { |
---|
| 523 | close(source); |
---|
| 524 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 525 | g_free(phb->host); |
---|
| 526 | g_free(phb); |
---|
| 527 | return; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | phb->inpa = gaim_input_add(source, GAIM_INPUT_READ, s5_canread, phb); |
---|
| 531 | } |
---|
| 532 | |
---|
[c3ffa45] | 533 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 534 | { |
---|
| 535 | phb->host = g_strdup(host); |
---|
| 536 | phb->port = port; |
---|
| 537 | phb->proxy_func = s5_canwrite; |
---|
| 538 | phb->proxy_data = phb; |
---|
| 539 | |
---|
| 540 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | |
---|
| 544 | /* Export functions */ |
---|
| 545 | |
---|
| 546 | gint gaim_input_add(gint source, GaimInputCondition condition, GaimInputFunction function, gpointer data) |
---|
| 547 | { |
---|
| 548 | GaimIOClosure *closure = g_new0(GaimIOClosure, 1); |
---|
| 549 | GIOChannel *channel; |
---|
| 550 | GIOCondition cond = 0; |
---|
| 551 | |
---|
| 552 | closure->function = function; |
---|
| 553 | closure->data = data; |
---|
| 554 | |
---|
| 555 | if (condition & GAIM_INPUT_READ) |
---|
| 556 | cond |= GAIM_READ_COND; |
---|
| 557 | if (condition & GAIM_INPUT_WRITE) |
---|
| 558 | cond |= GAIM_WRITE_COND; |
---|
| 559 | |
---|
| 560 | channel = g_io_channel_unix_new(source); |
---|
| 561 | closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
---|
| 562 | gaim_io_invoke, closure, gaim_io_destroy); |
---|
| 563 | |
---|
| 564 | g_io_channel_unref(channel); |
---|
| 565 | return closure->result; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | void gaim_input_remove(gint tag) |
---|
| 569 | { |
---|
| 570 | if (tag > 0) |
---|
| 571 | g_source_remove(tag); |
---|
| 572 | } |
---|
| 573 | |
---|
[c3ffa45] | 574 | int proxy_connect(const char *host, int port, GaimInputFunction func, gpointer data) |
---|
[b7d3cc34] | 575 | { |
---|
| 576 | struct PHB *phb; |
---|
| 577 | |
---|
| 578 | if (!host || !port || (port == -1) || !func || strlen(host) > 128) { |
---|
| 579 | return -1; |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | phb = g_new0(struct PHB, 1); |
---|
| 583 | phb->func = func; |
---|
| 584 | phb->data = data; |
---|
| 585 | |
---|
| 586 | #ifndef _WIN32 |
---|
| 587 | sethostent(1); |
---|
| 588 | #endif |
---|
| 589 | |
---|
| 590 | if ((proxytype == PROXY_NONE) || !proxyhost || !proxyhost[0] || !proxyport || (proxyport == -1)) |
---|
| 591 | return proxy_connect_none(host, port, phb); |
---|
| 592 | else if (proxytype == PROXY_HTTP) |
---|
| 593 | return proxy_connect_http(host, port, phb); |
---|
| 594 | else if (proxytype == PROXY_SOCKS4) |
---|
| 595 | return proxy_connect_socks4(host, port, phb); |
---|
| 596 | else if (proxytype == PROXY_SOCKS5) |
---|
| 597 | return proxy_connect_socks5(host, port, phb); |
---|
| 598 | |
---|
| 599 | if (phb->host) g_free(phb); |
---|
| 600 | g_free(phb); |
---|
| 601 | return -1; |
---|
| 602 | } |
---|