Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#267 closed (fixed)

About changeset 8409

Reported by: samueldg@… Owned by: somebody
Keywords: Cc:

Description

Perhaps a bit shift is more adecuate.

Posible problems of this code:

Advantages:

  • Don't need to save many constant strings.
  • Shorter.
  • You can optimice more de variables too, really don't need "ipX" variables, you use them 1 time when initialice mask_aux.

try something as this:

char *loopmask; default value for 255.255.255.0 static char loopmask_aux[ 5 ] = ""; char *nmask = nvram_safe_get ("lan_netmask"); int ip0 = get_single_ip (nmask, 0); int ip1 = get_single_ip (nmask, 1); int ip2 = get_single_ip (nmask, 2); int ip3 = get_single_ip (nmask, 3); register int ones_counter;

DWORD mask_aux = ( ip0 << 24 )
( ip1 << 16 ) ( ip2 << 8 ) ip3;

for( ones_counter = 0; mask_aux; mask_aux << 1 ); sprintf( loopmask_aux, "0/%i", ones_counter ); loopmask = loopmask_aux;

Change History (3)

comment:1 Changed 6 years ago by Eko

  • Resolution set to fixed
  • Status changed from new to closed

Thanks. Did it new way.

	  int loopmask=0;
	  char *nmask = nvram_safe_get ("lan_netmask");  //assuming lan_netmask is valid

	  int ip[4] = { 0, 0, 0, 0 };

	  sscanf (nmask, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);

	  int n = 8;

	  for (--n; n >= 0; --n)  //test all 4 bytes in one pass
	   {
		if (ip[0] & 1 << n) loopmask++;
		if (ip[1] & 1 << n) loopmask++;
		if (ip[2] & 1 << n) loopmask++;
		if (ip[3] & 1 << n) loopmask++;
	   }

	  save2file
		("-A POSTROUTING -o %s -m pkttype --pkt-type broadcast -j RETURN\n",
		 lanface);
	  save2file ("-A POSTROUTING -o %s -s %s0/%d -d %s0/%d -j MASQUERADE\n",
		 lanface, lan_cclass, loopmask, lan_cclass, loopmask);

comment:2 Changed 6 years ago by samueldg@…

Good!!

comment:3 Changed 3 years ago by anonymous

Milestone milestone1 deleted

Note: See TracTickets for help on using tickets.