network.h revision 70f7524d9adedaee65ac3709ecf31487878540dc
52N/A#ifndef __NETWORK_H
52N/A#define __NETWORK_H
292N/A
52N/A#ifndef WIN32
52N/A# include <sys/socket.h>
695N/A# include <netinet/in.h>
52N/A# include <netdb.h>
52N/A# include <arpa/inet.h>
52N/A#endif
52N/A
292N/A#ifdef HAVE_SOCKS_H
292N/A#include <socks.h>
292N/A#endif
292N/A
52N/A#ifndef AF_INET6
52N/A# ifdef PF_INET6
52N/A# define AF_INET6 PF_INET6
292N/A# else
292N/A# define AF_INET6 10
292N/A# endif
292N/A#endif
292N/A
292N/Astruct _IPADDR {
292N/A unsigned short family;
292N/A#ifdef HAVE_IPV6
292N/A struct in6_addr ip;
292N/A#else
292N/A struct in_addr ip;
52N/A#endif
52N/A};
52N/A
52N/A/* maxmimum string length of IP address */
52N/A#ifdef HAVE_IPV6
52N/A# define MAX_IP_LEN INET6_ADDRSTRLEN
52N/A#else
52N/A# define MAX_IP_LEN 20
52N/A#endif
52N/A
52N/A#define IPADDR_IS_V4(ip) ((ip)->family == AF_INET)
52N/A#define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6)
292N/A
52N/A/* returns 1 if IPADDRs are the same */
52N/Aint net_ip_compare(const IPADDR *ip1, const IPADDR *ip2);
6N/A
6N/A/* Connect to socket with ip address */
6N/Aint net_connect_ip(const IPADDR *ip, unsigned int port, const IPADDR *my_ip);
6N/A/* Connect to named UNIX socket */
377N/Aint net_connect_unix(const char *path);
377N/A/* Disconnect socket */
377N/Avoid net_disconnect(int fd);
377N/A/* Try to let the other side close the connection, if it still isn't
377N/A disconnected after certain amount of time, close it ourself */
6N/Avoid net_disconnect_later(int fd);
15N/A
6N/A/* Set socket blocking/nonblocking */
6N/Avoid net_set_nonblock(int fd, int nonblock);
735N/A/* Set TCP_CORK if supported, ie. don't send out partial frames.
6N/A Returns 0 if ok, -1 if failed. */
15N/Aint net_set_cork(int fd, int cork);
549N/A
6N/A/* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may
377N/A include IPv4 depending on the system (Linux yes, BSD no). */
549N/Avoid net_get_ip_any4(IPADDR *ip);
549N/Avoid net_get_ip_any6(IPADDR *ip);
549N/A
377N/A/* Listen for connections on a socket */
377N/Aint net_listen(const IPADDR *my_ip, unsigned int *port);
377N/A/* Listen for connections on an UNIX socket */
377N/Aint net_listen_unix(const char *path);
377N/A/* Accept a connection on a socket. Returns -1 for temporary failure,
377N/A -2 for fatal failure */
377N/Aint net_accept(int fd, IPADDR *addr, unsigned int *port);
6N/A
6N/A/* Read data from socket, return number of bytes read, -1 = error */
549N/Assize_t net_receive(int fd, void *buf, size_t len);
467N/A/* Transmit data, return number of bytes sent, -1 = error */
467N/Assize_t net_transmit(int fd, const void *data, size_t len);
549N/A
6N/A/* Get IP addresses for host. ips contains ips_count of IPs, they don't need
6N/A to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
6N/Aint net_gethostbyname(const char *addr, IPADDR **ips, int *ips_count);
6N/A/* get error of net_gethostname() */
302N/Aconst char *net_gethosterror(int error);
302N/A/* return TRUE if host lookup failed because it didn't exist (ie. not
6N/A some error with name server) */
6N/Aint net_hosterror_notfound(int error);
6N/A
735N/A/* Get socket address/port */
121N/Aint net_getsockname(int fd, IPADDR *addr, unsigned int *port);
735N/A
525N/A/* IPADDR -> char* translation. `host' must be at least MAX_IP_LEN bytes */
6N/Aint net_ip2host(const IPADDR *ip, char *host);
15N/A/* char* -> IPADDR translation. */
15N/Aint net_host2ip(const char *host, IPADDR *ip);
15N/A
15N/A/* Get socket error */
15N/Aint net_geterror(int fd);
15N/A
15N/A/* Get name of TCP service */
15N/Achar *net_getservbyport(unsigned short port);
15N/A
15N/Aint is_ipv4_address(const char *host);
302N/Aint is_ipv6_address(const char *host);
302N/A
302N/A#endif
545N/A