changeset 1581:bad894bd648f

* src/bootp_support.c: * src/dhcp_prot.c: Close sockets before exiting so we don't leak them. * src/dhcp_prot.c (new_lease): Use a smaller infinite so we don't get a compiler warning.
author asl
date Sun, 11 Apr 2004 11:23:51 +0000
parents e4c676dcd9b0
children 410822b9fdd5
files packages/net/common/current/ChangeLog packages/net/common/current/src/bootp_support.c packages/net/common/current/src/dhcp_prot.c
diffstat 3 files changed, 106 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/packages/net/common/current/ChangeLog
+++ b/packages/net/common/current/ChangeLog
@@ -1,3 +1,11 @@
+2004-04-11  Andrew Lunn  <andrew.lunn@ascom.ch>
+
+	* src/bootp_support.c: 
+	* src/dhcp_prot.c: Close sockets before exiting so we don't leak
+	them.
+	* src/dhcp_prot.c (new_lease): Use a smaller infinite so we don't get
+	a compiler warning.
+	
 2004-02-27     Robert Chenault <robertchenault@yahoo.com>
 
         * cdl/net.cdl:
--- a/packages/net/common/current/src/bootp_support.c
+++ b/packages/net/common/current/src/bootp_support.c
@@ -86,12 +86,12 @@ do_bootp(const char *intf, struct bootp 
     struct ifreq ifr;
     struct sockaddr_in cli_addr, serv_addr, bootp_server_addr;
     struct ecos_rtentry route;
-    int s, addrlen;
+    int s=-1, addrlen;
     int one = 1;
     struct bootp bootp_xmit;
     unsigned char mincookie[] = {99,130,83,99,255} ;
     struct timeval tv;
-    cyg_bool_t retcode = true;
+    cyg_bool_t retcode = false;
 
     // Ensure clean slate
     cyg_route_reinit();  // Force any existing routes to be forgotten
@@ -99,12 +99,12 @@ do_bootp(const char *intf, struct bootp 
     s = socket(AF_INET, SOCK_DGRAM, 0);
     if (s < 0) {
         perror("socket");
-        return false;
+        goto out;
     }
 
     if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) {
         perror("setsockopt");
-        return false;
+        goto out;
     }
 
     addrp = (struct sockaddr_in *) &ifr.ifr_addr;
@@ -117,30 +117,30 @@ do_bootp(const char *intf, struct bootp 
     strcpy(ifr.ifr_name, intf);
     if (ioctl(s, SIOCSIFADDR, &ifr)) {
         perror("SIOCSIFADDR");
-        return false;
+        goto out;
     }
 
     if (ioctl(s, SIOCSIFNETMASK, &ifr)) {
         perror("SIOCSIFNETMASK");
-        return false;
+        goto out;
     }
 
     /* the broadcast address is 255.255.255.255 */
     memset(&addrp->sin_addr, 255, sizeof(addrp->sin_addr));
     if (ioctl(s, SIOCSIFBRDADDR, &ifr)) {
         perror("SIOCSIFBRDADDR");
-        return false;
+        goto out;
     }
 
     ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
     if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
         perror("SIOCSIFFLAGS");
-        return false;
+        goto out;
     }
 
     if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
         perror("SIOCGIFHWADDR");
-        return false;
+        goto out;
     }
 
     // Set up routing
@@ -162,7 +162,7 @@ do_bootp(const char *intf, struct bootp 
     if (ioctl(s, SIOCADDRT, &route)) {
         if (errno != EEXIST) {
             perror("SIOCADDRT 3");
-            return false;
+            goto out;
         }
     }
 
@@ -173,15 +173,15 @@ do_bootp(const char *intf, struct bootp 
     
     if(bind(s, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
         perror("bind error");
-        return false;
+        goto out;
     }
     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
         perror("setsockopt SO_REUSEADDR");
-        return false;
+        goto out;
     }
     if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
         perror("setsockopt SO_REUSEPORT");
-        return false;
+        goto out;
     }
     
     memset((char *) &serv_addr, 0, sizeof(serv_addr));
@@ -193,7 +193,7 @@ do_bootp(const char *intf, struct bootp 
     bzero(&bootp_xmit, sizeof(bootp_xmit));
     if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
         perror("SIOCGIFHWADDR");
-        return false;
+        goto out;
     }
     bootp_xmit.bp_htype = HTYPE_ETHERNET;
     bootp_xmit.bp_hlen = IFHWADDRLEN;
@@ -207,13 +207,16 @@ do_bootp(const char *intf, struct bootp 
     if(sendto(s, &bootp_xmit, sizeof(struct bootp), 0, 
               (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
         perror("sendto error");
-        return false;
+        goto out;
     }
 
     tv.tv_sec = 5;
     tv.tv_usec = 0;
     setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
-
+    
+    // Everything passed here is a success.
+    retcode = true;
+    
     addrlen = sizeof(bootp_server_addr);
     if (recvfrom(s, recv, sizeof(struct bootp), 0,
                  (struct sockaddr *)&bootp_server_addr, &addrlen) < 0) {
@@ -239,11 +242,12 @@ do_bootp(const char *intf, struct bootp 
     ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
     if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
         perror("SIOCSIFFLAGS");
-        return false;
     }
 
+ out:
     // All done with socket
-    close(s);
+    if (s != -1)
+      close(s);
     return retcode;
 }
 
@@ -433,21 +437,22 @@ init_net(const char *intf, struct bootp 
 {
     struct sockaddr_in *addrp;
     struct ifreq ifr;
-    int s;
+    int s=-1;
     int one = 1;
     struct ecos_rtentry route;
     struct in_addr netmask, gateway;
     unsigned int length;
-
+    int retcode = false;
+    
     s = socket(AF_INET, SOCK_DGRAM, 0);
     if (s < 0) {
         perror("socket");
-        return false;
+        goto out;
     }
 
     if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) {
         perror("setsockopt");
-        return false;
+        goto out;
     }
 
     addrp = (struct sockaddr_in *) &ifr.ifr_addr;
@@ -462,7 +467,7 @@ init_net(const char *intf, struct bootp 
     strcpy(ifr.ifr_name, intf);
     if (ioctl(s, SIOCSIFADDR, &ifr)) {
         perror("SIOCIFADDR");
-        return false;
+        goto out;
     }
 
     length = sizeof(addrp->sin_addr);
@@ -470,14 +475,14 @@ init_net(const char *intf, struct bootp 
         netmask = addrp->sin_addr;
         if (ioctl(s, SIOCSIFNETMASK, &ifr)) {
             perror("SIOCSIFNETMASK");
-            return false;
+            goto out;
         }
         // Must do this again so that [sub]netmask (and so default route)
         // is taken notice of.
         addrp->sin_addr = bp->bp_yiaddr;  // The address BOOTP gave us
         if (ioctl(s, SIOCSIFADDR, &ifr)) {
             perror("SIOCIFADDR 2");
-            return false;
+            goto out;
         }
     }
 
@@ -485,7 +490,7 @@ init_net(const char *intf, struct bootp 
     if (get_bootp_option(bp, TAG_IP_BROADCAST, &addrp->sin_addr,&length)) {
         if (ioctl(s, SIOCSIFBRDADDR, &ifr)) {
             perror("SIOCSIFBRDADDR");
-            return false;
+            goto out;
         }
         // Do not re-set the IFADDR after this; doing *that* resets the
         // BRDADDR to the default!
@@ -494,7 +499,7 @@ init_net(const char *intf, struct bootp 
     ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
     if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
         perror("SIOCSIFFLAGS");
-        return false;
+        goto out;
     }
 
     // Set up routing
@@ -526,12 +531,13 @@ init_net(const char *intf, struct bootp 
                   inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
                 if (errno != EEXIST) {
                     perror("SIOCADDRT 3");
-                    return false;
+                    goto out;
                 }
             }
         }
     }
-    close(s);
+    retcode = true;
+    
 #ifdef CYGINT_ISO_DNS
     {
 #define MAX_IP_ADDR_LEN 16
@@ -588,7 +594,10 @@ init_net(const char *intf, struct bootp 
         }
     }
 #endif /* CYGNUM_NET_SNTP_UNICAST_MAXDHCP */
-    return true;
+ out:
+    if (s != -1) 
+      close(s);
+    return retcode;
 }
 
 #ifdef INET6
@@ -596,15 +605,16 @@ extern const struct in6_addr in6mask128;
 cyg_bool_t
 init_net_IPv6(const char *intf, struct bootp *bp, char *prefix)
 {
-    int s;
+    int s =-1 ;
     struct in6_aliasreq in6_addr;
     char in6_ip[128];
+    int retcode = false;
 
     // Set up non link-layer address
     s = socket(AF_INET6, SOCK_DGRAM, 0);
     if (s < 0) {
         perror("socket IPv6");
-        return false;
+        goto out;
     }
     bzero(&in6_addr, sizeof(in6_addr));
     diag_sprintf(in6_ip, "%s::%s", prefix, inet_ntoa(bp->bp_yiaddr));
@@ -612,7 +622,7 @@ init_net_IPv6(const char *intf, struct b
     in6_addr.ifra_addr.sin6_family = AF_INET6;
     if (!inet_pton(AF_INET6, in6_ip, (char *)&in6_addr.ifra_addr.sin6_addr)) {
         diag_printf("Can't set IPv6 address: %s\n", in6_ip);
-        return false;
+        goto out;
     }
     in6_addr.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
     in6_addr.ifra_prefixmask.sin6_family = AF_INET6;
@@ -622,10 +632,13 @@ init_net_IPv6(const char *intf, struct b
     in6_addr.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
     if (ioctl(s, SIOCAIFADDR_IN6, &in6_addr)) {
         perror("SIOCAIFADDR_IN6");
-        return false;
+        goto out;
     }
-    close(s);
-    return true;
+    retcode = true;
+ out:
+    if (s != -1) 
+      close(s);
+    return retcode;
 }
 #endif
 
--- a/packages/net/common/current/src/dhcp_prot.c
+++ b/packages/net/common/current/src/dhcp_prot.c
@@ -285,11 +285,12 @@ unset_tag( struct bootp *ppkt,
 static int
 bring_half_up(const char *intf, struct ifreq *ifrp )
 {
-    int s;
+    int s = -1;
     int one = 1;
 
     struct sockaddr_in *addrp;
     struct ecos_rtentry route;
+    int retcode = false;
 
     // Ensure clean slate
     cyg_route_reinit();  // Force any existing routes to be forgotten
@@ -297,12 +298,12 @@ bring_half_up(const char *intf, struct i
     s = socket(AF_INET, SOCK_DGRAM, 0);
     if (s < 0) {
         perror("socket");
-        return false;
+        goto out;
     }
 
     if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) {
         perror("setsockopt");
-        return false;
+        goto out;
     }
 
     addrp = (struct sockaddr_in *) &ifrp->ifr_addr;
@@ -315,30 +316,30 @@ bring_half_up(const char *intf, struct i
     strcpy(ifrp->ifr_name, intf);
     if (ioctl(s, SIOCSIFADDR, ifrp)) { /* set ifnet address */
         perror("SIOCSIFADDR");
-        return false;
+        goto out;
     }
 
     if (ioctl(s, SIOCSIFNETMASK, ifrp)) { /* set net addr mask */
         perror("SIOCSIFNETMASK");
-        return false;
+        goto out;
     }
 
     /* the broadcast address is 255.255.255.255 */
     memset(&addrp->sin_addr, 255, sizeof(addrp->sin_addr));
     if (ioctl(s, SIOCSIFBRDADDR, ifrp)) { /* set broadcast addr */
         perror("SIOCSIFBRDADDR");
-        return false;
+        goto out;
     }
 
     ifrp->ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
     if (ioctl(s, SIOCSIFFLAGS, ifrp)) { /* set ifnet flags */
         perror("SIOCSIFFLAGS up");
-        return false;
+        goto out;
     }
 
     if (ioctl(s, SIOCGIFHWADDR, ifrp) < 0) { /* get MAC address */
         perror("SIOCGIFHWADDR 1");
-        return false;
+        goto out;
     }
 
     // Set up routing
@@ -362,13 +363,15 @@ bring_half_up(const char *intf, struct i
     if (ioctl(s, SIOCADDRT, &route)) { /* add route */
         if (errno != EEXIST) {
             perror("SIOCADDRT 3");
-            return false;
+            goto out;
         }
     }
+    retcode = true;
+ out:
+    if (s != -1) 
+      close(s);
 
-    close(s);
-
-    return true;
+    return retcode;
 }
 
 
@@ -473,9 +476,9 @@ static inline void new_lease( struct boo
         tag = 0xffffffff;
 
     if ( 0xffffffff == tag ) {
-        lease->expiry = 0xffffffffffffffff;
-        lease->t2     = 0xffffffffffffffff;
-        lease->t1     = 0xffffffffffffffff;
+        lease->expiry = 0xffffffff;
+        lease->t2     = 0xffffffff;
+        lease->t1     = 0xffffffff;
         return; // it's an infinite lease, hurrah!
     }
 
@@ -593,7 +596,7 @@ do_dhcp(const char *intf, struct bootp *
 {
     struct ifreq ifr;
     struct sockaddr_in cli_addr, broadcast_addr, server_addr, rx_addr;
-    int s, addrlen;
+    int s = -1, addrlen;
     int one = 1;
     unsigned char mincookie[] = {99,130,83,99,255} ;
     struct timeval tv;
@@ -638,12 +641,12 @@ do_dhcp(const char *intf, struct bootp *
     s = socket(AF_INET, SOCK_DGRAM, 0);
     if (s < 0) {
         perror("socket");
-        return false;
+        goto out;
     }
 
     if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) {
         perror("setsockopt");
-        return false;
+        goto out;
     }
 
     memset((char *) &cli_addr, 0, sizeof(cli_addr));
@@ -666,15 +669,15 @@ do_dhcp(const char *intf, struct bootp *
 
     if(bind(s, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
         perror("bind error");
-        return false;
+        goto out;
     }
     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
         perror("setsockopt SO_REUSEADDR");
-        return false;
+        goto out;
     }
     if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
         perror("setsockopt SO_REUSEPORT");
-        return false;
+        goto out;
     }
     
     // Now, we can launch into the DHCP state machine.  I think this will
@@ -687,7 +690,7 @@ do_dhcp(const char *intf, struct bootp *
     strcpy(&ifr.ifr_name[0], intf);
     if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
         perror("SIOCGIFHWADDR 2");
-        return false;
+        goto out;
     }
 
     // Choose from scratch depending on ifr_hwaddr...[]
@@ -956,7 +959,8 @@ do_dhcp(const char *intf, struct bootp *
 
             // All done with socket
             close(s);
-
+            s = -1;
+            
             // Re-initialize the interface with the new state
             if ( DHCPSTATE_BOUND != oldstate ) {
                 // Then need to go down and up
@@ -967,7 +971,7 @@ do_dhcp(const char *intf, struct bootp *
                     if (!init_net(intf, res)) {
                         do_dhcp_down_net( intf, res, pstate, lease );
                         *pstate = DHCPSTATE_FAILED;
-                        return false;
+                        goto out;
                     }
                 }
             }
@@ -1204,6 +1208,8 @@ do_dhcp(const char *intf, struct bootp *
         case DHCPSTATE_BOOTP_FALLBACK:
             // All done with socket
             close(s);
+            s = -1;
+            
             // And no lease should have become active, but JIC
             no_lease( lease );
             // Re-initialize the interface with the new state
@@ -1216,7 +1222,7 @@ do_dhcp(const char *intf, struct bootp *
                     if (!init_net(intf, res)) {
                         do_dhcp_down_net( intf, res, pstate, lease );
                         *pstate = DHCPSTATE_FAILED;
-                        return false;
+                        goto out;
                     }
                 }
             }
@@ -1292,7 +1298,10 @@ do_dhcp(const char *intf, struct bootp *
             return false;
         }
     }
-    /* NOTREACHED */
+out:
+    if (s != -1) 
+      close (s);
+    
     return false;
 }
 
@@ -1306,7 +1315,8 @@ do_dhcp_down_net(const char *intf, struc
 {
     struct sockaddr_in *addrp;
     struct ifreq ifr;
-    int s;
+    int s = -1;
+    int retcode = false;
 
     // Ensure clean slate
     cyg_route_reinit();  // Force any existing routes to be forgotten
@@ -1314,7 +1324,7 @@ do_dhcp_down_net(const char *intf, struc
     s = socket(AF_INET, SOCK_DGRAM, 0);
     if (s < 0) {
         perror("socket");
-        return false;
+        goto out;
     }
 
     addrp = (struct sockaddr_in *) &ifr.ifr_addr;
@@ -1335,7 +1345,7 @@ do_dhcp_down_net(const char *intf, struc
         strcpy(ifr.ifr_name, intf);
         if (ioctl(s, SIOCGIFADDR, &ifr)) {
             perror("SIOCGIFADDR 1");
-            return false;
+            goto out;
         }
     }
 
@@ -1352,6 +1362,7 @@ do_dhcp_down_net(const char *intf, struc
       s6 = socket(AF_INET6, SOCK_DGRAM, 0);
       if (s6 < 0) {
         perror("socket AF_INET6");
+        close (s);
         return false;
       }
       // Now delete the ipv6 addr
@@ -1372,16 +1383,19 @@ do_dhcp_down_net(const char *intf, struc
     ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
     if (ioctl(s, SIOCSIFFLAGS, &ifr)) { /* set ifnet flags */
         perror("SIOCSIFFLAGS down");
-        return false;
+        goto out;
     }
-
-    // All done with socket
-    close(s);
-
+    retcode = true;
+ 
     if ( 0 != *pstate ) // preserve initial state
         *pstate = DHCPSTATE_INIT;
 
-    return true;
+    
+ out:
+    if (s != -1)
+      close(s);
+    
+    return retcode;
 }
 
 // ------------------------------------------------------------------------