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