static int parse_ipaddr(struct ipset_session *session, enum ipset_opt opt, const char *str, uint8_t family) { uint8_t m = family == AF_INET ? 32 : 128; int aerr = EINVAL, err = 0, range = 0; char *saved = strdup(str); char *a, *tmp = saved; struct addrinfo *info; enum ipset_opt copt = opt == IPSET_OPT_IP ? IPSET_OPT_CIDR : IPSET_OPT_CIDR2; if (tmp == NULL) return ipset_err(session, "Cannot allocate memory to duplicate %s.", str); if ((a = cidr_separator(tmp)) != NULL) { /* IP/mask */ *a++ = '\0'; if ((err = string_to_cidr(session, a, 0, m, &m)) != 0 || (err = ipset_session_data_set(session, copt, &m)) != 0) goto out; } else if ((a = range_separator(tmp)) != NULL) { /* IP-IP */ *a++ = '\0'; D("range %s", a); range++; } if ((aerr = get_addrinfo(session, opt, tmp, &info, family)) != 0 || !range) goto out; freeaddrinfo(info); aerr = get_addrinfo(session, IPSET_OPT_IP_TO, a, &info, family); out: if (aerr != EINVAL) /* getaddrinfo not failed */ freeaddrinfo(info); else if (aerr) err = -1; free(saved); return err; }
/* * Takes a url and returns an addrinfo struct and an open socket if we can * connect. The addrinfo struct must be freed with freeaddrinfo(). Returns the * socket on success, -1 on error. */ int open_socket(const struct http_url_t *url, struct addrinfo **addr) { trace("%s\n", "Getting address info"); int status; if ((status = get_addrinfo(url, addr)) != 0) { return status; } return new_connection(*addr); }