network.c revision 833ce47e1d1869537c3b9cdedda5ab18103020a9
/* Copyright (c) 1999-2005 Timo Sirainen */
#include "lib.h"
#include "close-keep-errno.h"
#include "fd-set-nonblock.h"
#include "network.h"
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
union sockaddr_union {
struct sockaddr_in sin;
#ifdef HAVE_IPV6
struct sockaddr_in6 sin6;
#endif
};
#ifdef HAVE_IPV6
#else
#endif
{
return 0;
#ifdef HAVE_IPV6
#endif
}
/* copy IP to sockaddr */
static inline void
{
#ifdef HAVE_IPV6
#else
#endif
return;
}
#ifdef HAVE_IPV6
else
#endif
}
static inline void
{
#ifdef HAVE_IPV6
else
#endif
else
}
{
#ifdef HAVE_IPV6
else
#endif
}
{
#ifdef HAVE_IPV6
#endif
return 0;
}
{
union sockaddr_union so;
i_warning("net_connect_ip(): ip->family != my_ip->family");
}
/* create the socket */
if (fd == -1) {
i_error("socket() failed: %m");
return -1;
}
/* set socket options */
/* set our own address */
/* failed, set it back to INADDR_ANY */
return -1;
}
}
/* connect */
#ifndef WIN32
#else
#endif
{
return -1;
}
return fd;
}
int net_connect_unix(const char *path)
{
struct sockaddr_un sa;
/* too long path */
return -1;
}
/* create the socket */
if (fd == -1) {
return -1;
}
/* connect */
return -1;
}
return fd;
}
void net_disconnect(int fd)
{
i_error("net_disconnect() failed: %m");
}
{
}
{
#ifdef TCP_CORK
#else
errno = ENOPROTOOPT;
return -1;
#endif
}
{
}
{
#ifdef HAVE_IPV6
#else
#endif
}
{
union sockaddr_union so;
/* create the socket */
#ifdef HAVE_IPV6
/* IPv6 is not supported by OS */
}
#endif
if (fd == -1) {
i_error("socket() failed: %m");
return -1;
}
/* set socket options */
/* if using IPv6, bind both on the IPv4 and IPv6 addresses */
#ifdef IPV6_V6ONLY
opt = 0;
}
#endif
if (ret < 0) {
if (errno != EADDRINUSE) {
i_error("bind(%s, %u) failed: %m",
}
} else {
/* get the actual port we started listen */
if (ret >= 0) {
/* start listening */
return fd;
if (errno != EADDRINUSE)
i_error("listen() failed: %m");
}
}
/* error */
return -1;
}
{
struct sockaddr_un sa;
int fd;
/* too long path */
return -1;
}
/* create the socket */
if (fd == -1) {
i_error("socket() failed: %m");
return -1;
}
/* bind */
if (errno != EADDRINUSE)
} else {
/* start listening */
return fd;
if (errno != EADDRINUSE)
i_error("listen() failed: %m");
}
return -1;
}
{
union sockaddr_union so;
int ret;
if (ret < 0) {
return -1;
else
return -2;
}
return ret;
}
{
if (ret == 0) {
/* disconnected */
errno = 0;
return -2;
}
if (ret < 0) {
return 0;
/* treat as disconnection */
return -2;
}
}
return ret;
}
{
return 0;
return -2;
return ret;
}
unsigned int *ips_count)
{
/* @UNSAFE */
#ifdef HAVE_IPV6
union sockaddr_union *so;
int host_error;
#else
#endif
int count;
*ips_count = 0;
#ifdef HAVE_IPV6
/* save error to host_error for later use */
if (host_error != 0)
return host_error;
/* get number of IPs */
count++;
count = 0;
}
#else
return h_errno;
/* get number of IPs */
count = 0;
count++;
while (count > 0) {
count--;
}
#endif
return 0;
}
{
union sockaddr_union so;
return -1;
return 0;
}
{
union sockaddr_union so;
return -1;
return 0;
}
{
#ifdef HAVE_IPV6
return NULL;
#else
unsigned long ip4;
return NULL;
return t_strdup_printf("%lu.%lu.%lu.%lu",
(ip4 & 0x000000ff));
#endif
}
{
/* IPv6 */
#ifdef HAVE_IPV6
return -1;
#else
#endif
} else {
/* IPv4 */
return -1;
}
return 0;
}
{
#ifdef HAVE_IPV6
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
if (!IPADDR_IS_V6(src))
return -1;
return -1;
return 0;
#else
return -1;
#endif
}
int net_geterror(int fd)
{
int data;
return -1;
return data;
}
const char *net_gethosterror(int error)
{
#ifdef HAVE_IPV6
return gai_strerror(error);
#else
switch (error) {
case HOST_NOT_FOUND:
return "Host not found";
case NO_ADDRESS:
return "No IP address found for name";
case NO_RECOVERY:
return "A non-recovable name server error occurred";
case TRY_AGAIN:
return "A temporary error on an authoritative name server";
}
/* unknown error */
return NULL;
#endif
}
int net_hosterror_notfound(int error)
{
#ifdef HAVE_IPV6
#ifdef EAI_NODATA /* NODATA is depricated */
#else
#endif
#else
#endif
}
const char *net_getservbyport(unsigned short port)
{
}
bool is_ipv4_address(const char *addr)
{
while (*addr != '\0') {
return FALSE;
addr++;
}
return TRUE;
}
bool is_ipv6_address(const char *addr)
{
while (*addr != '\0') {
return FALSE;
addr++;
}
return TRUE;
}