Index: net/common/current//ChangeLog =================================================================== RCS file: /cvs/ecos/ecos-opt/net/net/common/current/ChangeLog,v retrieving revision 1.56 diff -u -r1.56 ChangeLog --- net/common/current//ChangeLog 9 Mar 2004 08:00:12 -0000 1.56 +++ net/common/current//ChangeLog 11 Apr 2004 11:21:33 -0000 @@ -1,3 +1,11 @@ +2004-04-11 Andrew Lunn + + * 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 * cdl/net.cdl: Index: net/common/current//src/bootp_support.c =================================================================== RCS file: /cvs/ecos/ecos-opt/net/net/common/current/src/bootp_support.c,v retrieving revision 1.4 diff -u -r1.4 bootp_support.c --- net/common/current//src/bootp_support.c 21 Oct 2003 17:50:58 -0000 1.4 +++ net/common/current//src/bootp_support.c 11 Apr 2004 11:21:34 -0000 @@ -86,12 +86,12 @@ 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 @@ 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 @@ 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 @@ if (ioctl(s, SIOCADDRT, &route)) { if (errno != EEXIST) { perror("SIOCADDRT 3"); - return false; + goto out; } } @@ -173,15 +173,15 @@ 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 @@ 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 @@ 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 @@ 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 @@ { 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ } } #endif /* CYGNUM_NET_SNTP_UNICAST_MAXDHCP */ - return true; + out: + if (s != -1) + close(s); + return retcode; } #ifdef INET6 @@ -596,15 +605,16 @@ 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 @@ 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 @@ 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 Index: net/common/current//src/dhcp_prot.c =================================================================== RCS file: /cvs/ecos/ecos-opt/net/net/common/current/src/dhcp_prot.c,v retrieving revision 1.15 diff -u -r1.15 dhcp_prot.c --- net/common/current//src/dhcp_prot.c 9 Mar 2004 08:00:12 -0000 1.15 +++ net/common/current//src/dhcp_prot.c 11 Apr 2004 11:21:36 -0000 @@ -285,11 +285,12 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ { 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 @@ 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 @@ 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 @@ 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 @@ // 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 @@ if (!init_net(intf, res)) { do_dhcp_down_net( intf, res, pstate, lease ); *pstate = DHCPSTATE_FAILED; - return false; + goto out; } } } @@ -1204,6 +1208,8 @@ 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 @@ if (!init_net(intf, res)) { do_dhcp_down_net( intf, res, pstate, lease ); *pstate = DHCPSTATE_FAILED; - return false; + goto out; } } } @@ -1292,7 +1298,10 @@ return false; } } - /* NOTREACHED */ +out: + if (s != -1) + close (s); + return false; } @@ -1306,7 +1315,8 @@ { 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 @@ 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 @@ strcpy(ifr.ifr_name, intf); if (ioctl(s, SIOCGIFADDR, &ifr)) { perror("SIOCGIFADDR 1"); - return false; + goto out; } } @@ -1352,6 +1362,7 @@ 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 @@ 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; } // ------------------------------------------------------------------------