Make sure start_listening_socket does everything, this will lead the way to multiple listen ports.

This commit is contained in:
Jan Vidar Krey
2010-01-21 17:26:27 +01:00
parent c47ea14047
commit 21a5981905
6 changed files with 75 additions and 61 deletions

View File

@@ -27,8 +27,6 @@
#define NET_EVENT_TIMEOUT 0x0001
#define NET_EVENT_READ 0x0002
#define NET_EVENT_WRITE 0x0004
#define NET_EVENT_SOCKERROR 0x1000 /* Socket error, closed */
#define NET_EVENT_CLOSED 0x2000 /* Socket closed */
struct net_connection;
@@ -53,7 +51,6 @@ extern void net_con_callback(struct net_connection* con, int events);
* Close the connection.
* This will ensure a connection is closed properly and will generate a NET_EVENT_DESTROYED event which indicates
* that the con can safely be deleted (or set to NULL).
* @returns 1 if the memory pointed to by con can be freed immediately, or 0 if it needs to go through the NET_EVENT_DESTROYED event.
*/
extern void net_con_close(struct net_connection* con);

View File

@@ -381,11 +381,11 @@ int net_accept(int fd, struct ip_addr_encap* ipaddr)
{
char address[INET6_ADDRSTRLEN+1] = { 0, };
net_address_to_string(AF_INET6, (void*) &addr6->sin6_addr, address, INET6_ADDRSTRLEN+1);
if (!strncmp(address, "::ffff:", 7))
if (strchr(address, '.'))
{
/* Hack to convert IPv6 mapped IPv4 addresses to true IPv4 addresses */
net_string_to_address(AF_INET, address + 7, (void*) &ipaddr->internal_ip_data.in);
ipaddr->af = AF_INET;
net_string_to_address(AF_INET, address, (void*) &ipaddr->internal_ip_data.in);
}
else
{
@@ -526,7 +526,12 @@ const char* net_address_to_string(int af, const void* src, char* dst, socklen_t
return NULL;
#else
return inet_ntop(af, src, dst, cnt);
const char* address = inet_ntop(af, src, dst, cnt);
if (af == AF_INET6 && strncmp(address, "::ffff:", 7) == 0) /* IPv6 mapped IPv4 address. */
{
return &address[7];
}
return address;
#endif
}
@@ -573,9 +578,6 @@ int net_string_to_address(int af, const char* src, void* dst)
#endif
}
const char* net_get_peer_address(int fd)
{
static char address[INET6_ADDRSTRLEN+1];
@@ -598,17 +600,12 @@ const char* net_get_peer_address(int fd)
if (af == AF_INET6)
{
net_address_to_string(af, (void*) &name6->sin6_addr, address, INET6_ADDRSTRLEN);
if (strncmp(address, "::ffff:", 7) == 0) /* IPv6 mapped IPv4 address. */
{
return &address[7];
}
return address;
}
else
{
net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN);
return address;
}
return address;
}
else
{
@@ -619,6 +616,45 @@ const char* net_get_peer_address(int fd)
return "0.0.0.0";
}
const char* net_get_local_address(int fd)
{
static char address[INET6_ADDRSTRLEN+1];
struct sockaddr_storage storage;
struct sockaddr_in6* name6;
struct sockaddr_in* name4;
struct sockaddr* name;
memset(address, 0, INET6_ADDRSTRLEN);
socklen_t namelen = sizeof(struct sockaddr_storage);
memset(&storage, 0, namelen);
name6 = (struct sockaddr_in6*) &storage;
name4 = (struct sockaddr_in*) &storage;
name = (struct sockaddr*) &storage;
if (getsockname(fd, (struct sockaddr*) name, &namelen) != -1)
{
int af = name4->sin_family;
if (af == AF_INET6)
{
net_address_to_string(af, (void*) &name6->sin6_addr, address, INET6_ADDRSTRLEN);
}
else
{
net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN);
}
return address;
}
else
{
net_error_out(fd, "net_get_local_address");
net_stats_add_error();
}
return "0.0.0.0";
}
ssize_t net_recv(int fd, void* buf, size_t len, int flags)
{

View File

@@ -224,6 +224,8 @@ extern int net_is_ipv6_supported();
*/
extern const char* net_get_peer_address(int fd);
extern const char* net_get_local_address(int fd);
/**
* See man(3) inet_ntop.
*/