memory not freed?

Wilson Yang wyang at neoninc.org
Wed Dec 22 00:58:20 UTC 2010


Hi,

 

Not sure if dhcp-bugs or dhcp-hacker is right email alias to use. Apologize
if any inconvenience might have caused.

I am seeing some weird behavior using the code below.

The application polls dhcpd every 10 seconds and ~20 bytes not freed up per
poll. I am using dhcp-3.1-ESV right now.

Linux "ps" command shows memory usage keep adding up per poll.

Could someone take a look at if there are programming mistakes ?

 

The memory seems to be held by dhcpctl_open_object(), and use the
dhcpctl_object_remove() and omapi_object_dereference() do not help. Any
input/suggestion are welcome.

Thanks in advance.

 

Wilson Yang

 

==========================================================================

 

#include <stdio.h>

#include <string.h>

#include <sys/ioctl.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <linux/if.h>

 

#include <stdarg.h>

#include <sys/time.h>

#include <netinet/in.h>

 

#include <dhcpctl.h>

 

#define MAX_CLIENTS  254

#define DHCPD_PORT   7911

#define STATE_FREE   0x01

#define STATE_ACTIVE 0x02

 

#define DEBUG 0

 

int main (int argc, char **argv)

{

        int i, j, s;

        struct ifreq buffer;

        struct sockaddr sa;

       struct in_addr my_addr, my_addr_org;

       char hwaddr[6];

 

        dhcpctl_data_string ipaddrstring = NULL;

        dhcpctl_data_string value = NULL;

        dhcpctl_handle connection = NULL;

        dhcpctl_handle lease;

        isc_result_t status, waitstatus;

       int binding_state;

       char *leased_ip;

       char *data;

 

        memset(&buffer, 0, sizeof(buffer));

 

       strcpy(buffer.ifr_name, "eth0");

        s = socket(PF_INET, SOCK_DGRAM, 0);

 

        ioctl(s, SIOCGIFADDR, &buffer);

 

        sa = (struct sockaddr)(buffer.ifr_addr);

 

       my_addr.s_addr = 0;

        printf("MY IP : ");

        for( i = 2; i < 6; i++ )

        {

              printf("%d.", (unsigned char)buffer.ifr_addr.sa_data[i]);

              my_addr.s_addr = my_addr.s_addr | ((unsigned
char)(buffer.ifr_addr.sa_data[i]) << ((i - 2)*8));

        }

        printf("\n");

       my_addr_org = my_addr;

 

        status = dhcpctl_initialize ();

        if (status != ISC_R_SUCCESS) {

                fprintf (stderr, "dhcpctl_initialize: %s\n",

                         isc_result_totext (status));

                return(1);

        }

 

        status = dhcpctl_connect (&connection, "127.0.0.1", DHCPD_PORT, 0);

 

        if (status != ISC_R_SUCCESS) {

                fprintf (stderr, "dhcpctl_connect: %s\n",

                         isc_result_totext (status));

              /* return if server is not responding. */

                return(1);

        }

 

       if (!connection) {

                fprintf (stderr, "connection: is NULL \n");

              return(1);

       }

 

       while (1)

       {

 

       /* not to exceed 255 or subnet range. */

       for (i = 0; i < MAX_CLIENTS; i++)

       {

              status = dhcpctl_new_object (&lease, connection, "lease");

              /* seems to return error codes here, not sure why! */

              if (status != ISC_R_SUCCESS) {

                     fprintf (stderr, "dhcpctl_new_object: %s\n",

                     isc_result_totext (status));

              }

 

              memset (&ipaddrstring, 0, sizeof(ipaddrstring));

 

              status = omapi_data_string_new (&ipaddrstring, 4, MDL);

              if (status != ISC_R_SUCCESS) {

                     fprintf (stderr, "omapi_data_string_new: %s\n",

                     isc_result_totext (status));

              }

 

              my_addr.s_addr = (my_addr_org.s_addr & htonl(0xffffff00)) +
htonl(i);

if (DEBUG) printf("%d 0x%x\n", __LINE__, &my_addr.s_addr);

               memcpy(ipaddrstring->value, &my_addr.s_addr, 4);

 

              status = dhcpctl_set_value (lease, ipaddrstring,
"ip-address");

              if (status != ISC_R_SUCCESS) {

                     fprintf (stderr, "dhcpctl_set_value: %s\n",

                     isc_result_totext (status));

              }

if (DEBUG) printf("%d i=%d \n", __LINE__, i);

 

              status = dhcpctl_open_object(lease, connection, 0);

              if (status != ISC_R_SUCCESS) {

                     fprintf (stderr, "create: dhcpctl_open_object: %s\n",

                     isc_result_totext (status));

              }

if (DEBUG) printf("%d i=%d \n", __LINE__, i);

 

              status = dhcpctl_wait_for_completion (lease, &waitstatus);

              if (status != ISC_R_SUCCESS) {

                     fprintf (stderr, "dhcpctl_wait_for_completion: %s\n",

                     isc_result_totext (status));

              }

if (DEBUG) printf("%d i=%d \n", __LINE__, i);

 

              dhcpctl_data_string_dereference(&ipaddrstring, MDL);

if (DEBUG) printf("%d i=%d \n", __LINE__, i);

 

              if (waitstatus == ISC_R_SUCCESS) {

                     dhcpctl_get_value (&value, lease, "state");

                     memcpy(&binding_state, value->value, value->len);

                     dhcpctl_data_string_dereference(&value, MDL);

if (DEBUG) printf("value->value 0x%x, value-len 0x%x\n", value->value,
value->len);

 

                     if (STATE_ACTIVE == ntohl(binding_state)) {

                           leased_ip = (char *)inet_ntoa(my_addr);

                           fprintf(stdout, "%s leased! ", leased_ip);

                     }

                     /* clean up and ready for next one! */

                     status = dhcpctl_object_remove(connection, lease);

                     if (status != ISC_R_SUCCESS) {

                                   fprintf (stderr,
"dhcpctl_object_remove:%s\n",

                           isc_result_totext (status));

                     }

if (DEBUG) printf("%d i=%d \n", __LINE__, i);

                     status = dhcpctl_wait_for_completion (lease,
&waitstatus);

                     if (status != ISC_R_SUCCESS) {

                           fprintf (stderr,

                           "%d remove: dhcpctl_wait_for_completion:%s\n",

                           __LINE__, isc_result_totext (status));

                     }

if (DEBUG) printf("%d i=%d \n", __LINE__, i);

              } else {

                     if (DEBUG) printf("%d i=%d wait error !\n", __LINE__,
i);

              }

              omapi_object_dereference (&lease, MDL);

 

       } /* end of for loop */

if (DEBUG) printf("%d i=%d \n", __LINE__, i);

 

       fprintf(stdout, "\n");

       fflush(stdout);

 

       sleep(10);

       } /* end of while */

 

       return 0;

}

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.isc.org/pipermail/dhcp-hackers/attachments/20101221/ee86378b/attachment.html>


More information about the dhcp-hackers mailing list