/*
* Misc. network related functions
*
* Copyright 2004-2009 Nicolas Bernard
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifndef _NETMISC_H_
#define _NETMISC_H_
#include
#include
/** returns 1 if addr is a loopback address, 0 else (or < 0 if an error occured,
e.g. addr is of an unsupported type (neither IPv4 nor IPv6). */
extern int isloopback(const struct sockaddr* addr);
/** returns 1 if addr is a private (e.g. on a NAT) address, 0 else (or < 0 if
an error occured, e.g. addr is of an unsupported type (neither IPv4 nor
IPv6). */
extern int isprivateip(const struct sockaddr* addr);
/** returns 0 if IP(a) != IP(b), 1 if IP(a) == IP(b), <0 in case of error */
extern int addreql(const struct sockaddr* a, const struct sockaddr* b);
/** returns 0 if Port(a) != Port(b), 1 if Port(a) == Port(b),
<0 in case of error */
extern int porteql(const struct sockaddr* a, const struct sockaddr* b);
/** returns 1 if the upto first bits of the addresses are equal, 0 otherwise
<0 in case of error */
extern int prefixeql(const struct sockaddr* a, const struct sockaddr* b, uint8_t upto);
/** prints addr on f */
extern int echoip(FILE* f, const struct sockaddr* addr);
/** returns the assumed size of the struct sockaddr.
useful because GLIBC's sockaddr doesn't have a sa_len field */
extern unsigned int addrsize(const struct sockaddr* addr);
/** put the string representation of sa in buffer. */
/* Flawfinder: ignore */
extern int string_ip(char buffer[47], const struct sockaddr_storage *sa);
/** convert a string representation to a binary one */
extern int read_ip(const char *string, struct sockaddr_storage *sa);
/** those 2 fcts wait for the whole bufsize. returns < 0 if a
fatal error occurs, 0 if all goes well */
extern int writen(int desc, const void* buf, unsigned int bufsize);
extern int readn(int desc, void* buf, unsigned int bufsize);
/** like readn buf don't try to much to read bufsize octets */
extern int readunk(int desc, void* buf, unsigned int bufsize);
/* returns the port of the address in addr */
extern uint16_t portof(const struct sockaddr* addr);
#endif /* ! _NETMISC_H_ */