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