int parse_ip(char *str, char **ipstr, unsigned int *mask) { int ret; char *maskstr; unsigned int ip[4]; char dst[65]; int family; if ((maskstr = strchr(str, '/')) != NULL) *maskstr++ = 0; if ((family = get_netaddr(str, ip)) == -1) return -1; if ((inet_ntop(family, ip, dst, sizeof(dst) - 1)) == NULL) return -1; *mask = 0; if (maskstr != NULL) { if (check_ipv4(maskstr)) { if (get_netaddr(maskstr, ip) == -1) return -1; *mask = ip[0]; } else { ret = parse_int(maskstr, (int *)mask); if (ret || *mask > 128 || *mask < 0) return -1; if (family == AF_INET) *mask = htonl(~0 << (32 - *mask)); } } *ipstr = strdup(dst); return 0; }
int ip_ctl(vps_handler *h, envid_t veid, int op, char *ip) { int ret; int family; unsigned int ipaddr[4]; if ((family = get_netaddr(ip, ipaddr)) < 0) return 0; ret = _ip_ctl(h, veid, op, ipaddr, family); if (ret) { switch (errno) { case EADDRINUSE: ret = VZ_IP_INUSE; break; case ESRCH: ret = VZ_VE_NOT_RUNNING; break; case EADDRNOTAVAIL: if (op == VE_IP_DEL) return 0; ret = VZ_IP_NA; break; default: ret = VZ_CANT_ADDIP; break; } logger(-1, errno, "Unable to %s IP %s", op == VE_IP_ADD ? "add" : "del", ip); } return ret; }