}
if (service != NULL && service[0] != '\0') {
- if (isdigit((int)service[0])) {
- port = (unsigned short)atoi(service);
- } else {
- const char *proto = NULL;
- struct servent *s;
+ const char *proto = NULL;
+ struct servent *s;
+ char *end_ptr;
+ long sl;
- if (hints->ai_protocol != 0) {
- struct protoent *pent;
+ errno = 0;
+ sl = strtol(service, &end_ptr, 10);
- pent = getprotobynumber(hints->ai_protocol);
- if (pent != NULL) {
- proto = pent->p_name;
- }
+ if (*end_ptr == '\0' || end_ptr != service) {
+ port = sl;
+ goto valid_port;
+ } else if (hints->ai_flags & AI_NUMERICSERV) {
+ return EAI_SERVICE;
+ }
+
+ if (hints->ai_protocol != 0) {
+ struct protoent *pent;
+
+ pent = getprotobynumber(hints->ai_protocol);
+ if (pent != NULL) {
+ proto = pent->p_name;
}
+ }
- s = getservbyname(service, proto);
- if (s != NULL) {
- port = ntohs(s->s_port);
- } else {
- if (p != NULL) {
- freeaddrinfo(p);
- }
- return EAI_SERVICE;
+ s = getservbyname(service, proto);
+ if (s != NULL) {
+ port = ntohs(s->s_port);
+ } else {
+ if (p != NULL) {
+ freeaddrinfo(p);
}
+ return EAI_SERVICE;
}
}
+valid_port:
rc = 0;
if (hints->ai_family == AF_UNSPEC || hints->ai_family == AF_INET) {
rc = inet_pton(AF_INET, node, &addr.in.v4);