Re: [PATCH v2 nft] Set/print base chain prios with textual names

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jun 07, 2018 at 12:28:16PM +0200, Máté Eckl wrote:
> Like this? I don't know how common or accepted is it to use inline struct
> definitions, but I think this is the only better way then the actual.
> 
> 	static const char *chain_prio2str(int prio)
> 	{
> 		static char pe[256];
> 		const int reach = 10;
> 		size_t i;
> 
> 		struct tmp {

instead of 'tmp', use struct chain_prio_tag or something.

> 			int val;
> 			const char *str;
> 		};
> 
> 		const static struct tmp prios[] = {
> 			{ NF_IP_PRI_RAW,      "raw" },
> 			{ NF_IP_PRI_MANGLE,   "mangle" },
> 			{ NF_IP_PRI_NAT_DST,  "dnat" },
> 			{ NF_IP_PRI_FILTER,   "filter" },
> 			{ NF_IP_PRI_SECURITY, "security" },
> 			{ NF_IP_PRI_NAT_SRC,  "snat" },
> 		};

Yes, but place it as static const away from the function body.

> 		pe[0] = 0;
> 		for (i = 0; i < sizeof(prios); ++i) {
> 			if (chain_prio2str_helper(pe, prios[i].val, prios[i].str, prio, reach))
> 				return pe;
> 		}
> 		sprintf(pe, "%d", prio);
> 		return pe;
> 	}
> 
> 
[...]
> > > +"rawprio"       { return PRIO_RAW; }
> > > +"mangleprio"    { return PRIO_MANGLE; }
> > > +"dstnatprio"    { return PRIO_NAT_DST; }
> > > +"filterprio"    { return PRIO_FILTER; }
> > > +"securityprio"  { return PRIO_SECURITY; }
> > > +"srcnatprio"    { return PRIO_NAT_SRC; }
> > 
> > Any way we can avoid defining tokens?
> 
> I just want to avoid strcmp-ing every name and associating their value manually.
> Is there a way, I can do this without tokens?
>
> If I use these without the 'prio' suffix as you suggested, some of them are also
> defined (snat, dnat), and the others may become quite general and thus reusable.
> 
> This would be them:
> 		"raw"       { return RAW; }
> 		"mangle"    { return MANGLE; }
> 		"filter"    { return FILTER; }
> 		"security"  { return SECURITY; }
> 
> The problem with this is that these keywords will cause problem where these are
> expected as strings (like filter chain definition...).

We don't really need tokens for this. Tokens are there to provide
structure for the grammar, which is not needed in this case. strcmp()
should be fine for this case.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux