[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 | #define BITLBEE_CORE |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | #include <sys/types.h> |
---|
| 28 | #ifndef _WIN32 |
---|
| 29 | #include <sys/socket.h> |
---|
| 30 | #include <netdb.h> |
---|
| 31 | #include <netinet/in.h> |
---|
| 32 | #include <arpa/inet.h> |
---|
| 33 | #include <unistd.h> |
---|
| 34 | #else |
---|
| 35 | #include "sock.h" |
---|
| 36 | #define ETIMEDOUT WSAETIMEDOUT |
---|
| 37 | #define EINPROGRESS WSAEINPROGRESS |
---|
| 38 | #endif |
---|
| 39 | #include <fcntl.h> |
---|
| 40 | #include <errno.h> |
---|
| 41 | #include "nogaim.h" |
---|
| 42 | #include "proxy.h" |
---|
[7ed3199] | 43 | #include "base64.h" |
---|
[b7d3cc34] | 44 | |
---|
| 45 | char proxyhost[128] = ""; |
---|
| 46 | int proxyport = 0; |
---|
| 47 | int proxytype = PROXY_NONE; |
---|
| 48 | char proxyuser[128] = ""; |
---|
| 49 | char proxypass[128] = ""; |
---|
| 50 | |
---|
| 51 | struct PHB { |
---|
[ba9edaa] | 52 | b_event_handler func, proxy_func; |
---|
[b7d3cc34] | 53 | gpointer data, proxy_data; |
---|
| 54 | char *host; |
---|
| 55 | int port; |
---|
| 56 | int fd; |
---|
| 57 | gint inpa; |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | |
---|
[c3ffa45] | 62 | static struct sockaddr_in *gaim_gethostbyname(const char *host, int port) |
---|
[b7d3cc34] | 63 | { |
---|
| 64 | static struct sockaddr_in sin; |
---|
| 65 | |
---|
| 66 | if (!inet_aton(host, &sin.sin_addr)) { |
---|
| 67 | struct hostent *hp; |
---|
| 68 | if (!(hp = gethostbyname(host))) { |
---|
| 69 | return NULL; |
---|
| 70 | } |
---|
| 71 | memset(&sin, 0, sizeof(struct sockaddr_in)); |
---|
| 72 | memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); |
---|
| 73 | sin.sin_family = hp->h_addrtype; |
---|
| 74 | } else |
---|
| 75 | sin.sin_family = AF_INET; |
---|
| 76 | sin.sin_port = htons(port); |
---|
| 77 | |
---|
| 78 | return &sin; |
---|
| 79 | } |
---|
| 80 | |
---|
[ba9edaa] | 81 | static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 82 | { |
---|
| 83 | struct PHB *phb = data; |
---|
| 84 | unsigned int len; |
---|
| 85 | int error = ETIMEDOUT; |
---|
| 86 | len = sizeof(error); |
---|
| 87 | |
---|
| 88 | #ifndef _WIN32 |
---|
| 89 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 90 | closesocket(source); |
---|
[ba9edaa] | 91 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 92 | if( phb->proxy_func ) |
---|
| 93 | phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ); |
---|
| 94 | else { |
---|
| 95 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 96 | g_free(phb); |
---|
| 97 | } |
---|
[ba9edaa] | 98 | return FALSE; |
---|
[b7d3cc34] | 99 | } |
---|
| 100 | #endif |
---|
[701acdd4] | 101 | sock_make_blocking(source); |
---|
[ba9edaa] | 102 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 103 | if( phb->proxy_func ) |
---|
| 104 | phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ); |
---|
| 105 | else { |
---|
| 106 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 107 | g_free(phb); |
---|
| 108 | } |
---|
[ba9edaa] | 109 | |
---|
| 110 | return FALSE; |
---|
[b7d3cc34] | 111 | } |
---|
| 112 | |
---|
[c3ffa45] | 113 | static int proxy_connect_none(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 114 | { |
---|
| 115 | struct sockaddr_in *sin; |
---|
[aefaac3a] | 116 | struct sockaddr_in me; |
---|
[b7d3cc34] | 117 | int fd = -1; |
---|
| 118 | |
---|
| 119 | if (!(sin = gaim_gethostbyname(host, port))) { |
---|
| 120 | g_free(phb); |
---|
| 121 | return -1; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) { |
---|
| 125 | g_free(phb); |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | sock_make_nonblocking(fd); |
---|
[19ac9c5] | 130 | |
---|
[aefaac3a] | 131 | if( global.conf->iface_out ) |
---|
| 132 | { |
---|
| 133 | me.sin_family = AF_INET; |
---|
| 134 | me.sin_port = 0; |
---|
| 135 | me.sin_addr.s_addr = inet_addr( global.conf->iface_out ); |
---|
| 136 | |
---|
| 137 | if( bind( fd, (struct sockaddr *) &me, sizeof( me ) ) != 0 ) |
---|
| 138 | event_debug( "bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out ); |
---|
| 139 | } |
---|
| 140 | |
---|
[19ac9c5] | 141 | event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port, fd); |
---|
| 142 | |
---|
[6f7ac17] | 143 | if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0 && !sockerr_again()) { |
---|
| 144 | closesocket(fd); |
---|
| 145 | g_free(phb); |
---|
| 146 | |
---|
| 147 | return -1; |
---|
| 148 | } else { |
---|
| 149 | phb->inpa = b_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb); |
---|
| 150 | phb->fd = fd; |
---|
| 151 | |
---|
| 152 | return fd; |
---|
[b7d3cc34] | 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | /* Connecting to HTTP proxies */ |
---|
| 158 | |
---|
| 159 | #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established" |
---|
| 160 | #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" |
---|
| 161 | |
---|
[ba9edaa] | 162 | static gboolean http_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 163 | { |
---|
| 164 | int nlc = 0; |
---|
| 165 | int pos = 0; |
---|
| 166 | struct PHB *phb = data; |
---|
| 167 | char inputline[8192]; |
---|
| 168 | |
---|
[ba9edaa] | 169 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 170 | |
---|
| 171 | while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
| 172 | if (inputline[pos - 1] == '\n') |
---|
| 173 | nlc++; |
---|
| 174 | else if (inputline[pos - 1] != '\r') |
---|
| 175 | nlc = 0; |
---|
| 176 | } |
---|
| 177 | inputline[pos] = '\0'; |
---|
| 178 | |
---|
| 179 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
| 180 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
| 181 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 182 | g_free(phb->host); |
---|
| 183 | g_free(phb); |
---|
[ba9edaa] | 184 | return FALSE; |
---|
[b7d3cc34] | 185 | } |
---|
| 186 | |
---|
| 187 | close(source); |
---|
| 188 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 189 | g_free(phb->host); |
---|
| 190 | g_free(phb); |
---|
[ba9edaa] | 191 | |
---|
| 192 | return FALSE; |
---|
[b7d3cc34] | 193 | } |
---|
| 194 | |
---|
[ba9edaa] | 195 | static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 196 | { |
---|
| 197 | char cmd[384]; |
---|
| 198 | struct PHB *phb = data; |
---|
| 199 | unsigned int len; |
---|
| 200 | int error = ETIMEDOUT; |
---|
| 201 | if (phb->inpa > 0) |
---|
[ba9edaa] | 202 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 203 | len = sizeof(error); |
---|
| 204 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 205 | close(source); |
---|
| 206 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 207 | g_free(phb->host); |
---|
| 208 | g_free(phb); |
---|
[ba9edaa] | 209 | return FALSE; |
---|
[b7d3cc34] | 210 | } |
---|
[701acdd4] | 211 | sock_make_blocking(source); |
---|
[b7d3cc34] | 212 | |
---|
| 213 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
| 214 | phb->host, phb->port); |
---|
| 215 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 216 | close(source); |
---|
| 217 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 218 | g_free(phb->host); |
---|
| 219 | g_free(phb); |
---|
[ba9edaa] | 220 | return FALSE; |
---|
[b7d3cc34] | 221 | } |
---|
| 222 | |
---|
[f618a4a] | 223 | if (strlen(proxyuser) > 0) { |
---|
[b7d3cc34] | 224 | char *t1, *t2; |
---|
| 225 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
| 226 | t2 = tobase64(t1); |
---|
| 227 | g_free(t1); |
---|
| 228 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
| 229 | g_free(t2); |
---|
| 230 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 231 | close(source); |
---|
| 232 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 233 | g_free(phb->host); |
---|
| 234 | g_free(phb); |
---|
[ba9edaa] | 235 | return FALSE; |
---|
[b7d3cc34] | 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
| 240 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 241 | close(source); |
---|
| 242 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 243 | g_free(phb->host); |
---|
| 244 | g_free(phb); |
---|
[ba9edaa] | 245 | return FALSE; |
---|
[b7d3cc34] | 246 | } |
---|
| 247 | |
---|
[ba9edaa] | 248 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, http_canread, phb); |
---|
| 249 | |
---|
| 250 | return FALSE; |
---|
[b7d3cc34] | 251 | } |
---|
| 252 | |
---|
[c3ffa45] | 253 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 254 | { |
---|
| 255 | phb->host = g_strdup(host); |
---|
| 256 | phb->port = port; |
---|
| 257 | phb->proxy_func = http_canwrite; |
---|
| 258 | phb->proxy_data = phb; |
---|
| 259 | |
---|
| 260 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | /* Connecting to SOCKS4 proxies */ |
---|
| 265 | |
---|
[ba9edaa] | 266 | static gboolean s4_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 267 | { |
---|
| 268 | unsigned char packet[12]; |
---|
| 269 | struct PHB *phb = data; |
---|
| 270 | |
---|
[ba9edaa] | 271 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 272 | |
---|
| 273 | memset(packet, 0, sizeof(packet)); |
---|
| 274 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
| 275 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 276 | g_free(phb->host); |
---|
| 277 | g_free(phb); |
---|
[ba9edaa] | 278 | return FALSE; |
---|
[b7d3cc34] | 279 | } |
---|
| 280 | |
---|
| 281 | close(source); |
---|
| 282 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 283 | g_free(phb->host); |
---|
| 284 | g_free(phb); |
---|
[ba9edaa] | 285 | |
---|
| 286 | return FALSE; |
---|
[b7d3cc34] | 287 | } |
---|
| 288 | |
---|
[ba9edaa] | 289 | static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 290 | { |
---|
| 291 | unsigned char packet[12]; |
---|
| 292 | struct hostent *hp; |
---|
| 293 | struct PHB *phb = data; |
---|
| 294 | unsigned int len; |
---|
| 295 | int error = ETIMEDOUT; |
---|
| 296 | if (phb->inpa > 0) |
---|
[ba9edaa] | 297 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 298 | len = sizeof(error); |
---|
| 299 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 300 | close(source); |
---|
| 301 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 302 | g_free(phb->host); |
---|
| 303 | g_free(phb); |
---|
[ba9edaa] | 304 | return FALSE; |
---|
[b7d3cc34] | 305 | } |
---|
[701acdd4] | 306 | sock_make_blocking(source); |
---|
[b7d3cc34] | 307 | |
---|
| 308 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
| 309 | if (!(hp = gethostbyname(phb->host))) { |
---|
| 310 | close(source); |
---|
| 311 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 312 | g_free(phb->host); |
---|
| 313 | g_free(phb); |
---|
[ba9edaa] | 314 | return FALSE; |
---|
[b7d3cc34] | 315 | } |
---|
| 316 | |
---|
| 317 | packet[0] = 4; |
---|
| 318 | packet[1] = 1; |
---|
| 319 | packet[2] = phb->port >> 8; |
---|
| 320 | packet[3] = phb->port & 0xff; |
---|
| 321 | packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; |
---|
| 322 | packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; |
---|
| 323 | packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; |
---|
| 324 | packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; |
---|
| 325 | packet[8] = 0; |
---|
| 326 | if (write(source, packet, 9) != 9) { |
---|
| 327 | close(source); |
---|
| 328 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 329 | g_free(phb->host); |
---|
| 330 | g_free(phb); |
---|
[ba9edaa] | 331 | return FALSE; |
---|
[b7d3cc34] | 332 | } |
---|
| 333 | |
---|
[ba9edaa] | 334 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s4_canread, phb); |
---|
| 335 | |
---|
| 336 | return FALSE; |
---|
[b7d3cc34] | 337 | } |
---|
| 338 | |
---|
[c3ffa45] | 339 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 340 | { |
---|
| 341 | phb->host = g_strdup(host); |
---|
| 342 | phb->port = port; |
---|
| 343 | phb->proxy_func = s4_canwrite; |
---|
| 344 | phb->proxy_data = phb; |
---|
| 345 | |
---|
| 346 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | |
---|
| 350 | /* Connecting to SOCKS5 proxies */ |
---|
| 351 | |
---|
[ba9edaa] | 352 | static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 353 | { |
---|
| 354 | unsigned char buf[512]; |
---|
| 355 | struct PHB *phb = data; |
---|
| 356 | |
---|
[ba9edaa] | 357 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 358 | |
---|
| 359 | if (read(source, buf, 10) < 10) { |
---|
| 360 | close(source); |
---|
| 361 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 362 | g_free(phb->host); |
---|
| 363 | g_free(phb); |
---|
[ba9edaa] | 364 | return FALSE; |
---|
[b7d3cc34] | 365 | } |
---|
| 366 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
| 367 | close(source); |
---|
| 368 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 369 | g_free(phb->host); |
---|
| 370 | g_free(phb); |
---|
[ba9edaa] | 371 | return FALSE; |
---|
[b7d3cc34] | 372 | } |
---|
| 373 | |
---|
| 374 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 375 | g_free(phb->host); |
---|
| 376 | g_free(phb); |
---|
[ba9edaa] | 377 | |
---|
| 378 | return FALSE; |
---|
[b7d3cc34] | 379 | } |
---|
| 380 | |
---|
| 381 | static void s5_sendconnect(gpointer data, gint source) |
---|
| 382 | { |
---|
| 383 | unsigned char buf[512]; |
---|
| 384 | struct PHB *phb = data; |
---|
| 385 | int hlen = strlen(phb->host); |
---|
[ba9edaa] | 386 | |
---|
[b7d3cc34] | 387 | buf[0] = 0x05; |
---|
| 388 | buf[1] = 0x01; /* CONNECT */ |
---|
| 389 | buf[2] = 0x00; /* reserved */ |
---|
| 390 | buf[3] = 0x03; /* address type -- host name */ |
---|
| 391 | buf[4] = hlen; |
---|
| 392 | memcpy(buf + 5, phb->host, hlen); |
---|
| 393 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
| 394 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
| 395 | |
---|
| 396 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
| 397 | close(source); |
---|
| 398 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 399 | g_free(phb->host); |
---|
| 400 | g_free(phb); |
---|
| 401 | return; |
---|
| 402 | } |
---|
| 403 | |
---|
[ba9edaa] | 404 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb); |
---|
[b7d3cc34] | 405 | } |
---|
| 406 | |
---|
[ba9edaa] | 407 | static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 408 | { |
---|
| 409 | unsigned char buf[512]; |
---|
| 410 | struct PHB *phb = data; |
---|
| 411 | |
---|
[ba9edaa] | 412 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 413 | |
---|
| 414 | if (read(source, buf, 2) < 2) { |
---|
| 415 | close(source); |
---|
| 416 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 417 | g_free(phb->host); |
---|
| 418 | g_free(phb); |
---|
[ba9edaa] | 419 | return FALSE; |
---|
[b7d3cc34] | 420 | } |
---|
| 421 | |
---|
| 422 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
| 423 | close(source); |
---|
| 424 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 425 | g_free(phb->host); |
---|
| 426 | g_free(phb); |
---|
[ba9edaa] | 427 | return FALSE; |
---|
[b7d3cc34] | 428 | } |
---|
| 429 | |
---|
| 430 | s5_sendconnect(phb, source); |
---|
[ba9edaa] | 431 | |
---|
| 432 | return FALSE; |
---|
[b7d3cc34] | 433 | } |
---|
| 434 | |
---|
[ba9edaa] | 435 | static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 436 | { |
---|
| 437 | unsigned char buf[512]; |
---|
| 438 | struct PHB *phb = data; |
---|
| 439 | |
---|
[ba9edaa] | 440 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 441 | |
---|
| 442 | if (read(source, buf, 2) < 2) { |
---|
| 443 | close(source); |
---|
| 444 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 445 | g_free(phb->host); |
---|
| 446 | g_free(phb); |
---|
[ba9edaa] | 447 | return FALSE; |
---|
[b7d3cc34] | 448 | } |
---|
| 449 | |
---|
| 450 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
| 451 | close(source); |
---|
| 452 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 453 | g_free(phb->host); |
---|
| 454 | g_free(phb); |
---|
[ba9edaa] | 455 | return FALSE; |
---|
[b7d3cc34] | 456 | } |
---|
| 457 | |
---|
| 458 | if (buf[1] == 0x02) { |
---|
| 459 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
| 460 | buf[0] = 0x01; /* version 1 */ |
---|
| 461 | buf[1] = i; |
---|
| 462 | memcpy(buf + 2, proxyuser, i); |
---|
| 463 | buf[2 + i] = j; |
---|
| 464 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
| 465 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
| 466 | close(source); |
---|
| 467 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 468 | g_free(phb->host); |
---|
| 469 | g_free(phb); |
---|
[ba9edaa] | 470 | return FALSE; |
---|
[b7d3cc34] | 471 | } |
---|
| 472 | |
---|
[ba9edaa] | 473 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_readauth, phb); |
---|
[b7d3cc34] | 474 | } else { |
---|
| 475 | s5_sendconnect(phb, source); |
---|
| 476 | } |
---|
[ba9edaa] | 477 | |
---|
| 478 | return FALSE; |
---|
[b7d3cc34] | 479 | } |
---|
| 480 | |
---|
[ba9edaa] | 481 | static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 482 | { |
---|
| 483 | unsigned char buf[512]; |
---|
| 484 | int i; |
---|
| 485 | struct PHB *phb = data; |
---|
| 486 | unsigned int len; |
---|
| 487 | int error = ETIMEDOUT; |
---|
| 488 | if (phb->inpa > 0) |
---|
[ba9edaa] | 489 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 490 | len = sizeof(error); |
---|
| 491 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 492 | close(source); |
---|
| 493 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 494 | g_free(phb->host); |
---|
| 495 | g_free(phb); |
---|
[ba9edaa] | 496 | return FALSE; |
---|
[b7d3cc34] | 497 | } |
---|
[701acdd4] | 498 | sock_make_blocking(source); |
---|
[b7d3cc34] | 499 | |
---|
| 500 | i = 0; |
---|
| 501 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
| 502 | if (proxyuser[0]) { |
---|
| 503 | buf[1] = 0x02; /* two methods */ |
---|
| 504 | buf[2] = 0x00; /* no authentication */ |
---|
| 505 | buf[3] = 0x02; /* username/password authentication */ |
---|
| 506 | i = 4; |
---|
| 507 | } else { |
---|
| 508 | buf[1] = 0x01; |
---|
| 509 | buf[2] = 0x00; |
---|
| 510 | i = 3; |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | if (write(source, buf, i) < i) { |
---|
| 514 | close(source); |
---|
| 515 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 516 | g_free(phb->host); |
---|
| 517 | g_free(phb); |
---|
[ba9edaa] | 518 | return FALSE; |
---|
[b7d3cc34] | 519 | } |
---|
| 520 | |
---|
[ba9edaa] | 521 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread, phb); |
---|
| 522 | |
---|
| 523 | return FALSE; |
---|
[b7d3cc34] | 524 | } |
---|
| 525 | |
---|
[c3ffa45] | 526 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 527 | { |
---|
| 528 | phb->host = g_strdup(host); |
---|
| 529 | phb->port = port; |
---|
| 530 | phb->proxy_func = s5_canwrite; |
---|
| 531 | phb->proxy_data = phb; |
---|
| 532 | |
---|
| 533 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | |
---|
| 537 | /* Export functions */ |
---|
| 538 | |
---|
[ba9edaa] | 539 | int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) |
---|
[b7d3cc34] | 540 | { |
---|
| 541 | struct PHB *phb; |
---|
| 542 | |
---|
[58a1449] | 543 | if (!host || port <= 0 || !func || strlen(host) > 128) { |
---|
[b7d3cc34] | 544 | return -1; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | phb = g_new0(struct PHB, 1); |
---|
| 548 | phb->func = func; |
---|
| 549 | phb->data = data; |
---|
| 550 | |
---|
[58a1449] | 551 | if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0) |
---|
[b7d3cc34] | 552 | return proxy_connect_none(host, port, phb); |
---|
| 553 | else if (proxytype == PROXY_HTTP) |
---|
| 554 | return proxy_connect_http(host, port, phb); |
---|
| 555 | else if (proxytype == PROXY_SOCKS4) |
---|
| 556 | return proxy_connect_socks4(host, port, phb); |
---|
| 557 | else if (proxytype == PROXY_SOCKS5) |
---|
| 558 | return proxy_connect_socks5(host, port, phb); |
---|
| 559 | |
---|
| 560 | g_free(phb); |
---|
| 561 | return -1; |
---|
| 562 | } |
---|