static gboolean val_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_, LogFunc logfunc) { guint32 addr; unsigned int nmask_bits; const char *has_slash; const char *net_str, *addr_str; fvalue_t *nmask_fvalue; /* Look for CIDR: Is there a single slash in the string? */ has_slash = strchr(s, '/'); if (has_slash) { /* Make a copy of the string up to but not including the * slash; that's the address portion. */ addr_str = ep_strndup(s, has_slash - s); } else { addr_str = s; } if (!get_host_ipaddr(addr_str, &addr)) { logfunc("\"%s\" is not a valid hostname or IPv4 address.", addr_str); return FALSE; } ipv4_addr_set_net_order_addr(&(fv->value.ipv4), addr); /* If CIDR, get netmask bits. */ if (has_slash) { /* Skip past the slash */ net_str = has_slash + 1; /* XXX - this is inefficient */ nmask_fvalue = fvalue_from_unparsed(FT_UINT32, net_str, FALSE, logfunc); if (!nmask_fvalue) { return FALSE; } nmask_bits = fvalue_get_uinteger(nmask_fvalue); FVALUE_FREE(nmask_fvalue); if (nmask_bits > 32) { logfunc("Netmask bits in a CIDR IPv4 address should be <= 32, not %u", nmask_bits); return FALSE; } ipv4_addr_set_netmask_bits(&fv->value.ipv4, nmask_bits); } else { /* Not CIDR; mask covers entire address. */ ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32); } return TRUE; }
static void set_uinteger(fvalue_t *fv, guint32 value) { ipv4_addr_set_net_order_addr(&(fv->value.ipv4), value); ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32); }
static gboolean val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc) { guint32 addr; unsigned int nmask_bits; char *has_slash, *s_copy = NULL; char *net_str, *addr_str; fvalue_t *nmask_fvalue; /* Look for CIDR: Is there a single slash in the string? */ has_slash = strchr(s, '/'); if (has_slash) { /* Make a copy of the string and use strtok() to * get the address portion. */ s_copy = ep_strdup(s); addr_str = strtok(s_copy, "/"); /* I just checked for slash! I shouldn't get NULL here. * Double check just in case. */ if (!addr_str) { logfunc("Unexpected strtok() error parsing IP address: %s", s_copy); return FALSE; } } else { addr_str = s; } if (!get_host_ipaddr(addr_str, &addr)) { logfunc("\"%s\" is not a valid hostname or IPv4 address.", addr_str); return FALSE; } ipv4_addr_set_host_order_addr(&(fv->value.ipv4), addr); /* If CIDR, get netmask bits. */ if (has_slash) { net_str = strtok(NULL, "/"); /* I checked for slash! I shouldn't get NULL here. * Double check just in case. */ if (!net_str) { logfunc("Unexpected strtok() error parsing netmask: %s", s_copy); return FALSE; } /* XXX - this is inefficient */ nmask_fvalue = fvalue_from_unparsed(FT_UINT32, net_str, FALSE, logfunc); if (!nmask_fvalue) { return FALSE; } nmask_bits = fvalue_get_uinteger(nmask_fvalue); FVALUE_FREE(nmask_fvalue); if (nmask_bits > 32) { logfunc("Netmask bits in a CIDR IPv4 address should be <= 32, not %u", nmask_bits); return FALSE; } ipv4_addr_set_netmask_bits(&fv->value.ipv4, nmask_bits); } else { /* Not CIDR; mask covers entire address. */ ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32); } return TRUE; }