|
|
@ -1,7 +1,7 @@
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) Jay Sorg 2004-2012
|
|
|
|
* Copyright (C) Jay Sorg 2004-2013
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@ -427,29 +427,42 @@ g_tcp_set_keepalive(int sck)
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* returns a newly created socket or -1 on error */
|
|
|
|
/* returns a newly created socket or -1 on error */
|
|
|
|
|
|
|
|
/* in win32 a socket is an unsigned int, in linux, its an int */
|
|
|
|
int APP_CC
|
|
|
|
int APP_CC
|
|
|
|
g_tcp_socket(void)
|
|
|
|
g_tcp_socket(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
int rv;
|
|
|
|
int rv;
|
|
|
|
int option_value;
|
|
|
|
int option_value;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
int option_len;
|
|
|
|
int option_len;
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
int option_value;
|
|
|
|
|
|
|
|
unsigned int option_len;
|
|
|
|
unsigned int option_len;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* in win32 a socket is an unsigned int, in linux, its an int */
|
|
|
|
#if !defined(NO_ARPA_INET_H_IP6)
|
|
|
|
rv = (int)socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
rv = (int)socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
rv = (int)socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
if (rv < 0)
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(NO_ARPA_INET_H_IP6)
|
|
|
|
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
|
|
|
|
if (getsockopt(rv, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&option_value,
|
|
|
|
|
|
|
|
&option_len) == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (option_value != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
option_value = 0;
|
|
|
|
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
|
|
|
|
setsockopt(rv, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&option_value,
|
|
|
|
|
|
|
|
option_len);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
|
|
|
|
|
|
|
|
if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value,
|
|
|
|
if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value,
|
|
|
|
&option_len) == 0)
|
|
|
|
&option_len) == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -494,17 +507,18 @@ g_tcp_local_socket(void)
|
|
|
|
void APP_CC
|
|
|
|
void APP_CC
|
|
|
|
g_tcp_close(int sck)
|
|
|
|
g_tcp_close(int sck)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char ip[256] ;
|
|
|
|
char ip[256];
|
|
|
|
|
|
|
|
|
|
|
|
if (sck == 0)
|
|
|
|
if (sck == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#if defined(_WIN32)
|
|
|
|
closesocket(sck);
|
|
|
|
closesocket(sck);
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
g_write_ip_address(sck,ip,255);
|
|
|
|
g_write_ip_address(sck, ip, 255);
|
|
|
|
log_message(LOG_LEVEL_INFO,"An established connection closed to endpoint: %s", ip);
|
|
|
|
log_message(LOG_LEVEL_INFO, "An established connection closed to "
|
|
|
|
|
|
|
|
"endpoint: %s", ip);
|
|
|
|
close(sck);
|
|
|
|
close(sck);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -514,34 +528,55 @@ g_tcp_close(int sck)
|
|
|
|
int APP_CC
|
|
|
|
int APP_CC
|
|
|
|
g_tcp_connect(int sck, const char *address, const char *port)
|
|
|
|
g_tcp_connect(int sck, const char *address, const char *port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct sockaddr_in s;
|
|
|
|
int res = 0;
|
|
|
|
struct hostent *h;
|
|
|
|
struct addrinfo p;
|
|
|
|
|
|
|
|
struct addrinfo *h = (struct addrinfo *)NULL;
|
|
|
|
g_memset(&s, 0, sizeof(struct sockaddr_in));
|
|
|
|
struct addrinfo *rp = (struct addrinfo *)NULL;
|
|
|
|
s.sin_family = AF_INET;
|
|
|
|
|
|
|
|
s.sin_port = htons((tui16)atoi(port));
|
|
|
|
/* initialize (zero out) local variables: */
|
|
|
|
s.sin_addr.s_addr = inet_addr(address);
|
|
|
|
g_memset(&p, 0, sizeof(struct addrinfo));
|
|
|
|
|
|
|
|
|
|
|
|
if (s.sin_addr.s_addr == INADDR_NONE)
|
|
|
|
/* in IPv6-enabled environments, set the AI_V4MAPPED
|
|
|
|
|
|
|
|
* flag in ai_flags and specify ai_family=AF_INET6 in
|
|
|
|
|
|
|
|
* order to ensure that getaddrinfo() returns any
|
|
|
|
|
|
|
|
* available IPv4-mapped addresses in case the target
|
|
|
|
|
|
|
|
* host does not have a true IPv6 address:
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
p.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
p.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
|
|
|
#if !defined(NO_ARPA_INET_H_IP6)
|
|
|
|
|
|
|
|
p.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED;
|
|
|
|
|
|
|
|
p.ai_family = AF_INET6;
|
|
|
|
|
|
|
|
if (g_strcmp(address, "127.0.0.1") == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
res = getaddrinfo("::1", port, &p, &h);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
h = gethostbyname(address);
|
|
|
|
res = getaddrinfo(address, port, &p, &h);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (h != 0)
|
|
|
|
#else
|
|
|
|
|
|
|
|
p.ai_flags = AI_ADDRCONFIG;
|
|
|
|
|
|
|
|
p.ai_family = AF_INET;
|
|
|
|
|
|
|
|
res = getaddrinfo(address, port, &p, &h);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (res > -1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (h != NULL)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (h->h_name != 0)
|
|
|
|
for (rp = h; rp != NULL; rp = rp->ai_next)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (h->h_addr_list != 0)
|
|
|
|
rp = h;
|
|
|
|
|
|
|
|
res = connect(sck, (struct sockaddr *)(rp->ai_addr),
|
|
|
|
|
|
|
|
rp->ai_addrlen);
|
|
|
|
|
|
|
|
if (res != -1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ((*(h->h_addr_list)) != 0)
|
|
|
|
break; /* Success */
|
|
|
|
{
|
|
|
|
|
|
|
|
s.sin_addr.s_addr = *((int *)(*(h->h_addr_list)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
return connect(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/*****************************************************************************/
|
|
|
@ -578,23 +613,117 @@ g_tcp_set_non_blocking(int sck)
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
/* return boolean */
|
|
|
|
|
|
|
|
static int APP_CC
|
|
|
|
|
|
|
|
address_match(const char *address, struct addrinfo *j)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct sockaddr_in *ipv4_in;
|
|
|
|
|
|
|
|
struct sockaddr_in6 *ipv6_in;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (address == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address[0] == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_strcmp(address, "0.0.0.0") == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((g_strcmp(address, "127.0.0.1") == 0) ||
|
|
|
|
|
|
|
|
(g_strcmp(address, "::1") == 0) ||
|
|
|
|
|
|
|
|
(g_strcmp(address, "localhost") == 0))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (j->ai_addr != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (j->ai_addr->sa_family == AF_INET)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ipv4_in = (struct sockaddr_in *) (j->ai_addr);
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, "127.0.0.1", &(ipv4_in->sin_addr)))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j->ai_addr->sa_family == AF_INET6)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ipv6_in = (struct sockaddr_in6 *) (j->ai_addr);
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, "::1", &(ipv6_in->sin6_addr)))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j->ai_addr != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (j->ai_addr->sa_family == AF_INET)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ipv4_in = (struct sockaddr_in *) (j->ai_addr);
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, address, &(ipv4_in->sin_addr)))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j->ai_addr->sa_family == AF_INET6)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ipv6_in = (struct sockaddr_in6 *) (j->ai_addr);
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, address, &(ipv6_in->sin6_addr)))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
/* returns error, zero is good */
|
|
|
|
|
|
|
|
static int APP_CC
|
|
|
|
|
|
|
|
g_tcp_bind_flags(int sck, const char *port, const char *address, int flags)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
struct addrinfo *i;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res = -1;
|
|
|
|
|
|
|
|
g_memset(&hints, 0, sizeof(hints));
|
|
|
|
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
|
|
|
|
hints.ai_flags = flags;
|
|
|
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
|
|
|
error = getaddrinfo(NULL, port, &hints, &i);
|
|
|
|
|
|
|
|
if (error == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
while ((i != 0) && (res < 0))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (address_match(address, i))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
res = bind(sck, i->ai_addr, i->ai_addrlen);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
i = i->ai_next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* returns error, zero is good */
|
|
|
|
/* returns error, zero is good */
|
|
|
|
int APP_CC
|
|
|
|
int APP_CC
|
|
|
|
g_tcp_bind(int sck, char *port)
|
|
|
|
g_tcp_bind(int sck, const char *port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct sockaddr_in s;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
|
|
|
|
memset(&s, 0, sizeof(struct sockaddr_in));
|
|
|
|
flags = AI_ADDRCONFIG | AI_PASSIVE;
|
|
|
|
s.sin_family = AF_INET;
|
|
|
|
return g_tcp_bind_flags(sck, port, 0, flags);
|
|
|
|
s.sin_port = htons((tui16)atoi(port));
|
|
|
|
|
|
|
|
s.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
return bind(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/*****************************************************************************/
|
|
|
|
int APP_CC
|
|
|
|
int APP_CC
|
|
|
|
g_tcp_local_bind(int sck, char *port)
|
|
|
|
g_tcp_local_bind(int sck, const char *port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
@ -611,21 +740,12 @@ g_tcp_local_bind(int sck, char *port)
|
|
|
|
/*****************************************************************************/
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* returns error, zero is good */
|
|
|
|
/* returns error, zero is good */
|
|
|
|
int APP_CC
|
|
|
|
int APP_CC
|
|
|
|
g_tcp_bind_address(int sck, char *port, const char *address)
|
|
|
|
g_tcp_bind_address(int sck, const char *port, const char *address)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct sockaddr_in s;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
|
|
|
|
memset(&s, 0, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
s.sin_family = AF_INET;
|
|
|
|
|
|
|
|
s.sin_port = htons((tui16)atoi(port));
|
|
|
|
|
|
|
|
s.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inet_aton(address, &s.sin_addr) < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return -1; /* bad address */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bind(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_in));
|
|
|
|
flags = AI_ADDRCONFIG | AI_PASSIVE;
|
|
|
|
|
|
|
|
return g_tcp_bind_flags(sck, port, address, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/*****************************************************************************/
|
|
|
@ -2017,7 +2137,7 @@ g_htoi(char *str)
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/*****************************************************************************/
|
|
|
|
int APP_CC
|
|
|
|
int APP_CC
|
|
|
|
g_pos(char *str, const char *to_find)
|
|
|
|
g_pos(const char *str, const char *to_find)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char *pp;
|
|
|
|
char *pp;
|
|
|
|
|
|
|
|
|
|
|
|