made ip_hostname() take a buffer pointer, and a buffer size.
authoraland <aland>
Thu, 2 Mar 2000 19:30:41 +0000 (19:30 +0000)
committeraland <aland>
Thu, 2 Mar 2000 19:30:41 +0000 (19:30 +0000)
This makes it multi-threaded safe.

Changed a number of other functions which call ip_hostname(),
to update them for this change.

Also changed the print function in lib/print.c, to call ip_hostname,
instead of ip_ntoa.

src/include/libradius.h
src/lib/misc.c
src/lib/print.c
src/main/files.c
src/main/nas.c
src/main/radwho.c
src/main/radzap.c
src/modules/rlm_files/rlm_files.c

index c0faab3..3ca7931 100644 (file)
@@ -182,7 +182,7 @@ extern int  librad_debug;
 /*
  *     Several handy miscellaneous functions.
  */
-char *         ip_hostname (uint32_t);
+char *         ip_hostname (char *buf, size_t buflen, uint32_t ipaddr);
 uint32_t       ip_getaddr (const char *);
 char *         ip_ntoa(char *, uint32_t);
 uint32_t       ip_addr(const char *);
index 608226b..a512c7c 100644 (file)
@@ -33,25 +33,27 @@ int         librad_debug = 0;
  *     Return a printable host name (or IP address in dot notation)
  *     for the supplied IP address.
  */
-char * ip_hostname(uint32_t ipaddr)
+char * ip_hostname(char *buf, size_t buflen, uint32_t ipaddr)
 {
        struct          hostent *hp;
-       static char     hstname[128];
 
        /*
         *      No DNS: don't look up host names
         */
        if (!librad_dodns) {
-               ip_ntoa(hstname, ipaddr);
-               return(hstname);
+               ip_ntoa(buf, ipaddr);
+               return buf;
        }
 
        hp = gethostbyaddr((char *)&ipaddr, sizeof (struct in_addr), AF_INET);
-       if (hp == 0) {
-               ip_ntoa(hstname, ipaddr);
-               return(hstname);
+       if ((hp == 0) ||
+           (strlen((char *)hp->h_name) >= buflen)) {
+               ip_ntoa(buf, ipaddr);
+               return buf;
        }
-       return (char *)hp->h_name;
+
+       strNcpy(buf, (char *)hp->h_name, buflen);
+       return buf;
 }
 
 
index 8e110e8..d9f1394 100644 (file)
@@ -130,7 +130,9 @@ int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
                        if (vp->strvalue[0])
                                a = vp->strvalue;
                        else
-                               a = ip_ntoa(NULL, vp->lvalue);
+                               a = ip_hostname(vp->strvalue,
+                                               sizeof(vp->strvalue),
+                                               vp->lvalue);
                        break;
                case PW_TYPE_ABINARY:
 #ifdef ASCEND_BINARY
index edc98c5..8c19f70 100644 (file)
@@ -372,7 +372,7 @@ int read_clients_file(const char *file)
                }
                strcpy(c->secret, secret);
                strcpy(c->shortname, shortnm);
-               strcpy(c->longname, ip_hostname(c->ipaddr));
+               ip_hostname(c->longname, sizeof(c->longname), c->ipaddr);
 
                c->next = clients;
                clients = c;
@@ -411,7 +411,14 @@ char *client_name(uint32_t ipaddr)
                else
                        return cl->longname;
        }
-       return ip_hostname(ipaddr);
+
+       /*
+        *      FIXME!
+        *
+        *      We should NEVER reach this piece of code, as we should
+        *      NEVER be looking up client names for clients we don't know!
+        */
+       return NULL;
 }
 
 #ifndef BUILDDBM /* HACK HACK */
index 9202412..34350d6 100644 (file)
@@ -116,7 +116,8 @@ int read_naslist_file(char *file)
                        strcpy(c->longname, hostnm);
                } else {
                        c->ipaddr = ip_getaddr(hostnm);
-                       strcpy(c->longname, ip_hostname(c->ipaddr));
+                       ip_hostname(c->longname, sizeof(c->longname),
+                                   c->ipaddr);
                }
 
                c->next = naslist;
@@ -179,6 +180,7 @@ NAS *nas_findbyname(char *nasname)
 char *nas_name(uint32_t ipaddr)
 {
        NAS *cl;
+       char buf[256];
 
        if ((cl = nas_find(ipaddr)) != NULL) {
                if (cl->shortname[0])
@@ -186,7 +188,13 @@ char *nas_name(uint32_t ipaddr)
                else
                        return cl->longname;
        }
-       return ip_hostname(ipaddr);
+
+       /*
+        *      FIXME!
+        *
+        *      This isn't multi-threaded safe!
+        */
+       return ip_hostname(buf, sizeof(buf), ipaddr);
 }
 
 /*
@@ -197,6 +205,7 @@ char *nas_name2(RADIUS_PACKET *packet)
        uint32_t        ipaddr;
        NAS             *cl;
        VALUE_PAIR      *pair;
+       char            buf[256];
 
        if ((pair = pairfind(packet->vps, PW_NAS_IP_ADDRESS)) != NULL)
                ipaddr = pair->lvalue;
@@ -209,6 +218,12 @@ char *nas_name2(RADIUS_PACKET *packet)
                else
                        return cl->longname;
        }
-       return ip_hostname(ipaddr);
+
+       /*
+        *      FIXME!!!
+        *
+        *      This isn't multi-threaded safe!
+        */
+       return ip_hostname(buf, sizeof(buf), ipaddr);
 }
 
index ab2c042..ce3b761 100644 (file)
@@ -122,9 +122,7 @@ static NAS *my_read_naslist_file(char *file)
                c->ipaddr = ip_getaddr(hostnm);
                strNcpy(c->nastype, nastype, sizeof(c->nastype));
                strNcpy(c->shortname, shortnm, sizeof(c->shortname));
-               strNcpy(c->longname, ip_hostname(c->ipaddr),
-                       sizeof(c->longname));
-
+               ip_hostname(c->longname, sizeof(c->longname), c->ipaddr);
                c->next = cl;
                cl = c;
        }
@@ -342,11 +340,11 @@ static const char *nasname(uint32_t ipaddr)
 /*
  *     Print address of NAS.
  */
-static const char *hostname(uint32_t ipaddr)
+static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
 {
        if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
                return "";
-       return ip_hostname(ipaddr);
+       return ip_hostname(buf, buflen, ipaddr);
 }
 
 
@@ -380,6 +378,7 @@ int main(int argc, char **argv)
        int hdrdone = 0;
        char inbuf[128];
        char myname[128];
+       char othername[256];
        char session_id[16];
        int fingerd = 0;
        int showlocal = 0;
@@ -550,7 +549,7 @@ int main(int argc, char **argv)
                                portind, portno,
                                dotime(rt.time),
                                nasname(rt.nas_address),
-                               hostname(rt.framed_address), eol);
+                               hostname(othername, sizeof(othername), rt.framed_address), eol);
                        else
                            printf((rawoutput == 0? rfmt2: rfmt2r),
                                rt.login,
@@ -558,7 +557,7 @@ int main(int argc, char **argv)
                                proto(rt.proto, rt.porttype),
                                dotime(rt.time),
                                nasname(rt.nas_address),
-                               hostname(rt.framed_address), eol);
+                               hostname(othername, sizeof(othername), rt.framed_address), eol);
                }
        }
        fflush(stdout);
index c4d1f87..e88c2ba 100644 (file)
@@ -74,7 +74,7 @@ int main(int argc, char **argv)
        if (nas) ip = nas->ipaddr;
 
        printf("radzap: zapping termserver %s, port %d",
-               ip_hostname(ip), nas_port);
+               ip_hostname(buf, sizeof(buf), ip), nas_port);
        if (user) printf(", user %s", user);
        printf("\n");
 
index 59d77ae..3e32858 100644 (file)
@@ -650,7 +650,6 @@ static int file_accounting(REQUEST *request)
        FILE            *outfd;
        char            nasname[128];
        char            buffer[512];
-       char            *s;
        VALUE_PAIR      *pair;
        uint32_t        nas;
        NAS             *cl;
@@ -689,10 +688,7 @@ static int file_accounting(REQUEST *request)
        }
 
        if (cl == NULL) {
-               s = ip_hostname(nas);
-               if (strlen(s) >= sizeof(nasname) || strchr(s, '/'))
-                       return -1;
-               strcpy(nasname, s);
+               ip_hostname(nasname, sizeof(nasname), nas);
        }
 
        /*