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