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