Hello All:
     Recently I use iptables to write a application and find that it seem has a memory leak, I am not sure whether it is really leak, and howto solved it, so ask for help from this, thank you for your help.
 
 
I check the memory leak by this way, it will not have problem when it run one time, but when I put iptables in my program to run thousand of rules, the menory increase by mega bytes...
 
step 1: 
Modify the Makefile and let the flag be this: COPT_FLAGS:=-O2 -g
 
step 2:
I put the   mtrace();  at the first of do_command fuction in iptables-standalone.c
like this:
......
 const char *modprobe = NULL;
 mtrace();
 memset(&fw, 0, sizeof(fw));
......
 
step 3:
run the program and check the leak
[root@dev01 iptables-1.2.5]# export MALLOC_TRACE=mymemory.log
[root@dev01 iptables-1.2.5]# ./iptables -t nat -A POSTROUTING -j MASQUERADE -s 192.168.1.200
[root@dev01 iptables-1.2.5]# mtrace ./iptables ./mymemory.log
Memory not freed:
-----------------
   Address     Size     Caller
0x08053038     0x2d  at /lib/ld-linux.so.2:(_dl_map_object+0x355)[0x400070d9]
0x08053070    0x22c  at /lib/ld-linux.so.2:(_dl_lookup_versioned_symbol_skip+0x9cb)[0x40008f4b]
0x080532a0     0x35  at /lib/ld-linux.so.2:(_dl_lookup_versioned_symbol_skip+0x9da)[0x40008f5a]
0x080532e0     0x2d  at /lib/ld-linux.so.2:(_dl_lookup_versioned_symbol_skip+0xb31)[0x400090b1]
0x08053318      0x4  at /lib/ld-linux.so.2:(_dl_map_object_deps+0x80f)[0x4000a45f]
0x08053328     0x34  at /home/dev/iptables-1.2.5/iptables.c:498
0x08053360    0x210  at /home/dev/iptables-1.2.5/iptables.c:966
0x08053578      0x4  at /home/dev/iptables-1.2.5/iptables.c:510
0x08053588      0x4  at /home/dev/iptables-1.2.5/iptables.c:510
0x08053c18     0xa4  at /home/dev/iptables-1.2.5/iptables.c:510
 
the relate source:
 
static void *
fw_calloc(size_t count, size_t size)
{
 void *p;
 if ((p = calloc(count, size)) == NULL) {  //498
  perror("iptables: calloc failed");
  exit(1);
 }
 return p;
}
 
static struct in_addr *
host_to_addr(const char *name, unsigned int *naddr)
{
 struct hostent *host;
 struct in_addr *addr;
 unsigned int i;
 *naddr = 0;
 if ((host = gethostbyname(name)) != NULL) {
  if (host->h_addrtype != AF_INET ||
      host->h_length != sizeof(struct in_addr))
   return (struct in_addr *) NULL;
  while (host->h_addr_list[*naddr] != (char *) NULL)
   (*naddr)++;
  addr = fw_calloc(*naddr, sizeof(struct in_addr));  // 510
  for (i = 0; i < *naddr; i++)
   inaddrcpy(&(addr[i]),
      (struct in_addr *) host->h_addr_list[i]);
  return addr;
 }
 return (struct in_addr *) NULL;
}
static struct option *
merge_options(struct option *oldopts, const struct option *newopts,
       unsigned int *option_offset)
{
 unsigned int num_old, num_new, i;
 struct option *merge;
 for (num_old = 0; oldopts[num_old].name; num_old++);
 for (num_new = 0; newopts[num_new].name; num_new++);
 global_option_offset += OPTION_OFFSET;
 *option_offset = global_option_offset;
 merge = malloc(sizeof(struct option) * (num_new + num_old + 1)); //966
 memcpy(merge, oldopts, num_old * sizeof(struct option));
 for (i = 0; i < num_new; i++) {
  merge[num_old + i] = newopts[i];
  merge[num_old + i].val += *option_offset;
 }
 memset(merge + num_old + num_new, 0, sizeof(struct option));
 return merge;
}
 

Best Regards.
LSQ

TEL:13500011809
E-mail:[EMAIL PROTECTED]
Homepage:www.freelsq.org


Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com

Reply via email to