Thread: Printing protocol name based on netinet/in.h

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Printing protocol name based on netinet/in.h

    Hi,

    Code:
    cat /usr/include/netinet/in.h | grep "#define IPPR"
    #define IPPROTO_IP              IPPROTO_IP
    #define IPPROTO_HOPOPTS         IPPROTO_HOPOPTS
    #define IPPROTO_ICMP            IPPROTO_ICMP
    #define IPPROTO_IGMP            IPPROTO_IGMP
    #define IPPROTO_IPIP            IPPROTO_IPIP
    #define IPPROTO_TCP             IPPROTO_TCP
    #define IPPROTO_EGP             IPPROTO_EGP
    #define IPPROTO_PUP             IPPROTO_PUP
    #define IPPROTO_UDP             IPPROTO_UDP
    #define IPPROTO_IDP             IPPROTO_IDP
    #define IPPROTO_TP              IPPROTO_TP
    #define IPPROTO_IPV6            IPPROTO_IPV6
    #define IPPROTO_ROUTING         IPPROTO_ROUTING
    #define IPPROTO_FRAGMENT        IPPROTO_FRAGMENT
    #define IPPROTO_RSVP            IPPROTO_RSVP
    #define IPPROTO_GRE             IPPROTO_GRE
    #define IPPROTO_ESP             IPPROTO_ESP
    #define IPPROTO_AH              IPPROTO_AH
    #define IPPROTO_ICMPV6          IPPROTO_ICMPV6
    #define IPPROTO_NONE            IPPROTO_NONE
    #define IPPROTO_DSTOPTS         IPPROTO_DSTOPTS
    #define IPPROTO_MTP             IPPROTO_MTP
    #define IPPROTO_ENCAP           IPPROTO_ENCAP
    #define IPPROTO_PIM             IPPROTO_PIM
    #define IPPROTO_COMP            IPPROTO_COMP
    #define IPPROTO_SCTP            IPPROTO_SCTP
    #define IPPROTO_RAW             IPPROTO_RAW
    I want to be able to print need to print IPPROTO_UDP, IPPROTO_ENCAP, or any other out of structure ip_hdr.


    Code:
    printf("%16d|", ip_hdr->ip_p);
    						/*
    						switch (ip_hdr->ip_p) {
                            case IPPROTO_TCP:
                                    printf("%8s|", "TCP");
                                    break;
                            case IPPROTO_UDP:
                                    printf("%8s|", "UDP");
                                    break;
                            case IPPROTO_ICMP:
                                    printf("%8s|", "ICMP");
                                    break;
                            case IPPROTO_IP:
                                    printf("%8s|", "IP");
                                    break;
                            default:
                                    printf("%8s|", "UNKNOWN");
                                    return;
                            }
    						*/
    Currently, I just match with the CASE. Who knows another way?


    Thank you in advance!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    As far as I'm aware, there is no function that does what you want. Your approach is really all you can do.

    I would recommend using a script to generate such a function, though. It should make life easier if you need to add types in the future:
    Code:
    #!/bin/sh
    protos="IP ICMP" # list them all
    echo "const char *proto_name(int proto) {"
    echo "  switch(proto) {"
    for proto in ${protos}
    do
      cat <<EOF
    #ifdef IPPROTO_${proto}
        case IPPROTO_${proto}: return "${proto}";
    #endif
    EOF
    done
    cat <<EOF
      }
      return "UNKNOWN";
    }
    EOF
    To be portable, you want to check for the existence of each macro (a few you have listed are standard, but many are not).

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Are you looking for something like this: getservbyport(3): service entry - Linux man page

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by greenberg View Post
    Hi,
    I want to be able to print need to print IPPROTO_UDP, IPPROTO_ENCAP, or any other out of structure ip_hdr.

    Currently, I just match with the CASE. Who knows another way?
    Thank you in advance!
    Each protocal is assigned a number... You could build a .h file with an constant array of strings... the array index is the protocal number, the string is the protocal name.... Then you can printf("%s" IPPROTO_UDP); to display it's name.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    #define MKSTR(x)    #x
    #define FOO 10
    
    int main ( ) {
      printf("%s=%d\n", MKSTR(FOO), FOO );
      return 0;
    }
    
    $ gcc foo.c
    $ ./a.out 
    FOO=10
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing out an ASCII tire based on two radiuses
    By levitylek in forum C Programming
    Replies: 7
    Last Post: 10-14-2010, 06:47 PM
  2. Replies: 2
    Last Post: 10-18-2009, 01:05 PM
  3. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  4. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  5. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM