Hi,
I am having an issue using getservbyport. Here is a little program to demonstrate the problem (removed the includes):
int
main(void) {
struct servent *service;
int memsize = sizeof(struct servent);
service = (struct servent *)malloc(memsize);
bzero(service, memsize);
service = getservbyport(25, NULL);
if (service == NULL)
{
perror("getservbyport()");
exit(1);
}
printf("service name = %s\n", service->s_name);
return 0;
}
Here is how I am compiling and running this program
me@host:~$ gcc -o foo foo.c
me@host:~$ ./foo
getservbyport(): Success
I was expecting to see "service name = smtp", since my /etc/services file contains the following two lines:
smtp 25/tcp mail #Simple Mail Transfer
smtp 25/udp mail #Simple Mail Transfer
I tried this on two hosts with the same result. One has gcc 4.1.2 and the other has gcc 2.96. I also get the same result, on both hosts, if I specify the protocol to use when calling getservbyport (instead of NULL which matches any protocol).
Can anyone see what I am doing wrong, or why a NULL result from the call to getservbyport would say "Success" from the perror call? The man page for getservbyport says the following:
The getservent(), getservbyname() and getservbyport() functions return
the servent structure, or a NULL pointer if an error occurs or the end
of the file is reached.
It could be reaching EOF, but again, the two lines are present for smtp in my /etc/services file.
Thanks,
goon12