reduce service_to_port duplication
PATCH 1/1 reduce service_to_port duplication
The service_to_port function is used in a number of places, and could benefit from some centralization instead of being duplicated everywhere.
Phil
extensions/libip6t_multiport.c +0 -11
@@ -50,17 +50,6 @@ } } -static int -service_to_port (const char *name,const char *proto) -{ -struct servent *service; - -if ((service =getservbyname (name, proto)) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_port (const char *port,const char *proto) {
extensions/libip6t_tcp.c +1 -12
@@ -38,24 +38,13 @@ {0} }; -static int -service_to_port (const char *name) -{ -struct servent *service; - -if ((service =getservbyname (name,"tcp" )) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_tcp_port (const char *port) {unsigned int portnum;if (string_to_number (port,0 ,65535 , &portnum) !=-1 || - (portnum =service_to_port (port)) !=-1 ) + (portnum =service_to_port (port,"tcp" )) !=-1 )return (u_int16_t )portnum; exit_error(PARAMETER_PROBLEM,
extensions/libip6t_udp.c +1 -12
@@ -30,24 +30,13 @@ {0} }; -static int -service_to_port (const char *name) -{ -struct servent *service; - -if ((service =getservbyname (name,"udp" )) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_udp_port (const char *port) {unsigned int portnum;if (string_to_number (port,0 ,65535 , &portnum) !=-1 || - (portnum =service_to_port (port)) !=-1 ) + (portnum =service_to_port (port,"udp" )) !=-1 )return (u_int16_t )portnum; exit_error(PARAMETER_PROBLEM,
extensions/libipt_dccp.c +1 -12
@@ -56,17 +56,6 @@ { .name = 0 } }; -static int -service_to_port (const char *name) -{ -struct servent *service; - -if ((service =getservbyname (name,"dccp" )) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_dccp_port (const char *port) {
@@ -74,7 +63,7 @@DEBUGP ("%s\n" , port);if (string_to_number (port,0 ,65535 , &portnum) !=-1 || - (portnum =service_to_port (port)) !=-1 ) + (portnum =service_to_port (port,"dccp" )) !=-1 )return (u_int16_t )portnum; exit_error(PARAMETER_PROBLEM,
extensions/libipt_mport.c +0 -11
@@ -33,17 +33,6 @@ {0} }; -static int -service_to_port (const char *name,const char *proto) -{ -struct servent *service; - -if ((service =getservbyname (name, proto)) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_port (const char *port,const char *proto) {
extensions/libipt_multiport.c +0 -11
@@ -68,17 +68,6 @@ } } -static int -service_to_port (const char *name,const char *proto) -{ -struct servent *service; - -if ((service =getservbyname (name, proto)) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_port (const char *port,const char *proto) {
extensions/libipt_sctp.c +1 -12
@@ -79,17 +79,6 @@ { .name = 0 } }; -static int -service_to_port (const char *name) -{ -struct servent *service; - -if ((service =getservbyname (name,"sctp" )) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_sctp_port (const char *port) {
@@ -97,7 +86,7 @@DEBUGP ("%s\n" , port);if (string_to_number (port,0 ,65535 , &portnum) !=-1 || - (portnum =service_to_port (port)) !=-1 ) + (portnum =service_to_port (port,"sctp" )) !=-1 )return (u_int16_t )portnum; exit_error(PARAMETER_PROBLEM,
extensions/libipt_tcp.c +1 -12
@@ -38,24 +38,13 @@ {0} }; -static int -service_to_port (const char *name) -{ -struct servent *service; - -if ((service =getservbyname (name,"tcp" )) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_tcp_port (const char *port) {unsigned int portnum;if (string_to_number (port,0 ,65535 , &portnum) !=-1 || - (portnum =service_to_port (port)) !=-1 ) + (portnum =service_to_port (port,"tcp" )) !=-1 )return (u_int16_t )portnum; exit_error(PARAMETER_PROBLEM,
extensions/libipt_udp.c +1 -12
@@ -30,24 +30,13 @@ {0} }; -static int -service_to_port (const char *name) -{ -struct servent *service; - -if ((service =getservbyname (name,"udp" )) !=NULL ) -return ntohs ((unsigned short ) service->s_port); - -return -1 ; -} -static u_int16_t parse_udp_port (const char *port) {unsigned int portnum;if (string_to_number (port,0 ,65535 , &portnum) !=-1 || - (portnum =service_to_port (port)) !=-1 ) + (portnum =service_to_port (port,"udp" )) !=-1 )return (u_int16_t )portnum; exit_error(PARAMETER_PROBLEM,
include/ip6tables.h +1 -0
@@ -133,6 +133,7 @@extern void register_match6 (struct ip6tables_match *me);extern void register_target6 (struct ip6tables_target *me); +extern int service_to_port (const char *name,const char *proto);extern int do_command6 (int argc,char *argv[],char **table,ip6tc_handle_t *handle);/* Keeping track of external matches and targets: linked lists. */
include/iptables.h +1 -0
@@ -151,6 +151,7 @@extern void register_match (struct iptables_match *me);extern void register_target (struct iptables_target *me); +extern int service_to_port (const char *name,const char *proto);extern struct in_addr *dotted_to_addr (const char *dotted);extern char *addr_to_dotted (const struct in_addr *addrp);extern char *addr_to_anyname (const struct in_addr *addr);
ip6tables.c +11 -0
@@ -245,6 +245,17 @@return NULL ; } +int +service_to_port (const char *name,const char *proto) +{ +struct servent *service; + +if ((service =getservbyname (name, proto)) !=NULL ) +return ntohs ((unsigned short ) service->s_port); + +return -1 ; +} +static void in6addrcpy (struct in6_addr *dst,struct in6_addr *src) {
iptables.c +11 -0
@@ -249,6 +249,17 @@return NULL ; } +int +service_to_port (const char *name,const char *proto) +{ +struct servent *service; + +if ((service =getservbyname (name, proto)) !=NULL ) +return ntohs ((unsigned short ) service->s_port); + +return -1 ; +} +struct in_addr *dotted_to_addr (const char *dotted) {
Patrick McHardy
AppliedQuoted 2 lines · Phil Oester
The
service_to_portfunction is used in a number of places, and could benefit from some centralization instead of being duplicated everywhere.
Applied, thanks Phil.