version 1.245, 2014/02/02 03:44:31 |
version 1.246, 2014/02/06 22:21:01 |
|
|
ssh_create_socket(int privileged, struct addrinfo *ai) |
ssh_create_socket(int privileged, struct addrinfo *ai) |
{ |
{ |
int sock, r, gaierr; |
int sock, r, gaierr; |
struct addrinfo hints, *res; |
struct addrinfo hints, *res = NULL; |
|
|
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
if (sock < 0) { |
if (sock < 0) { |
|
|
if (options.bind_address == NULL && !privileged) |
if (options.bind_address == NULL && !privileged) |
return sock; |
return sock; |
|
|
memset(&hints, 0, sizeof(hints)); |
if (options.bind_address) { |
hints.ai_family = ai->ai_family; |
memset(&hints, 0, sizeof(hints)); |
hints.ai_socktype = ai->ai_socktype; |
hints.ai_family = ai->ai_family; |
hints.ai_protocol = ai->ai_protocol; |
hints.ai_socktype = ai->ai_socktype; |
hints.ai_flags = AI_PASSIVE; |
hints.ai_protocol = ai->ai_protocol; |
gaierr = getaddrinfo(options.bind_address, NULL, &hints, &res); |
hints.ai_flags = AI_PASSIVE; |
if (gaierr) { |
gaierr = getaddrinfo(options.bind_address, NULL, &hints, &res); |
error("getaddrinfo: %s: %s", options.bind_address, |
if (gaierr) { |
ssh_gai_strerror(gaierr)); |
error("getaddrinfo: %s: %s", options.bind_address, |
close(sock); |
ssh_gai_strerror(gaierr)); |
return -1; |
close(sock); |
|
return -1; |
|
} |
} |
} |
/* |
/* |
* If we are running as root and want to connect to a privileged |
* If we are running as root and want to connect to a privileged |
|
|
*/ |
*/ |
if (privileged) { |
if (privileged) { |
PRIV_START; |
PRIV_START; |
r = bindresvport_sa(sock, res->ai_addr); |
r = bindresvport_sa(sock, res ? res->ai_addr : NULL); |
PRIV_END; |
PRIV_END; |
if (r < 0) { |
if (r < 0) { |
error("bindresvport_sa: af=%d %s", ai->ai_family, |
error("bindresvport_sa: af=%d %s", ai->ai_family, |
|
|
return -1; |
return -1; |
} |
} |
} |
} |
freeaddrinfo(res); |
if (res != NULL) |
|
freeaddrinfo(res); |
return sock; |
return sock; |
} |
} |
|
|