/* * Get IP address and port from the argument. * Return 0 if failed, * 1 if addr read * 2 if addr and port read */ int parse_addrport(char *buf, u_int16_t proto, u_int32_t * addr, u_int16_t * port) { char *pp; long prt; struct in_addr inaddr; pp = strchr(buf, ':'); if (pp) *pp = '\0'; if (inet_aton(buf, &inaddr) != 0) *addr = inaddr.s_addr; else if (host_to_addr(buf, &inaddr) != -1) *addr = inaddr.s_addr; else return 0; if (pp == NULL) return 1; if ((prt = string_to_number(pp + 1, 0, 65535)) != -1) *port = htons(prt); else if ((prt = service_to_port(pp + 1, proto)) != -1) *port = htons(prt); else return 0; return 2; }
static int parse_service(char *buf, ipvs_service_t *svc) { char *portp = NULL; long portn; int result=SERVICE_NONE; struct in_addr inaddr; struct in6_addr inaddr6; if (buf == NULL || str_is_digit(buf)) return SERVICE_NONE; if (buf[0] == '[') { buf++; portp = strchr(buf, ']'); if (portp == NULL) return SERVICE_NONE; *portp = '\0'; portp++; if (*portp == ':') *portp = '\0'; else return SERVICE_NONE; } if (inet_pton(AF_INET6, buf, &inaddr6) > 0) { svc->addr.in6 = inaddr6; svc->af = AF_INET6; svc->netmask = 128; } else { portp = strrchr(buf, ':'); if (portp != NULL) *portp = '\0'; if (inet_aton(buf, &inaddr) != 0) { svc->addr.ip = inaddr.s_addr; svc->af = AF_INET; } else if (host_to_addr(buf, &inaddr) != -1) { svc->addr.ip = inaddr.s_addr; svc->af = AF_INET; } else return SERVICE_NONE; } result |= SERVICE_ADDR; if (portp != NULL) { result |= SERVICE_PORT; if ((portn = string_to_number(portp+1, 0, 65535)) != -1) svc->port = htons(portn); else if ((portn = service_to_port(portp+1, svc->protocol)) != -1) svc->port = htons(portn); else return SERVICE_NONE; } return result; }
static u_int16_t parse_tcp_port(const char *port) { unsigned int portnum; if (string_to_number(port, 0, 65535, &portnum) != -1 || (portnum = service_to_port(port)) != -1) return (u_int16_t)portnum; exit_error(PARAMETER_PROBLEM, "invalid TCP port/service `%s' specified", port); }
static u_int16_t parse_port(const char *port) { unsigned int portnum; if ((portnum = service_to_port(port)) != -1) { return (u_int16_t)portnum; } else { return atoi(port); } }