ipops: added dsn_int_match_ip(hostname, ipaddr)
[sip-router] / modules / ipops / ipops_mod.c
index d21546c..dc58fc8 100644 (file)
@@ -87,7 +87,8 @@ static int w_compare_ips(struct sip_msg*, char*, char*);
 static int w_compare_pure_ips(struct sip_msg*, char*, char*);
 static int w_is_ip_rfc1918(struct sip_msg*, char*);
 static int w_ip_is_in_subnet(struct sip_msg*, char*, char*);
-static int w_dns_nc_match_ip(sip_msg_t*, char*, char*);
+static int w_dns_sys_match_ip(sip_msg_t*, char*, char*);
+static int w_dns_int_match_ip(sip_msg_t*, char*, char*);
 
 
 /*
@@ -115,7 +116,9 @@ static cmd_export_t cmds[] =
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
   { "is_in_subnet", (cmd_function)w_ip_is_in_subnet, 2, fixup_spve_spve, 0,
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
-  { "dns_nc_match_ip", (cmd_function)w_dns_nc_match_ip, 2, fixup_spve_spve, 0,
+  { "dns_sys_match_ip", (cmd_function)w_dns_sys_match_ip, 2, fixup_spve_spve, 0,
+  ANY_ROUTE },
+  { "dns_int_match_ip", (cmd_function)w_dns_int_match_ip, 2, fixup_spve_spve, 0,
   ANY_ROUTE },
   { "bind_ipops", (cmd_function)bind_ipops, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0 }
@@ -617,7 +620,7 @@ static inline ip_addr_t *strtoipX(str *ips)
        }
 }
 
-static int w_dns_nc_match_ip(sip_msg_t *msg, char *hnp, char *ipp)
+static int w_dns_sys_match_ip(sip_msg_t *msg, char *hnp, char *ipp)
 {
        struct addrinfo hints, *res, *p;
        int status;
@@ -653,7 +656,7 @@ static int w_dns_nc_match_ip(sip_msg_t *msg, char *hnp, char *ipp)
        if ((status = getaddrinfo(hns.s, NULL, &hints, &res)) != 0)
        {
         LM_ERR("getaddrinfo: %s\n", gai_strerror(status));
-        return -2;
+        return -4;
     }
 
        for(p = res;p != NULL; p = p->ai_next)
@@ -680,3 +683,52 @@ static int w_dns_nc_match_ip(sip_msg_t *msg, char *hnp, char *ipp)
 
        return -1;
 }
+
+static int w_dns_int_match_ip(sip_msg_t *msg, char *hnp, char *ipp)
+{
+       ip_addr_t *ipa;
+       str hns;
+       str ips;
+       struct hostent* he;
+       char ** h;
+       int ret;
+
+       if (fixup_get_svalue(msg, (gparam_p)hnp, &hns))
+       {
+               LM_ERR("cannot evaluate hostname parameter\n");
+               return -2;
+       }
+
+       if (fixup_get_svalue(msg, (gparam_p)ipp, &ips))
+       {
+               LM_ERR("cannot evaluate ip address parameter\n");
+               return -2;
+       }
+
+       ipa = strtoipX(&ips);
+       if(ipa==NULL)
+       {
+               LM_ERR("invalid ip address: %.*s\n", ips.len, ips.s);
+               return -3;
+       }
+
+       he=resolvehost(hns.s);
+       if (he==0) {
+               DBG("could not resolve %s\n", hns.s);
+               return -4;
+       }
+       ret = 0;
+       if (he->h_addrtype==ipa->af)
+       {
+               for(h=he->h_addr_list; (*h); h++)
+               {
+                       if(memcmp(ipa->u.addr, *h, ipa->len)==0)
+                       {
+                               /* match */
+                               return 1;
+                       }
+               }
+       }
+       /* no match */
+       return -1;
+}