From c8a8e368d5145a5ad5298fea04cfd79a764ce05a Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Sun, 17 Sep 2017 19:23:31 +0200 Subject: [PATCH] ipops: removed svn id, history; coherent indentation --- src/modules/ipops/api.c | 160 +++++++------- src/modules/ipops/api.h | 6 +- src/modules/ipops/detailed_ip_type.c | 2 - src/modules/ipops/detailed_ip_type.h | 2 - src/modules/ipops/ip_parser.c | 4 +- src/modules/ipops/ip_parser.h | 2 +- src/modules/ipops/ipops_mod.c | 311 ++++++++++++++------------- src/modules/ipops/ipops_pv.c | 8 +- src/modules/ipops/ipops_pv.h | 2 - src/modules/ipops/rfc1918_parser.h | 2 +- 10 files changed, 254 insertions(+), 245 deletions(-) diff --git a/src/modules/ipops/api.c b/src/modules/ipops/api.c index c8d80afc26b..e98f55b7478 100644 --- a/src/modules/ipops/api.c +++ b/src/modules/ipops/api.c @@ -1,7 +1,5 @@ /* - * $Id$ - * - * Functions that process IPOPS message + * Functions that process IPOPS message * * Copyright (C) 2012 Hugh Waite (crocodile-rcs.com) * @@ -17,8 +15,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ @@ -35,39 +33,39 @@ extern int _ip_is_in_subnet(char *ip1, size_t len1, enum enum_ip_type ip1_type, */ int ipopsapi_compare_ips(const str *const ip1, const str *const ip2) { - str string1 = *ip1; - str string2 = *ip2; - enum enum_ip_type ip1_type, ip2_type; - - switch(ip1_type = ip_parser_execute(string1.s, string1.len)) { - case(ip_type_error): - return -1; - break; - case(ip_type_ipv6_reference): - string1.s += 1; - string1.len -= 2; - ip1_type = ip_type_ipv6; - break; - default: - break; - } - switch(ip2_type = ip_parser_execute(string2.s, string2.len)) { - case(ip_type_error): - return -1; - break; - case(ip_type_ipv6_reference): - string2.s += 1; - string2.len -= 2; - ip2_type = ip_type_ipv6; - break; - default: - break; - } + str string1 = *ip1; + str string2 = *ip2; + enum enum_ip_type ip1_type, ip2_type; + + switch(ip1_type = ip_parser_execute(string1.s, string1.len)) { + case(ip_type_error): + return -1; + break; + case(ip_type_ipv6_reference): + string1.s += 1; + string1.len -= 2; + ip1_type = ip_type_ipv6; + break; + default: + break; + } + switch(ip2_type = ip_parser_execute(string2.s, string2.len)) { + case(ip_type_error): + return -1; + break; + case(ip_type_ipv6_reference): + string2.s += 1; + string2.len -= 2; + ip2_type = ip_type_ipv6; + break; + default: + break; + } - if (_compare_ips(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type)) - return 1; - else - return -1; + if (_compare_ips(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type)) + return 1; + else + return -1; } /** @@ -75,46 +73,46 @@ int ipopsapi_compare_ips(const str *const ip1, const str *const ip2) */ int ipopsapi_ip_is_in_subnet(const str *const ip1, const str *const ip2) { - str string1 = *ip1; - str string2 = *ip2; - enum enum_ip_type ip1_type, ip2_type; - char *cidr_pos = NULL; - int netmask = 0; - - switch(ip1_type = ip_parser_execute(string1.s, string1.len)) { - case(ip_type_error): - return -1; - break; - case(ip_type_ipv6_reference): - return -1; - break; - default: - break; - } - cidr_pos = string2.s + string2.len - 1; - while (cidr_pos > string2.s) - { - if (*cidr_pos == '/') break; - cidr_pos--; - } - if (cidr_pos == string2.s) return -1; - string2.len = (cidr_pos - string2.s); - netmask = atoi(cidr_pos+1); - switch(ip2_type = ip_parser_execute(string2.s, string2.len)) { - case(ip_type_error): - return -1; - break; - case(ip_type_ipv6_reference): - return -1; - break; - default: - break; - } - - if (_ip_is_in_subnet(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type, netmask)) - return 1; - else - return -1; + str string1 = *ip1; + str string2 = *ip2; + enum enum_ip_type ip1_type, ip2_type; + char *cidr_pos = NULL; + int netmask = 0; + + switch(ip1_type = ip_parser_execute(string1.s, string1.len)) { + case(ip_type_error): + return -1; + break; + case(ip_type_ipv6_reference): + return -1; + break; + default: + break; + } + cidr_pos = string2.s + string2.len - 1; + while (cidr_pos > string2.s) + { + if (*cidr_pos == '/') break; + cidr_pos--; + } + if (cidr_pos == string2.s) return -1; + string2.len = (cidr_pos - string2.s); + netmask = atoi(cidr_pos+1); + switch(ip2_type = ip_parser_execute(string2.s, string2.len)) { + case(ip_type_error): + return -1; + break; + case(ip_type_ipv6_reference): + return -1; + break; + default: + break; + } + + if (_ip_is_in_subnet(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type, netmask)) + return 1; + else + return -1; } /** @@ -122,10 +120,10 @@ int ipopsapi_ip_is_in_subnet(const str *const ip1, const str *const ip2) */ int ipopsapi_is_ip(const str * const ip) { - if (ip_parser_execute(ip->s, ip->len) != ip_type_error) - return 1; - else - return -1; + if (ip_parser_execute(ip->s, ip->len) != ip_type_error) + return 1; + else + return -1; } /** diff --git a/src/modules/ipops/api.h b/src/modules/ipops/api.h index d82cf1731fe..8e502cfb1e3 100644 --- a/src/modules/ipops/api.h +++ b/src/modules/ipops/api.h @@ -1,6 +1,4 @@ /* - * $Id$ - * * Functions that operate on IP addresses * * Copyright (C) 2012 Hugh Waite (crocodile-rcs.com) @@ -17,8 +15,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ diff --git a/src/modules/ipops/detailed_ip_type.c b/src/modules/ipops/detailed_ip_type.c index d5cd22a2667..a37c4aaccd2 100644 --- a/src/modules/ipops/detailed_ip_type.c +++ b/src/modules/ipops/detailed_ip_type.c @@ -1,6 +1,4 @@ /* - * $Id$ - * * Functions that operate on IP addresses * * Copyright (C) 2012 Hugh Waite (crocodile-rcs.com) diff --git a/src/modules/ipops/detailed_ip_type.h b/src/modules/ipops/detailed_ip_type.h index 36cab9a2e9f..35672b92972 100644 --- a/src/modules/ipops/detailed_ip_type.h +++ b/src/modules/ipops/detailed_ip_type.h @@ -1,6 +1,4 @@ /* - * $Id$ - * * Functions that operate on IP addresses * * Copyright (C) 2012 Hugh Waite (crocodile-rcs.com) diff --git a/src/modules/ipops/ip_parser.c b/src/modules/ipops/ip_parser.c index 25aea173a59..185aacfea82 100644 --- a/src/modules/ipops/ip_parser.c +++ b/src/modules/ipops/ip_parser.c @@ -26,14 +26,14 @@ enum enum_ip_type ip_parser_execute(const char *str, size_t len) p = str; pe = str+len; - + #line 36 "ip_parser.c" { cs = ip_parser_start; } #line 58 "ip_parser.rl" - + #line 43 "ip_parser.c" { if ( p == pe ) diff --git a/src/modules/ipops/ip_parser.h b/src/modules/ipops/ip_parser.h index 23ca248b72f..a1d9208bee7 100644 --- a/src/modules/ipops/ip_parser.h +++ b/src/modules/ipops/ip_parser.h @@ -1,7 +1,7 @@ /* * Warning: This file is auto generated from a ragel syntax (ip_parser.rl), * do not change it! - */ + */ #ifndef ip_parser_h #define ip_parser_h diff --git a/src/modules/ipops/ipops_mod.c b/src/modules/ipops/ipops_mod.c index 69b2adafbd4..bf6ef9b95b0 100644 --- a/src/modules/ipops/ipops_mod.c +++ b/src/modules/ipops/ipops_mod.c @@ -19,11 +19,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - * History: - * ------- - * 2015-03-31: Added srv_query function (rboisvert) - * 2011-07-29: Added a function to detect RFC1918 private IPv4 addresses (ibc) - * 2011-04-27: Initial version (ibc) */ /*! * \file @@ -74,16 +69,21 @@ MODULE_VERSION /* * Module internal functions */ -int _compare_ips(char*, size_t, enum enum_ip_type, char*, size_t, enum enum_ip_type); +int _compare_ips(char*, size_t, enum enum_ip_type, char*, size_t, + enum enum_ip_type); int _compare_ips_v4(struct in_addr *ip, char*, size_t); int _compare_ips_v6(struct in6_addr *ip, char*, size_t); int _ip_is_in_subnet(char *ip1, size_t len1, enum enum_ip_type ip1_type, char *ip2, size_t len2, enum enum_ip_type ip2_type, int netmask); -int _ip_is_in_subnet_v4(struct in_addr *ip, char *net, size_t netlen, int netmask); -int _ip_is_in_subnet_v6(struct in6_addr *ip, char *net, size_t netlen, int netmask); +int _ip_is_in_subnet_v4(struct in_addr *ip, char *net, size_t netlen, + int netmask); +int _ip_is_in_subnet_v6(struct in6_addr *ip, char *net, size_t netlen, + int netmask); int _ip_is_in_subnet_str(void *ip, enum enum_ip_type type, char *s, int slen); -int _ip_is_in_subnet_str_trimmed(void *ip, enum enum_ip_type type, char *b, char *e); -static int _detailed_ip_type(unsigned int _type, struct sip_msg* _msg, char* _s, char *_dst); +int _ip_is_in_subnet_str_trimmed(void *ip, enum enum_ip_type type, char *b, + char *e); +static int _detailed_ip_type(unsigned int _type, struct sip_msg* _msg, + char* _s, char *_dst); /* @@ -189,11 +189,11 @@ struct module_exports exports = { static int mod_init(void) { - /* turn detailed_ip_type relevant structures to netowork byte order so no need to - * transform each ip to host order before comparing */ - ipv4ranges_hton(); - ipv6ranges_hton(); - return 0; + /* turn detailed_ip_type relevant structures to netowork byte order + * so no need to transform each ip to host order before comparing */ + ipv4ranges_hton(); + ipv6ranges_hton(); + return 0; } @@ -204,39 +204,39 @@ static int mod_init(void) { */ static int fixup_detailed_ip_type(void** param, int param_no) { - if (param_no == 1) { - return fixup_spve_null(param, 1); - } - - if (param_no == 2) { - if (fixup_pvar_null(param, 1) != 0) { - LM_ERR("failed to fixup result pvar\n"); - return -1; - } - if (((pv_spec_t *) (*param))->setf == NULL) { - LM_ERR("result pvar is not writeble\n"); - return -1; - } - return 0; - } - - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; + if (param_no == 1) { + return fixup_spve_null(param, 1); + } + + if (param_no == 2) { + if (fixup_pvar_null(param, 1) != 0) { + LM_ERR("failed to fixup result pvar\n"); + return -1; + } + if (((pv_spec_t *) (*param))->setf == NULL) { + LM_ERR("result pvar is not writeble\n"); + return -1; + } + return 0; + } + + LM_ERR("invalid parameter number <%d>\n", param_no); + return -1; } static int fixup_free_detailed_ip_type(void** param, int param_no) { - if (param_no == 1) { - //LM_WARN("free function has not been defined for spve\n"); - return 0; - } + if (param_no == 1) { + //LM_WARN("free function has not been defined for spve\n"); + return 0; + } - if (param_no == 2) { - return fixup_free_pvar_null(param, 1); - } + if (param_no == 2) { + return fixup_free_pvar_null(param, 1); + } - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; + LM_ERR("invalid parameter number <%d>\n", param_no); + return -1; } /* @@ -244,7 +244,8 @@ static int fixup_free_detailed_ip_type(void** param, int param_no) */ /*! \brief Return 1 if both pure IP's are equal, 0 otherwise. */ -int _compare_ips(char *ip1, size_t len1, enum enum_ip_type ip1_type, char *ip2, size_t len2, enum enum_ip_type ip2_type) +int _compare_ips(char *ip1, size_t len1, enum enum_ip_type ip1_type, char *ip2, + size_t len2, enum enum_ip_type ip2_type) { struct in_addr in_addr1, in_addr2; struct in6_addr in6_addr1, in6_addr2; @@ -273,7 +274,8 @@ int _compare_ips(char *ip1, size_t len1, enum enum_ip_type ip1_type, char *ip2, case(ip_type_ipv6): if (inet_pton(AF_INET6, _ip1, &in6_addr1) != 1) return 0; if (inet_pton(AF_INET6, _ip2, &in6_addr2) != 1) return 0; - if (memcmp(in6_addr1.s6_addr, in6_addr2.s6_addr, sizeof(in6_addr1.s6_addr)) == 0) + if (memcmp(in6_addr1.s6_addr, in6_addr2.s6_addr, + sizeof(in6_addr1.s6_addr)) == 0) return 1; else return 0; @@ -305,8 +307,10 @@ int _compare_ips_v6(struct in6_addr *ip, char* ip2, size_t len2) memcpy(_ip2, ip2, len2); _ip2[len2] = '\0'; - if (inet_pton(AF_INET6, _ip2, &in6_addr2) != 1) return 0; - if (memcmp(ip->s6_addr, in6_addr2.s6_addr, sizeof(ip->s6_addr)) == 0) return 1; + if (inet_pton(AF_INET6, _ip2, &in6_addr2) != 1) + return 0; + if (memcmp(ip->s6_addr, in6_addr2.s6_addr, sizeof(ip->s6_addr)) == 0) + return 1; return 0; } @@ -352,11 +356,13 @@ int _ip_is_in_subnet(char *ip1, size_t len1, enum enum_ip_type ip1_type, for (i=0; i<16; i++) { if (netmask > ((i+1)*8)) ipv6_mask[i] = 0xFF; - else if (netmask > (i*8)) ipv6_mask[i] = ~(0xFF >> (netmask-(i*8))); + else if (netmask > (i*8)) + ipv6_mask[i] = ~(0xFF >> (netmask-(i*8))); else ipv6_mask[i] = 0x00; } for (i=0; i<16; i++) in6_addr1.s6_addr[i] &= ipv6_mask[i]; - if (memcmp(in6_addr1.s6_addr, in6_addr2.s6_addr, sizeof(in6_addr1.s6_addr)) == 0) + if (memcmp(in6_addr1.s6_addr, in6_addr2.s6_addr, + sizeof(in6_addr1.s6_addr)) == 0) return 1; else return 0; @@ -367,7 +373,8 @@ int _ip_is_in_subnet(char *ip1, size_t len1, enum enum_ip_type ip1_type, } } -int _ip_is_in_subnet_v4(struct in_addr *ip, char *net, size_t netlen, int netmask) +int _ip_is_in_subnet_v4(struct in_addr *ip, char *net, size_t netlen, + int netmask) { struct in_addr net_addr; char _net[INET6_ADDRSTRLEN]; @@ -387,7 +394,8 @@ int _ip_is_in_subnet_v4(struct in_addr *ip, char *net, size_t netlen, int netmas return 0; } -int _ip_is_in_subnet_v6(struct in6_addr *ip, char *net, size_t netlen, int netmask) +int _ip_is_in_subnet_v6(struct in6_addr *ip, char *net, size_t netlen, + int netmask) { struct in6_addr net_addr; char _net[INET6_ADDRSTRLEN]; @@ -444,47 +452,48 @@ int _ip_is_in_subnet_str(void *ip, enum enum_ip_type type, char *s, int slen) if (netmask == -1) { switch(type){ - case ip_type_ipv4: - if (_compare_ips_v4((struct in_addr *)ip, s, slen)) - return 1; - else - return -1; - break; - case ip_type_ipv6: - if (_compare_ips_v6((struct in6_addr *)ip, s, slen)) - return 1; - else - return -1; - break; - break; - default: - break; + case ip_type_ipv4: + if (_compare_ips_v4((struct in_addr *)ip, s, slen)) + return 1; + else + return -1; + break; + case ip_type_ipv6: + if (_compare_ips_v6((struct in6_addr *)ip, s, slen)) + return 1; + else + return -1; + break; + break; + default: + break; } } else { switch(type){ - case ip_type_ipv4: - if (_ip_is_in_subnet_v4((struct in_addr *)ip, s, slen,netmask)) - return 1; - else - return -1; - break; - case ip_type_ipv6: - if (_ip_is_in_subnet_v6((struct in6_addr *)ip, s, slen,netmask)) - return 1; - else - return -1; - break; - break; - default: - break; + case ip_type_ipv4: + if (_ip_is_in_subnet_v4((struct in_addr *)ip, s, slen,netmask)) + return 1; + else + return -1; + break; + case ip_type_ipv6: + if (_ip_is_in_subnet_v6((struct in6_addr *)ip, s, slen,netmask)) + return 1; + else + return -1; + break; + break; + default: + break; } } return 0; } -int _ip_is_in_subnet_str_trimmed(void *ip, enum enum_ip_type type, char *b, char *e) +int _ip_is_in_subnet_str_trimmed(void *ip, enum enum_ip_type type, char *b, + char *e) { while(bsetf(_msg, &dst->pvp, (int)EQ_T, &val); - return 1; + str string; + pv_spec_t *dst; + pv_value_t val; + char *res; + unsigned int assumed_type; + + if (_s == NULL) { + LM_ERR("bad parameter\n"); + return -2; + } + + if (fixup_get_svalue(_msg, (gparam_p)_s, &string)) + { + LM_ERR("cannot print the format for string\n"); + return -3; + } + + assumed_type = (ip_type_error == _type)? + ip_parser_execute(string.s, string.len) : _type; + + switch (assumed_type) { + case ip_type_ipv4: + if (!ip4_iptype(string, &res)) { + LM_ERR("bad ip parameter\n"); + return -1; + } + break; + case ip_type_ipv6_reference: + case ip_type_ipv6: + /* consider this reference */ + if (string.s[0] == '[') { + string.s++; + string.len -= 2; + } + if (!ip6_iptype(string, &res)) { + LM_ERR("bad ip parameter\n"); + return -1; + } + break; + default: + return -1; + } + + val.rs.s = res; + val.rs.len = strlen(res); + val.flags = PV_VAL_STR; + dst = (pv_spec_t *)_dst; + dst->setf(_msg, &dst->pvp, (int)EQ_T, &val); + return 1; } -/*! \brief Return true if both IP's (string or pv) are equal. This function also allows comparing an IPv6 with an IPv6 reference. */ +/*! \brief Return true if both IP's (string or pv) are equal. + * This function also allows comparing an IPv6 with an IPv6 reference. */ static int w_compare_ips(struct sip_msg* _msg, char* _s1, char* _s2) { str string1, string2; @@ -766,14 +782,16 @@ static int w_compare_ips(struct sip_msg* _msg, char* _s1, char* _s2) break; } - if (_compare_ips(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type)) + if (_compare_ips(string1.s, string1.len, ip1_type, string2.s, string2.len, + ip2_type)) return 1; else return -1; } -/*! \brief Return true if both pure IP's (string or pv) are equal. IPv6 references not allowed. */ +/*! \brief Return true if both pure IP's (string or pv) are equal. + * IPv6 references not allowed. */ static int w_compare_pure_ips(struct sip_msg* _msg, char* _s1, char* _s2) { str string1, string2; @@ -817,14 +835,17 @@ static int w_compare_pure_ips(struct sip_msg* _msg, char* _s1, char* _s2) break; } - if (_compare_ips(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type)) + if (_compare_ips(string1.s, string1.len, ip1_type, string2.s, string2.len, + ip2_type)) return 1; else return -1; } -/*! \brief Return true if the first IP (string or pv) is within the subnet defined by the second commma-separated IP list in CIDR notation. IPv6 references not allowed. */ +/*! \brief Return true if the first IP (string or pv) is within the subnet + * defined by the second commma-separated IP list in CIDR notation. + * IPv6 references not allowed. */ static int w_ip_is_in_subnet(struct sip_msg* _msg, char* _s1, char* _s2) { struct in6_addr ip_addr6; @@ -889,7 +910,8 @@ static int w_ip_is_in_subnet(struct sip_msg* _msg, char* _s1, char* _s2) } -/*! \brief Return true if the given argument (string or pv) is a valid RFC 1918 IPv4 (private address). */ +/*! \brief Return true if the given argument (string or pv) is a valid + * RFC 1918 IPv4 (private address). */ static int w_is_ip_rfc1918(struct sip_msg* _msg, char* _s) { str string; @@ -1121,4 +1143,3 @@ static int w_naptr_query(struct sip_msg* msg, char* str1, char* str2) return naptr_update_pv(&naptrname, &name); } - diff --git a/src/modules/ipops/ipops_pv.c b/src/modules/ipops/ipops_pv.c index a619a67f99e..5f77aa969ce 100644 --- a/src/modules/ipops/ipops_pv.c +++ b/src/modules/ipops/ipops_pv.c @@ -1,6 +1,4 @@ /** - * $Id$ - * * Copyright (C) 2013 Daniel-Constantin Mierla (asipto.com) * * This file is part of Kamailio, a free SIP server. @@ -204,7 +202,7 @@ int pv_parse_dns_name(pv_spec_t *sp, str *in) switch(pvs.len) { - case 4: + case 4: if(strncmp(pvs.s, "addr", 4)==0) dpv->type = 0; else if(strncmp(pvs.s, "type", 4)==0) @@ -215,7 +213,7 @@ int pv_parse_dns_name(pv_spec_t *sp, str *in) dpv->type = 3; else goto error; break; - case 5: + case 5: if(strncmp(pvs.s, "count", 5)==0) dpv->type = 4; else goto error; @@ -537,7 +535,7 @@ int pv_parse_hn_name(pv_spec_p sp, str *in) switch(in->len) { - case 1: + case 1: if(strncmp(in->s, "n", 1)==0) sp->pvp.pvn.u.isname.name.n = 0; else if(strncmp(in->s, "f", 1)==0) diff --git a/src/modules/ipops/ipops_pv.h b/src/modules/ipops/ipops_pv.h index fb3a9dad4c1..597c3652a93 100644 --- a/src/modules/ipops/ipops_pv.h +++ b/src/modules/ipops/ipops_pv.h @@ -1,6 +1,4 @@ /** - * $Id$ - * * Copyright (C) 2013 Daniel-Constantin Mierla (asipto.com) * * This file is part of Kamailio, a free SIP server. diff --git a/src/modules/ipops/rfc1918_parser.h b/src/modules/ipops/rfc1918_parser.h index e9ef634b84d..e21f61313ae 100644 --- a/src/modules/ipops/rfc1918_parser.h +++ b/src/modules/ipops/rfc1918_parser.h @@ -1,7 +1,7 @@ /* * Warning: This file is auto generated from a ragel syntax (rfc1918_parser.rl), * do not change it! - */ + */ #ifndef rfc1918_parser_h #define rfc1918_parser_h