[BACK]Return to ng_ipaddr.h CVS log [TXT][DIR] Up to [local] / ircnowd / src / ipaddr

Annotation of ircnowd/src/ipaddr/ng_ipaddr.h, Revision 1.1

1.1     ! tomglok     1: /*
        !             2:  * (c) 2008 Florian Westphal <fw@strlen.de>, public domain.
        !             3:  */
        !             4:
        !             5: #ifndef NG_IPADDR_HDR
        !             6: #define NG_IPADDR_HDR
        !             7:
        !             8: #include "portab.h"
        !             9:
        !            10: /**
        !            11:  * @file
        !            12:  * Functions for AF_ agnostic ipv4/ipv6 handling (header).
        !            13:  */
        !            14:
        !            15: #include <assert.h>
        !            16: #include <string.h>
        !            17: #include <sys/socket.h>
        !            18: #include <netinet/in.h>
        !            19:
        !            20: #ifdef HAVE_ARPA_INET_H
        !            21: # include <arpa/inet.h>
        !            22: #else
        !            23: # define PF_INET AF_INET
        !            24: #endif
        !            25:
        !            26:
        !            27: #ifdef WANT_IPV6
        !            28: #define NG_INET_ADDRSTRLEN     INET6_ADDRSTRLEN
        !            29: #else
        !            30: #define NG_INET_ADDRSTRLEN     16
        !            31: #endif
        !            32:
        !            33:
        !            34: #ifdef WANT_IPV6
        !            35: typedef union {
        !            36:        struct sockaddr sa;
        !            37:        struct sockaddr_in sin4;
        !            38:        struct sockaddr_in6 sin6;
        !            39: } ng_ipaddr_t;
        !            40: #else
        !            41: /* assume compiler can't deal with typedef struct {... */
        !            42: struct NG_IP_ADDR_DONTUSE {
        !            43:        struct sockaddr_in sin4;
        !            44: };
        !            45: typedef struct NG_IP_ADDR_DONTUSE ng_ipaddr_t;
        !            46: #endif
        !            47:
        !            48:
        !            49: static inline int
        !            50: ng_ipaddr_af(const ng_ipaddr_t *a)
        !            51: {
        !            52:        assert(a != NULL);
        !            53: #ifdef WANT_IPV6
        !            54:        return a->sa.sa_family;
        !            55: #else
        !            56:        assert(a->sin4.sin_family == 0 || a->sin4.sin_family == AF_INET);
        !            57:        return a->sin4.sin_family;
        !            58: #endif
        !            59: }
        !            60:
        !            61:
        !            62: static inline socklen_t
        !            63: ng_ipaddr_salen(const ng_ipaddr_t *a)
        !            64: {
        !            65:        assert(a != NULL);
        !            66: #ifdef WANT_IPV6
        !            67:        assert(a->sa.sa_family == AF_INET || a->sa.sa_family == AF_INET6);
        !            68:        if (a->sa.sa_family == AF_INET6)
        !            69:                return (socklen_t)sizeof(a->sin6);
        !            70: #endif
        !            71:        assert(a->sin4.sin_family == AF_INET);
        !            72:        return (socklen_t)sizeof(a->sin4);
        !            73: }
        !            74:
        !            75:
        !            76: static inline UINT16
        !            77: ng_ipaddr_getport(const ng_ipaddr_t *a)
        !            78: {
        !            79: #ifdef WANT_IPV6
        !            80:        int af = a->sa.sa_family;
        !            81:
        !            82:        assert(a != NULL);
        !            83:        assert(af == AF_INET || af == AF_INET6);
        !            84:
        !            85:        if (af == AF_INET6)
        !            86:                return ntohs(a->sin6.sin6_port);
        !            87: #endif /* WANT_IPV6 */
        !            88:
        !            89:        assert(a != NULL);
        !            90:        assert(a->sin4.sin_family == AF_INET);
        !            91:        return ntohs(a->sin4.sin_port);
        !            92: }
        !            93:
        !            94: /*
        !            95:  * init a ng_ipaddr_t object.
        !            96:  * @param addr: pointer to ng_ipaddr_t to initialize.
        !            97:  * @param ip_str: ip address in dotted-decimal (ipv4) or hexadecimal (ipv6) notation
        !            98:  * @param port: transport layer port number to use.
        !            99:  */
        !           100: GLOBAL bool ng_ipaddr_init PARAMS((ng_ipaddr_t *addr, const char *ip_str, UINT16 port));
        !           101:
        !           102: /* set sin4/sin6_port, depending on a->sa_family */
        !           103: GLOBAL void ng_ipaddr_setport PARAMS((ng_ipaddr_t *a, UINT16 port));
        !           104:
        !           105: /* return true if a and b have the same IP address. If a and b have different AF, return false. */
        !           106: GLOBAL bool ng_ipaddr_ipequal PARAMS((const ng_ipaddr_t *a, const ng_ipaddr_t *b));
        !           107:
        !           108:
        !           109: #ifdef WANT_IPV6
        !           110: /* convert struct sockaddr to string, returns pointer to static buffer */
        !           111: GLOBAL const char *ng_ipaddr_tostr PARAMS((const ng_ipaddr_t *addr));
        !           112:
        !           113: /* convert struct sockaddr to string. dest must be NG_INET_ADDRSTRLEN bytes long */
        !           114: GLOBAL bool ng_ipaddr_tostr_r PARAMS((const ng_ipaddr_t *addr, char *dest));
        !           115: #else
        !           116: static inline const char*
        !           117: ng_ipaddr_tostr(const ng_ipaddr_t *addr)
        !           118: {
        !           119:        assert(addr != NULL);
        !           120:        return inet_ntoa(addr->sin4.sin_addr);
        !           121: }
        !           122:
        !           123: static inline bool
        !           124: ng_ipaddr_tostr_r(const ng_ipaddr_t *addr, char *d)
        !           125: {
        !           126:        assert(addr != NULL);
        !           127:        assert(d != NULL);
        !           128:        strlcpy(d, inet_ntoa(addr->sin4.sin_addr), NG_INET_ADDRSTRLEN);
        !           129:        return true;
        !           130: }
        !           131: #endif
        !           132: #endif
        !           133:
        !           134: /* -eof- */

CVSweb