#ifndef fooutilshfoo #define fooutilshfoo #include #include #include #include #include #include #include #include extern unsigned debug_mask; #include "list.h" #include "debug.h" /* These functions need to be defined in the linking binary */ #define zmalloc(s) __zmalloc(__func__, __LINE__, s) void *__zmalloc(const char *fn, int line, size_t s); #define xstrdup(s) __xstrdup(__func__, __LINE__, s) char *__xstrdup(const char *fn, int line, const char *s); #define xstrndup(s, n) __xstrndup(__func__, __LINE__, s, n) char *__xstrndup(const char *fn, int line, const char *s, size_t n); #define xfree(s) __xfree(__func__, __LINE__, s) void __xfree(const char *fn, int line, void *ptr); /* Length of longest DNS name = 253 + trailing dot */ #define FQDN_STR_LEN 254 /* Length of longest port string = strlen("65535") */ #define PORT_STR_LEN 5 /* Length of longest address family string = strlen("AF_INETX") */ #define AF_STR_LEN 8 /* Length of longest addrstr, format = "AF_INETX */ #define ADDRSTRLEN (AF_STR_LEN + 1 + INET6_ADDRSTRLEN + 1 + PORT_STR_LEN + 1) struct saddr { union { struct sockaddr_storage storage; struct sockaddr_in in4; struct sockaddr_in6 in6; struct sockaddr_ll ll; }; socklen_t addrlen; char addrstr[ADDRSTRLEN]; struct list_head list; }; void socket_set_low_latency(int sfd, bool keepalive, bool iptos, bool nodelay); char *saddr_addr(struct saddr *saddr, char *buf, size_t len); uint16_t saddr_port(struct saddr *saddr); void saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port); void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip, in_port_t port); void saddr_set_addrstr(struct saddr *saddr); int strtou16_strict(const char *str, uint16_t *result); static inline bool empty_str(const char *str) { if (!str || str[0] == '\0') return true; else return false; } static inline bool streq(const char *a, const char *b) { return strcmp(a, b) == 0; } static inline bool strcaseeq(const char *a, const char *b) { return strcasecmp(a, b) == 0; } #if __BYTE_ORDER == __LITTLE_ENDIAN #define chtobe32(x) __bswap_constant_32(x) #else #define chtobe32(x) (x) #endif #if __BYTE_ORDER == __LITTLE_ENDIAN #define cinet_addr(a,b,c,d) ((uint32_t)((a)<<0|(b)<<8|(c)<<16|(d)<<24)) #else #define cinet_addr(a,b,c,d) ((uint32_t)((a)<<24|(b)<<16|(c)<<8|(d)<<0)) #endif #define PIPE_RD 0 #define PIPE_WR 1 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) #endif