[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; |
---|
| 116 | int fd = -1; |
---|
| 117 | |
---|
| 118 | if (!(sin = gaim_gethostbyname(host, port))) { |
---|
| 119 | g_free(phb); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if ((fd = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) { |
---|
| 124 | g_free(phb); |
---|
| 125 | return -1; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | sock_make_nonblocking(fd); |
---|
[19ac9c5] | 129 | |
---|
| 130 | event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port, fd); |
---|
| 131 | |
---|
[b7d3cc34] | 132 | if (connect(fd, (struct sockaddr *)sin, sizeof(*sin)) < 0) { |
---|
| 133 | if (sockerr_again()) { |
---|
[ba9edaa] | 134 | phb->inpa = b_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb); |
---|
[b7d3cc34] | 135 | phb->fd = fd; |
---|
| 136 | } else { |
---|
| 137 | closesocket(fd); |
---|
| 138 | g_free(phb); |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | return fd; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /* Connecting to HTTP proxies */ |
---|
| 148 | |
---|
| 149 | #define HTTP_GOODSTRING "HTTP/1.0 200 Connection established" |
---|
| 150 | #define HTTP_GOODSTRING2 "HTTP/1.1 200 Connection established" |
---|
| 151 | |
---|
[ba9edaa] | 152 | static gboolean http_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 153 | { |
---|
| 154 | int nlc = 0; |
---|
| 155 | int pos = 0; |
---|
| 156 | struct PHB *phb = data; |
---|
| 157 | char inputline[8192]; |
---|
| 158 | |
---|
[ba9edaa] | 159 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 160 | |
---|
| 161 | while ((pos < sizeof(inputline)-1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
| 162 | if (inputline[pos - 1] == '\n') |
---|
| 163 | nlc++; |
---|
| 164 | else if (inputline[pos - 1] != '\r') |
---|
| 165 | nlc = 0; |
---|
| 166 | } |
---|
| 167 | inputline[pos] = '\0'; |
---|
| 168 | |
---|
| 169 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
| 170 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
| 171 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 172 | g_free(phb->host); |
---|
| 173 | g_free(phb); |
---|
[ba9edaa] | 174 | return FALSE; |
---|
[b7d3cc34] | 175 | } |
---|
| 176 | |
---|
| 177 | close(source); |
---|
| 178 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 179 | g_free(phb->host); |
---|
| 180 | g_free(phb); |
---|
[ba9edaa] | 181 | |
---|
| 182 | return FALSE; |
---|
[b7d3cc34] | 183 | } |
---|
| 184 | |
---|
[ba9edaa] | 185 | static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 186 | { |
---|
| 187 | char cmd[384]; |
---|
| 188 | struct PHB *phb = data; |
---|
| 189 | unsigned int len; |
---|
| 190 | int error = ETIMEDOUT; |
---|
| 191 | if (phb->inpa > 0) |
---|
[ba9edaa] | 192 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 193 | len = sizeof(error); |
---|
| 194 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 195 | close(source); |
---|
| 196 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 197 | g_free(phb->host); |
---|
| 198 | g_free(phb); |
---|
[ba9edaa] | 199 | return FALSE; |
---|
[b7d3cc34] | 200 | } |
---|
[701acdd4] | 201 | sock_make_blocking(source); |
---|
[b7d3cc34] | 202 | |
---|
| 203 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
| 204 | phb->host, phb->port); |
---|
| 205 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 206 | close(source); |
---|
| 207 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 208 | g_free(phb->host); |
---|
| 209 | g_free(phb); |
---|
[ba9edaa] | 210 | return FALSE; |
---|
[b7d3cc34] | 211 | } |
---|
| 212 | |
---|
| 213 | if (proxyuser && *proxyuser) { |
---|
| 214 | char *t1, *t2; |
---|
| 215 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
| 216 | t2 = tobase64(t1); |
---|
| 217 | g_free(t1); |
---|
| 218 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
| 219 | g_free(t2); |
---|
| 220 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
| 221 | close(source); |
---|
| 222 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 223 | g_free(phb->host); |
---|
| 224 | g_free(phb); |
---|
[ba9edaa] | 225 | return FALSE; |
---|
[b7d3cc34] | 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
| 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 | |
---|
[ba9edaa] | 238 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, http_canread, phb); |
---|
| 239 | |
---|
| 240 | return FALSE; |
---|
[b7d3cc34] | 241 | } |
---|
| 242 | |
---|
[c3ffa45] | 243 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 244 | { |
---|
| 245 | phb->host = g_strdup(host); |
---|
| 246 | phb->port = port; |
---|
| 247 | phb->proxy_func = http_canwrite; |
---|
| 248 | phb->proxy_data = phb; |
---|
| 249 | |
---|
| 250 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | /* Connecting to SOCKS4 proxies */ |
---|
| 255 | |
---|
[ba9edaa] | 256 | static gboolean s4_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 257 | { |
---|
| 258 | unsigned char packet[12]; |
---|
| 259 | struct PHB *phb = data; |
---|
| 260 | |
---|
[ba9edaa] | 261 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 262 | |
---|
| 263 | memset(packet, 0, sizeof(packet)); |
---|
| 264 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
| 265 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 266 | g_free(phb->host); |
---|
| 267 | g_free(phb); |
---|
[ba9edaa] | 268 | return FALSE; |
---|
[b7d3cc34] | 269 | } |
---|
| 270 | |
---|
| 271 | close(source); |
---|
| 272 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 273 | g_free(phb->host); |
---|
| 274 | g_free(phb); |
---|
[ba9edaa] | 275 | |
---|
| 276 | return FALSE; |
---|
[b7d3cc34] | 277 | } |
---|
| 278 | |
---|
[ba9edaa] | 279 | static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 280 | { |
---|
| 281 | unsigned char packet[12]; |
---|
| 282 | struct hostent *hp; |
---|
| 283 | struct PHB *phb = data; |
---|
| 284 | unsigned int len; |
---|
| 285 | int error = ETIMEDOUT; |
---|
| 286 | if (phb->inpa > 0) |
---|
[ba9edaa] | 287 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 288 | len = sizeof(error); |
---|
| 289 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 290 | close(source); |
---|
| 291 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 292 | g_free(phb->host); |
---|
| 293 | g_free(phb); |
---|
[ba9edaa] | 294 | return FALSE; |
---|
[b7d3cc34] | 295 | } |
---|
[701acdd4] | 296 | sock_make_blocking(source); |
---|
[b7d3cc34] | 297 | |
---|
| 298 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
| 299 | if (!(hp = gethostbyname(phb->host))) { |
---|
| 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 | } |
---|
| 306 | |
---|
| 307 | packet[0] = 4; |
---|
| 308 | packet[1] = 1; |
---|
| 309 | packet[2] = phb->port >> 8; |
---|
| 310 | packet[3] = phb->port & 0xff; |
---|
| 311 | packet[4] = (unsigned char)(hp->h_addr_list[0])[0]; |
---|
| 312 | packet[5] = (unsigned char)(hp->h_addr_list[0])[1]; |
---|
| 313 | packet[6] = (unsigned char)(hp->h_addr_list[0])[2]; |
---|
| 314 | packet[7] = (unsigned char)(hp->h_addr_list[0])[3]; |
---|
| 315 | packet[8] = 0; |
---|
| 316 | if (write(source, packet, 9) != 9) { |
---|
| 317 | close(source); |
---|
| 318 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 319 | g_free(phb->host); |
---|
| 320 | g_free(phb); |
---|
[ba9edaa] | 321 | return FALSE; |
---|
[b7d3cc34] | 322 | } |
---|
| 323 | |
---|
[ba9edaa] | 324 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s4_canread, phb); |
---|
| 325 | |
---|
| 326 | return FALSE; |
---|
[b7d3cc34] | 327 | } |
---|
| 328 | |
---|
[c3ffa45] | 329 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 330 | { |
---|
| 331 | phb->host = g_strdup(host); |
---|
| 332 | phb->port = port; |
---|
| 333 | phb->proxy_func = s4_canwrite; |
---|
| 334 | phb->proxy_data = phb; |
---|
| 335 | |
---|
| 336 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | /* Connecting to SOCKS5 proxies */ |
---|
| 341 | |
---|
[ba9edaa] | 342 | static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 343 | { |
---|
| 344 | unsigned char buf[512]; |
---|
| 345 | struct PHB *phb = data; |
---|
| 346 | |
---|
[ba9edaa] | 347 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 348 | |
---|
| 349 | if (read(source, buf, 10) < 10) { |
---|
| 350 | close(source); |
---|
| 351 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 352 | g_free(phb->host); |
---|
| 353 | g_free(phb); |
---|
[ba9edaa] | 354 | return FALSE; |
---|
[b7d3cc34] | 355 | } |
---|
| 356 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
| 357 | close(source); |
---|
| 358 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 359 | g_free(phb->host); |
---|
| 360 | g_free(phb); |
---|
[ba9edaa] | 361 | return FALSE; |
---|
[b7d3cc34] | 362 | } |
---|
| 363 | |
---|
| 364 | phb->func(phb->data, source, GAIM_INPUT_READ); |
---|
| 365 | g_free(phb->host); |
---|
| 366 | g_free(phb); |
---|
[ba9edaa] | 367 | |
---|
| 368 | return FALSE; |
---|
[b7d3cc34] | 369 | } |
---|
| 370 | |
---|
| 371 | static void s5_sendconnect(gpointer data, gint source) |
---|
| 372 | { |
---|
| 373 | unsigned char buf[512]; |
---|
| 374 | struct PHB *phb = data; |
---|
| 375 | int hlen = strlen(phb->host); |
---|
[ba9edaa] | 376 | |
---|
[b7d3cc34] | 377 | buf[0] = 0x05; |
---|
| 378 | buf[1] = 0x01; /* CONNECT */ |
---|
| 379 | buf[2] = 0x00; /* reserved */ |
---|
| 380 | buf[3] = 0x03; /* address type -- host name */ |
---|
| 381 | buf[4] = hlen; |
---|
| 382 | memcpy(buf + 5, phb->host, hlen); |
---|
| 383 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
| 384 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
| 385 | |
---|
| 386 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
| 387 | close(source); |
---|
| 388 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 389 | g_free(phb->host); |
---|
| 390 | g_free(phb); |
---|
| 391 | return; |
---|
| 392 | } |
---|
| 393 | |
---|
[ba9edaa] | 394 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb); |
---|
[b7d3cc34] | 395 | } |
---|
| 396 | |
---|
[ba9edaa] | 397 | static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 398 | { |
---|
| 399 | unsigned char buf[512]; |
---|
| 400 | struct PHB *phb = data; |
---|
| 401 | |
---|
[ba9edaa] | 402 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 403 | |
---|
| 404 | if (read(source, buf, 2) < 2) { |
---|
| 405 | close(source); |
---|
| 406 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 407 | g_free(phb->host); |
---|
| 408 | g_free(phb); |
---|
[ba9edaa] | 409 | return FALSE; |
---|
[b7d3cc34] | 410 | } |
---|
| 411 | |
---|
| 412 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
| 413 | close(source); |
---|
| 414 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 415 | g_free(phb->host); |
---|
| 416 | g_free(phb); |
---|
[ba9edaa] | 417 | return FALSE; |
---|
[b7d3cc34] | 418 | } |
---|
| 419 | |
---|
| 420 | s5_sendconnect(phb, source); |
---|
[ba9edaa] | 421 | |
---|
| 422 | return FALSE; |
---|
[b7d3cc34] | 423 | } |
---|
| 424 | |
---|
[ba9edaa] | 425 | static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 426 | { |
---|
| 427 | unsigned char buf[512]; |
---|
| 428 | struct PHB *phb = data; |
---|
| 429 | |
---|
[ba9edaa] | 430 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 431 | |
---|
| 432 | if (read(source, buf, 2) < 2) { |
---|
| 433 | close(source); |
---|
| 434 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 435 | g_free(phb->host); |
---|
| 436 | g_free(phb); |
---|
[ba9edaa] | 437 | return FALSE; |
---|
[b7d3cc34] | 438 | } |
---|
| 439 | |
---|
| 440 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
| 441 | close(source); |
---|
| 442 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 443 | g_free(phb->host); |
---|
| 444 | g_free(phb); |
---|
[ba9edaa] | 445 | return FALSE; |
---|
[b7d3cc34] | 446 | } |
---|
| 447 | |
---|
| 448 | if (buf[1] == 0x02) { |
---|
| 449 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
| 450 | buf[0] = 0x01; /* version 1 */ |
---|
| 451 | buf[1] = i; |
---|
| 452 | memcpy(buf + 2, proxyuser, i); |
---|
| 453 | buf[2 + i] = j; |
---|
| 454 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
| 455 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
| 456 | close(source); |
---|
| 457 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 458 | g_free(phb->host); |
---|
| 459 | g_free(phb); |
---|
[ba9edaa] | 460 | return FALSE; |
---|
[b7d3cc34] | 461 | } |
---|
| 462 | |
---|
[ba9edaa] | 463 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_readauth, phb); |
---|
[b7d3cc34] | 464 | } else { |
---|
| 465 | s5_sendconnect(phb, source); |
---|
| 466 | } |
---|
[ba9edaa] | 467 | |
---|
| 468 | return FALSE; |
---|
[b7d3cc34] | 469 | } |
---|
| 470 | |
---|
[ba9edaa] | 471 | static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
[b7d3cc34] | 472 | { |
---|
| 473 | unsigned char buf[512]; |
---|
| 474 | int i; |
---|
| 475 | struct PHB *phb = data; |
---|
| 476 | unsigned int len; |
---|
| 477 | int error = ETIMEDOUT; |
---|
| 478 | if (phb->inpa > 0) |
---|
[ba9edaa] | 479 | b_event_remove(phb->inpa); |
---|
[b7d3cc34] | 480 | len = sizeof(error); |
---|
| 481 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
| 482 | close(source); |
---|
| 483 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 484 | g_free(phb->host); |
---|
| 485 | g_free(phb); |
---|
[ba9edaa] | 486 | return FALSE; |
---|
[b7d3cc34] | 487 | } |
---|
[701acdd4] | 488 | sock_make_blocking(source); |
---|
[b7d3cc34] | 489 | |
---|
| 490 | i = 0; |
---|
| 491 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
| 492 | if (proxyuser[0]) { |
---|
| 493 | buf[1] = 0x02; /* two methods */ |
---|
| 494 | buf[2] = 0x00; /* no authentication */ |
---|
| 495 | buf[3] = 0x02; /* username/password authentication */ |
---|
| 496 | i = 4; |
---|
| 497 | } else { |
---|
| 498 | buf[1] = 0x01; |
---|
| 499 | buf[2] = 0x00; |
---|
| 500 | i = 3; |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | if (write(source, buf, i) < i) { |
---|
| 504 | close(source); |
---|
| 505 | phb->func(phb->data, -1, GAIM_INPUT_READ); |
---|
| 506 | g_free(phb->host); |
---|
| 507 | g_free(phb); |
---|
[ba9edaa] | 508 | return FALSE; |
---|
[b7d3cc34] | 509 | } |
---|
| 510 | |
---|
[ba9edaa] | 511 | phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread, phb); |
---|
| 512 | |
---|
| 513 | return FALSE; |
---|
[b7d3cc34] | 514 | } |
---|
| 515 | |
---|
[c3ffa45] | 516 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
[b7d3cc34] | 517 | { |
---|
| 518 | phb->host = g_strdup(host); |
---|
| 519 | phb->port = port; |
---|
| 520 | phb->proxy_func = s5_canwrite; |
---|
| 521 | phb->proxy_data = phb; |
---|
| 522 | |
---|
| 523 | return( proxy_connect_none( proxyhost, proxyport, phb ) ); |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | |
---|
| 527 | /* Export functions */ |
---|
| 528 | |
---|
[ba9edaa] | 529 | int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) |
---|
[b7d3cc34] | 530 | { |
---|
| 531 | struct PHB *phb; |
---|
| 532 | |
---|
| 533 | if (!host || !port || (port == -1) || !func || strlen(host) > 128) { |
---|
| 534 | return -1; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | phb = g_new0(struct PHB, 1); |
---|
| 538 | phb->func = func; |
---|
| 539 | phb->data = data; |
---|
| 540 | |
---|
| 541 | if ((proxytype == PROXY_NONE) || !proxyhost || !proxyhost[0] || !proxyport || (proxyport == -1)) |
---|
| 542 | return proxy_connect_none(host, port, phb); |
---|
| 543 | else if (proxytype == PROXY_HTTP) |
---|
| 544 | return proxy_connect_http(host, port, phb); |
---|
| 545 | else if (proxytype == PROXY_SOCKS4) |
---|
| 546 | return proxy_connect_socks4(host, port, phb); |
---|
| 547 | else if (proxytype == PROXY_SOCKS5) |
---|
| 548 | return proxy_connect_socks5(host, port, phb); |
---|
| 549 | |
---|
| 550 | if (phb->host) g_free(phb); |
---|
| 551 | g_free(phb); |
---|
| 552 | return -1; |
---|
| 553 | } |
---|