X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=net.c;h=00844d735c033e73a20b8a975e0bf9db142d2600;hp=f10afb71e31f0a1e7178972102b22c0feb01bfcc;hb=3010ef96e10cb15d423eef8f9802fbed78744393;hpb=a9126f461792a84c760162ecb25100f1593d427d diff --git a/net.c b/net.c index f10afb71..00844d73 100644 --- a/net.c +++ b/net.c @@ -36,6 +36,47 @@ #include "string.h" #include "fd.h" +/** + * Parse and validate IPv4 address/netmask string. + * + * \param cidr Address in CIDR notation + * \param addr Copy of the IPv4 address part of \a cidr + * \param addrlen Size of \a addr in bytes + * \param netmask Value of the netmask part in \a cidr or the + * default of 32 if not specified. + * + * \return Pointer to \a addr if succesful, NULL on error. + * \sa RFC 4632 + */ +char *parse_cidr(const char *cidr, + char *addr, ssize_t addrlen, + int32_t *netmask) +{ + const char *o = cidr; + char *c = addr, *end = c + (addrlen - 1); + + *netmask = 0x20; + + if (cidr == NULL || addrlen < 1) + goto failed; + + for (o = cidr; (*c = *o == '/'? '\0' : *o); c++, o++) + if (c == end) + goto failed; + + if (*o == '/') + if (para_atoi32(++o, netmask) < 0 || + *netmask < 0 || *netmask > 0x20) + goto failed; + + if (is_valid_ipv4_address(addr)) + return addr; +failed: + *addr = '\0'; + return NULL; +} + + /** * Match string as a candidate IPv4 address. * @@ -47,7 +88,8 @@ static bool is_v4_dot_quad(const char *address) bool result; regex_t r; - assert(!regcomp(&r, "^([0-9]+\\.){3}[0-9]+$", REG_EXTENDED|REG_NOSUB)); + assert(para_regcomp(&r, "^([0-9]+\\.){3}[0-9]+$", + REG_EXTENDED | REG_NOSUB) >= 0); result = regexec(&r, address, 0, NULL, 0) == 0; regfree(&r); return result; @@ -796,10 +838,11 @@ int recv_pattern(int fd, const char *pattern, size_t bufsize) ret = 1; out: if (ret < 0) { - PARA_NOTICE_LOG("n = %d, did not receive pattern '%s'\n", n, - pattern); + PARA_NOTICE_LOG("did not receive pattern '%s'\n", pattern); if (n > 0) - PARA_NOTICE_LOG("recvd: %s\n", buf); + PARA_NOTICE_LOG("recvd %d bytes: %s\n", n, buf); + else if (n < 0) + PARA_NOTICE_LOG("%s\n", para_strerror(-n)); } free(buf); return ret;