Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3474

[Software] getservbyname panics if portname not present in /etc/services

$
0
0
I have observed that calling getservbyname with a portname that is not present in /etc/hosts causes a fault that terminate a program.

The man page for getservbyname() [netdb.h] says
RETURN VALUE
The getservent(), getservbyname(), and getservbyport() functions return
a pointer to a statically allocated servent structure, or NULL if an
error occurs or the end of the file is reached.

This does not seem to be the behavior that is actually exhibited.

This is how I am handling it.

#define WKPN_VPA 5164
const char * serviceName = "vpa";
const char * protocolName = "tcp";
int iVpaPort = 0;

/* if the requested port number is not present in /etc/services,
* an exception occurs. It should just return a null pointer.
*/

try {
struct servent *serv = getservbyname(serviceName, protocolName);
iVpaPort = ntohs(serv->s_port);
} catch (exception const& e) {
cerr << "Requested port not present in /etc/services" << endl;
iVpaPort = WKPN_VPA;
}

Statistics: Posted by ichibrosan — 2024-07-29 12:20 — Replies 1 — Views 13



Viewing all articles
Browse latest Browse all 3474

Trending Articles