0

I believe getprotoent() function gets values from /etc/protocols.

I made the following sample program: test.c.

It shows

proto.p_name = ip
*proto.p_aliases = IP
proto.p_proto = 0

These values are the same as the first entry in /etc/protocols.

I want to know how I can get the values of the 2nd entries like looping with evaluate some pointers not using getprotobyname() or getprotobynumber().

Test.c

#include <stdio.h>
#include <string.h>
#include <netdb.h>

int main(int argc,char *argv[])
{
    protEnt();
}

int protEnt()
{
    setprotoent(2);
    struct protoent proto;
    proto = *getprotoent();
    printf("proto.p_name      = %s\n",proto.p_name);
    printf("*proto.p_aliases  = %s\n",*proto.p_aliases);
    printf("proto.p_proto     = %d\n",proto.p_proto);
    proto = *getprotoent();
    endprotoent();
}
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
user1345414
  • 3,745
  • 9
  • 36
  • 56

1 Answers1

3

You can call getprotoent() all over and over again and get next entry on each call.. You just need to check it for NULL which means there are no entries left. This means that you need to change your proto variable to be pointer.

So something like this:

struct protoent *proto;
while((proto = getprotoent()) != NULL)
{
        printf("proto.p_name      = %s\n",(*proto).p_name);
        printf("*proto.p_aliases  = %s\n",*(*proto).p_aliases);
        printf("proto.p_proto     = %d\n",(*proto).p_proto);
}
Jeribo
  • 465
  • 4
  • 9