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