version 1.138, 2003/04/08 20:21:29 |
version 1.139, 2003/04/14 14:17:50 |
|
|
* Creates a (possibly privileged) socket for use as the ssh connection. |
* Creates a (possibly privileged) socket for use as the ssh connection. |
*/ |
*/ |
static int |
static int |
ssh_create_socket(int privileged, int family) |
ssh_create_socket(int privileged, struct addrinfo *ai) |
{ |
{ |
int sock, gaierr; |
int sock, gaierr; |
struct addrinfo hints, *res; |
struct addrinfo hints, *res; |
|
|
if (privileged) { |
if (privileged) { |
int p = IPPORT_RESERVED - 1; |
int p = IPPORT_RESERVED - 1; |
PRIV_START; |
PRIV_START; |
sock = rresvport_af(&p, family); |
sock = rresvport_af(&p, ai->ai_family); |
PRIV_END; |
PRIV_END; |
if (sock < 0) |
if (sock < 0) |
error("rresvport: af=%d %.100s", family, strerror(errno)); |
error("rresvport: af=%d %.100s", ai->ai_family, |
|
strerror(errno)); |
else |
else |
debug("Allocated local port %d.", p); |
debug("Allocated local port %d.", p); |
return sock; |
return sock; |
} |
} |
sock = socket(family, SOCK_STREAM, 0); |
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
if (sock < 0) |
if (sock < 0) |
error("socket: %.100s", strerror(errno)); |
error("socket: %.100s", strerror(errno)); |
|
|
|
|
return sock; |
return sock; |
|
|
memset(&hints, 0, sizeof(hints)); |
memset(&hints, 0, sizeof(hints)); |
hints.ai_family = family; |
hints.ai_family = ai->ai_family; |
hints.ai_socktype = SOCK_STREAM; |
hints.ai_socktype = ai->ai_socktype; |
|
hints.ai_protocol = ai->ai_protocol; |
hints.ai_flags = AI_PASSIVE; |
hints.ai_flags = AI_PASSIVE; |
gaierr = getaddrinfo(options.bind_address, "0", &hints, &res); |
gaierr = getaddrinfo(options.bind_address, "0", &hints, &res); |
if (gaierr) { |
if (gaierr) { |
|
|
host, ntop, strport); |
host, ntop, strport); |
|
|
/* Create a socket for connecting. */ |
/* Create a socket for connecting. */ |
sock = ssh_create_socket(needpriv, ai->ai_family); |
sock = ssh_create_socket(needpriv, ai); |
if (sock < 0) |
if (sock < 0) |
/* Any error is already output */ |
/* Any error is already output */ |
continue; |
continue; |