bug-inetutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

inetutils-1.4.1: suggest removal of getprotobyname() calls


From: Michael Deutschmann
Subject: inetutils-1.4.1: suggest removal of getprotobyname() calls
Date: Fri, 4 Oct 2002 01:12:12 -0700 (PDT)

There are three places in the code that use getprotobyname() calls with a
constant name.  That's pointless, as the number for a specific protocol
never changes.  It bloats the code slightly (as opposed to compiling in
the numbers), and means that ping requires /etc/protocols to be available
to function.

I noticed also that two of the uses seem to be erroneous as well.  In
rshd.c and rlogind.c, `getprotobyname("ip")' is looked up and the result
used as a level argument to get/setsockopt(), with IPPROTO_IP as a
fallback.  But the level argument should be a SOL_* constant, not an
IPPROTO_* constant.  It happens to work on Linux because IPPROTO_IP has
the same numeric value of the correct constant, SOL_IP (0).

Appended is a simple patch removing these unneccesary calls.  The
appropriate compile-time constants are used instead.

Interestingly, the one program that should (IMO) be using getprotobyname
to resolve human-supplied input, inetd, isn't.  It mostly ignores the
protocol string and always gives an argument of zero (default) to
socket().  I haven't messed with that at the moment.

---- Michael Deutschmann <address@hidden>

diff -durpN inetutils-1.4.1/libicmp/libping.c inetutils-work/libicmp/libping.c
--- inetutils-1.4.1/libicmp/libping.c   2001-08-11 01:32:20.000000000 -0700
+++ inetutils-work/libicmp/libping.c    2002-10-03 23:48:39.000000000 -0700
@@ -64,15 +64,10 @@ PING *
 ping_init (int type, int ident)
 {
   int fd;
-  struct protoent *proto;
   PING *p;
 
   /* Initialize raw ICMP socket */
-  if (!(proto = getprotobyname ("icmp"))) {
-    fprintf (stderr, "ping: unknown protocol icmp.\n");
-    return NULL;
-  }
-  if ((fd = socket (AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
+  if ((fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
     if (errno == EPERM) {
       fprintf (stderr, "ping: ping must run as root\n");
     }
diff -durpN inetutils-1.4.1/rlogind/rlogind.c inetutils-work/rlogind/rlogind.c
--- inetutils-1.4.1/rlogind/rlogind.c   2002-06-25 20:15:06.000000000 -0700
+++ inetutils-work/rlogind/rlogind.c    2002-10-03 23:47:39.000000000 -0700
@@ -534,14 +534,9 @@ rlogind_auth (int fd, struct auth_data *
       {
        u_char optbuf[BUFSIZ / 3], *cp;
        char lbuf[BUFSIZ], *lp;
-       int optsize = sizeof (optbuf), ipproto;
-       struct protoent *ip;
+       int optsize = sizeof (optbuf);
 
-       if ((ip = getprotobyname ("ip")) != NULL)
-         ipproto = ip->p_proto;
-       else
-         ipproto = IPPROTO_IP;
-       if (getsockopt (0, ipproto, IP_OPTIONS, (char *) optbuf,
+       if (getsockopt (0, SOL_IP, IP_OPTIONS, (char *) optbuf,
                        &optsize) == 0 && optsize != 0)
          {
            lp = lbuf;
@@ -549,7 +544,7 @@ rlogind_auth (int fd, struct auth_data *
              sprintf (lp, " %2.2x", *cp);
            syslog (LOG_NOTICE,
              "Ignoring IP options: %s", lbuf);
-           if (setsockopt (0, ipproto, IP_OPTIONS, (char *) NULL, optsize))
+           if (setsockopt (0, SOL_IP, IP_OPTIONS, (char *) NULL, optsize))
              {
                syslog (LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
                exit (1);
diff -durpN inetutils-1.4.1/rshd/rshd.c inetutils-work/rshd/rshd.c
--- inetutils-1.4.1/rshd/rshd.c 2002-09-02 07:13:37.000000000 -0700
+++ inetutils-work/rshd/rshd.c  2002-10-03 23:47:39.000000000 -0700
@@ -319,14 +319,9 @@ doit (int sockfd, struct sockaddr_in *fr
   {
     u_char optbuf[BUFSIZ/3], *cp;
     char lbuf[BUFSIZ], *lp;
-    int optsize = sizeof(optbuf), ipproto;
-    struct protoent *ip;
+    int optsize = sizeof(optbuf);
 
-    if ((ip = getprotobyname ("ip")) != NULL)
-      ipproto = ip->p_proto;
-    else
-      ipproto = IPPROTO_IP;
-    if (!getsockopt (sockfd, ipproto, IP_OPTIONS, (char *)optbuf,
+    if (!getsockopt (sockfd, SOL_IP, IP_OPTIONS, (char *)optbuf,
                     &optsize) && optsize != 0)
       {
        lp = lbuf;
@@ -340,7 +335,7 @@ doit (int sockfd, struct sockaddr_in *fr
               inet_ntoa (fromp->sin_addr), lbuf);
 
        /* Turn off the options.  If this doesn't work, we quit */
-       if (setsockopt (sockfd, ipproto, IP_OPTIONS,
+       if (setsockopt (sockfd, SOL_IP, IP_OPTIONS,
                        (char *)NULL, optsize) != 0)
          {
            syslog (LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");




reply via email to

[Prev in Thread] Current Thread [Next in Thread]