|
Lines 2175-2181
Link Here
|
| 2175 |
} |
2175 |
} |
| 2176 |
|
2176 |
|
| 2177 |
static int |
2177 |
static int |
| 2178 |
channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port, |
2178 |
channel_setup_fwd_listener(int type, const char *listen_addr, u_short *listen_port, |
| 2179 |
const char *host_to_connect, u_short port_to_connect, int gateway_ports) |
2179 |
const char *host_to_connect, u_short port_to_connect, int gateway_ports) |
| 2180 |
{ |
2180 |
{ |
| 2181 |
Channel *c; |
2181 |
Channel *c; |
|
Lines 2238-2244
Link Here
|
| 2238 |
hints.ai_family = IPv4or6; |
2238 |
hints.ai_family = IPv4or6; |
| 2239 |
hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
2239 |
hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
| 2240 |
hints.ai_socktype = SOCK_STREAM; |
2240 |
hints.ai_socktype = SOCK_STREAM; |
| 2241 |
snprintf(strport, sizeof strport, "%d", listen_port); |
2241 |
snprintf(strport, sizeof strport, "%d", (int)*listen_port); |
| 2242 |
if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
2242 |
if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 2243 |
if (addr == NULL) { |
2243 |
if (addr == NULL) { |
| 2244 |
/* This really shouldn't happen */ |
2244 |
/* This really shouldn't happen */ |
|
Lines 2289-2294
Link Here
|
| 2289 |
close(sock); |
2289 |
close(sock); |
| 2290 |
continue; |
2290 |
continue; |
| 2291 |
} |
2291 |
} |
|
|
2292 |
/* If the specified port was 0, get the actual port number */ |
| 2293 |
if (*listen_port == 0) { |
| 2294 |
struct sockaddr_in realport; |
| 2295 |
socklen_t realportsz = (socklen_t)sizeof(realport); |
| 2296 |
if (getsockname(sock, (struct sockaddr *)&realport, &realportsz) < 0) { |
| 2297 |
error("getsockname: %.100s", strerror(errno)); |
| 2298 |
close(sock); |
| 2299 |
continue; |
| 2300 |
} |
| 2301 |
*listen_port = (u_short)ntohs(realport.sin_port); |
| 2302 |
} |
| 2303 |
|
| 2292 |
/* Start listening for connections on the socket. */ |
2304 |
/* Start listening for connections on the socket. */ |
| 2293 |
if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
2305 |
if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
| 2294 |
error("listen: %.100s", strerror(errno)); |
2306 |
error("listen: %.100s", strerror(errno)); |
|
Lines 2301-2312
Link Here
|
| 2301 |
0, "port listener", 1); |
2313 |
0, "port listener", 1); |
| 2302 |
strlcpy(c->path, host, sizeof(c->path)); |
2314 |
strlcpy(c->path, host, sizeof(c->path)); |
| 2303 |
c->host_port = port_to_connect; |
2315 |
c->host_port = port_to_connect; |
| 2304 |
c->listening_port = listen_port; |
2316 |
c->listening_port = *listen_port; |
| 2305 |
success = 1; |
2317 |
success = 1; |
| 2306 |
} |
2318 |
} |
| 2307 |
if (success == 0) |
2319 |
if (success == 0) |
| 2308 |
error("channel_setup_fwd_listener: cannot listen to port: %d", |
2320 |
error("channel_setup_fwd_listener: cannot listen to port: %d", |
| 2309 |
listen_port); |
2321 |
(int)*listen_port); |
| 2310 |
freeaddrinfo(aitop); |
2322 |
freeaddrinfo(aitop); |
| 2311 |
return success; |
2323 |
return success; |
| 2312 |
} |
2324 |
} |
|
Lines 2338-2351
Link Here
|
| 2338 |
const char *host_to_connect, u_short port_to_connect, int gateway_ports) |
2350 |
const char *host_to_connect, u_short port_to_connect, int gateway_ports) |
| 2339 |
{ |
2351 |
{ |
| 2340 |
return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER, |
2352 |
return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER, |
| 2341 |
listen_host, listen_port, host_to_connect, port_to_connect, |
2353 |
listen_host, &listen_port, host_to_connect, port_to_connect, |
| 2342 |
gateway_ports); |
2354 |
gateway_ports); |
| 2343 |
} |
2355 |
} |
| 2344 |
|
2356 |
|
| 2345 |
/* protocol v2 remote port fwd, used by sshd */ |
2357 |
/* protocol v2 remote port fwd, used by sshd */ |
| 2346 |
int |
2358 |
int |
| 2347 |
channel_setup_remote_fwd_listener(const char *listen_address, |
2359 |
channel_setup_remote_fwd_listener(const char *listen_address, |
| 2348 |
u_short listen_port, int gateway_ports) |
2360 |
u_short *listen_port, int gateway_ports) |
| 2349 |
{ |
2361 |
{ |
| 2350 |
return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER, |
2362 |
return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER, |
| 2351 |
listen_address, listen_port, NULL, 0, gateway_ports); |
2363 |
listen_address, listen_port, NULL, 0, gateway_ports); |
|
Lines 2383-2390
Link Here
|
| 2383 |
packet_put_int(listen_port); |
2395 |
packet_put_int(listen_port); |
| 2384 |
packet_send(); |
2396 |
packet_send(); |
| 2385 |
packet_write_wait(); |
2397 |
packet_write_wait(); |
| 2386 |
/* Assume that server accepts the request */ |
2398 |
|
| 2387 |
success = 1; |
2399 |
if (listen_port != 0) |
|
|
2400 |
/* Assume that server accepts the request */ |
| 2401 |
success = 1; |
| 2402 |
else { |
| 2403 |
type = packet_read(); |
| 2404 |
switch(type) { |
| 2405 |
case SSH2_MSG_REQUEST_SUCCESS: |
| 2406 |
success = 1; |
| 2407 |
listen_port = (u_short)packet_get_int(); |
| 2408 |
break; |
| 2409 |
case SSH2_MSG_REQUEST_FAILURE: |
| 2410 |
logit("Warning: Server denied remote port 0 forwarding."); |
| 2411 |
break; |
| 2412 |
default: |
| 2413 |
/* Unknown packet */ |
| 2414 |
packet_disconnect("Protocol error for port 0 forward request:" |
| 2415 |
"received packet typr %d.", type); |
| 2416 |
} |
| 2417 |
} |
| 2418 |
|
| 2388 |
} else { |
2419 |
} else { |
| 2389 |
packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
2420 |
packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
| 2390 |
packet_put_int(listen_port); |
2421 |
packet_put_int(listen_port); |