~ubuntu-branches/debian/sid/kamailio/sid

« back to all changes in this revision

Viewing changes to modules/nathelper/nathelper.c

  • Committer: Package Import Robot
  • Author(s): Victor Seva
  • Date: 2014-01-06 11:47:13 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140106114713-t8xidp4arzrnyeya
Tags: 4.1.1-1
* New upstream release
* debian/patches:
  - add upstream fixes
* Added tls outbound websocket autheph dnssec modules
  - openssl exception added to their license
* removing sparc and ia64 from supported archs
  for mono module (Closes: #728915)

Show diffs side-by-side

added added

removed removed

Lines of Context:
288
288
static int fix_nated_contact_f(struct sip_msg *, char *, char *);
289
289
static int add_contact_alias_0_f(struct sip_msg *, char *, char *);
290
290
static int add_contact_alias_3_f(struct sip_msg *, char *, char *, char *);
 
291
static int set_contact_alias_f(struct sip_msg* msg, char* str1, char* str2);
291
292
static int handle_ruri_alias_f(struct sip_msg *, char *, char *);
292
293
static int pv_get_rr_count_f(struct sip_msg *, pv_param_t *, pv_value_t *);
293
294
static int pv_get_rr_top_count_f(struct sip_msg *, pv_param_t *, pv_value_t *);
339
340
static char *force_socket_str = 0;
340
341
static pid_t mypid;
341
342
static int sipping_flag = -1;
 
343
static int natping_disable_flag = -1;
342
344
static int natping_processes = 1;
343
345
 
344
346
static str nortpproxy_str = str_init("a=nortpproxy:yes");
348
350
static int_str rcv_avp_name;
349
351
 
350
352
static char *natping_socket = 0;
 
353
static int udpping_from_path = 0;
351
354
static int raw_sock = -1;
352
355
static unsigned int raw_ip = 0;
353
356
static unsigned short raw_port = 0;
367
370
                REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
368
371
        {"add_contact_alias",  (cmd_function)add_contact_alias_3_f,  3,
369
372
                fixup_add_contact_alias, 0,
 
373
                REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
 
374
        {"set_contact_alias",  (cmd_function)set_contact_alias_f,  0,
 
375
                0, 0,
370
376
                REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
371
377
        {"handle_ruri_alias",  (cmd_function)handle_ruri_alias_f,    0,
372
378
                0, 0,
412
418
        {"sipping_from",          STR_PARAM, &sipping_from.s        },
413
419
        {"sipping_method",        STR_PARAM, &sipping_method.s      },
414
420
        {"sipping_bflag",         INT_PARAM, &sipping_flag          },
 
421
        {"natping_disable_bflag", INT_PARAM, &natping_disable_flag  },
415
422
        {"natping_processes",     INT_PARAM, &natping_processes     },
416
423
        {"natping_socket",        STR_PARAM, &natping_socket        },
417
424
        {"keepalive_timeout",     INT_PARAM, &nh_keepalive_timeout  },
 
425
        {"udpping_from_path",     INT_PARAM, &udpping_from_path     },
418
426
 
419
427
        {0, 0, 0}
420
428
};
627
635
        }
628
636
 
629
637
        /* create raw socket? */
630
 
        if (natping_socket && natping_socket[0]) {
631
 
                if (get_natping_socket( natping_socket, &raw_ip, &raw_port)!=0)
 
638
        if ((natping_socket && natping_socket[0]) ||  udpping_from_path) {
 
639
                if ((!udpping_from_path) && get_natping_socket( natping_socket, &raw_ip, &raw_port)!=0)
632
640
                        return -1;
633
641
                if (init_raw_socket() < 0)
634
642
                        return -1;
670
678
                        return -1;
671
679
                }
672
680
                if (natping_processes<0) {
673
 
                        LM_ERR("bad config - natping_processes must be > 0\n");
 
681
                        LM_ERR("bad config - natping_processes must be >= 0\n");
674
682
                        return -1;
675
683
                }
676
684
 
677
685
                sipping_flag = (sipping_flag==-1)?0:(1<<sipping_flag);
 
686
                natping_disable_flag = (natping_disable_flag==-1)?0:(1<<natping_disable_flag);
678
687
 
679
688
                /* set reply function if SIP natping is enabled */
680
689
                if (sipping_flag) {
843
852
        return 1;
844
853
}
845
854
 
 
855
/*
 
856
 * Replaces ip:port pair in the Contact: field with the source address
 
857
 * of the packet.
 
858
 */
 
859
static int
 
860
set_contact_alias_f(struct sip_msg* msg, char* str1, char* str2)
 
861
{
 
862
        char nbuf[MAX_URI_SIZE];
 
863
        str nuri;
 
864
        int br;
 
865
 
 
866
        int offset, len;
 
867
        char *buf;
 
868
        contact_t *c;
 
869
        struct lump *anchor;
 
870
        struct sip_uri uri;
 
871
 
 
872
        nuri.s = nbuf;
 
873
        nuri.len = MAX_URI_SIZE;
 
874
        if (get_contact_uri(msg, &uri, &c) == -1)
 
875
                return -1;
 
876
        if ((c->uri.s < msg->buf) || (c->uri.s > (msg->buf + msg->len))) {
 
877
                LM_ERR("you can't update contact twice, check your config!\n");
 
878
                return -1;
 
879
        }
 
880
 
 
881
        if(uri_add_rcv_alias(msg, &c->uri, &nuri)<0) {
 
882
                LM_DBG("cannot add the alias parameter\n");
 
883
                return -1;
 
884
        }
 
885
 
 
886
        br = 1;
 
887
        if(c->uri.s[-1]=='<')
 
888
                br = 0;
 
889
 
 
890
 
 
891
        len = nuri.len + 2*br;
 
892
        buf = pkg_malloc(len + 1);
 
893
        if (buf == NULL) {
 
894
                LM_ERR("out of pkg memory\n");
 
895
                return -1;
 
896
        }
 
897
        if(br==1) {
 
898
                buf[0] = '<';
 
899
                strncpy(buf+1, nuri.s, nuri.len);
 
900
                buf[len-1] = '>';
 
901
        } else {
 
902
                strncpy(buf, nuri.s, nuri.len);
 
903
        }
 
904
        buf[len] = '\0';
 
905
 
 
906
        offset = c->uri.s - msg->buf;
 
907
        anchor = del_lump(msg, offset, c->uri.len, HDR_CONTACT_T);
 
908
        if (anchor == 0)
 
909
                return -1;
 
910
 
 
911
        if (insert_new_lump_after(anchor, buf, len, HDR_CONTACT_T) == 0) {
 
912
                pkg_free(buf);
 
913
                return -1;
 
914
        }
 
915
        c->uri.s = buf;
 
916
        c->uri.len = len;
 
917
 
 
918
        return 1;
 
919
}
 
920
 
846
921
 
847
922
#define SALIAS        ";alias="
848
923
#define SALIAS_LEN (sizeof(SALIAS) - 1)
879
954
    }
880
955
 
881
956
    /* Compare source ip and port against contact uri */
882
 
    if (((ip = str2ip(&(uri.host))) == NULL)
883
 
#ifdef USE_IPV6
884
 
        && ((ip = str2ip6(&(uri.host))) == NULL)
885
 
#endif
886
 
        ) {
 
957
    if (((ip = str2ip(&(uri.host))) == NULL) &&
 
958
        ((ip = str2ip6(&(uri.host))) == NULL)) {
887
959
        LM_DBG("contact uri host is not an ip address\n");
888
960
    } else {
889
961
        if (ip_addr_cmp(ip, &(msg->rcv.src_ip)) &&
1039
1111
        return -1;
1040
1112
    }
1041
1113
    if ((str2ip(&ip_str) == NULL)
1042
 
#ifdef USE_IPV6
1043
1114
        && (str2ip6(&ip_str) == NULL)
1044
 
#endif
1045
1115
        ) {
1046
1116
        LM_ERR("ip param value %s is not valid IP address\n", ip_str.s);
1047
1117
        return -1;
1918
1988
        return sendto(raw_sock, packet, len, 0, (struct sockaddr *) to, sizeof(struct sockaddr_in));
1919
1989
}
1920
1990
 
 
1991
/**
 
1992
 * quick function to extract ip:port from path
 
1993
 */
 
1994
static char *extract_last_path_ip(str path)
 
1995
{
 
1996
        /* used for raw UDP ping which works only on IPv4 */
 
1997
        static char ip[24];
 
1998
        char *start = NULL, *end = NULL, *p;
 
1999
        int i;
 
2000
        int path_depth = 0;
 
2001
        int max_path_depth;
 
2002
 
 
2003
        max_path_depth = udpping_from_path - 1;
 
2004
 
 
2005
        if (!path.len || !path.s) return NULL;
 
2006
 
 
2007
        p = path.s;
 
2008
        for (i = 0; i < path.len; i++) {
 
2009
                if (!strncmp("<sip:", p, 5) && i < path.len - 4) {
 
2010
                        start = p + 5;
 
2011
 
 
2012
                        end = NULL;
 
2013
                }
 
2014
                if ((*p == ';' || *p == '>') && !end) {
 
2015
                        end = p;
 
2016
                        if (max_path_depth) {
 
2017
                                path_depth++;
 
2018
                                if (path_depth >= max_path_depth) {
 
2019
                                        break;
 
2020
                                }
 
2021
                        }
 
2022
                }
 
2023
                p++;
 
2024
        }
 
2025
        if (start && end) {
 
2026
                int len = end - start;
 
2027
                if (len > sizeof(ip) -1) {
 
2028
                        return NULL;
 
2029
                }
 
2030
                memcpy(ip, start, len);
 
2031
                ip[len] = '\0';
 
2032
                return (char *) ip;
 
2033
        } else {
 
2034
                return NULL;
 
2035
        }
 
2036
}
 
2037
 
1921
2038
 
1922
2039
static void
1923
2040
nh_timer(unsigned int ticks, void *timer_idx)
1936
2053
        unsigned int flags;
1937
2054
        char proto;
1938
2055
        struct dest_info dst;
 
2056
        char *path_ip_str = NULL;
 
2057
        unsigned int path_ip = 0;
 
2058
        unsigned short path_port = 0;
1939
2059
 
1940
2060
        if((*natping_state) == 0)
1941
2061
                goto done;
1996
2116
                memcpy( &aorhash, cp, sizeof(aorhash));
1997
2117
                cp = (char*)cp + sizeof(aorhash);
1998
2118
 
 
2119
                if ((flags & natping_disable_flag)) /* always 0 if natping_disable_flag not set */
 
2120
                        continue;
 
2121
 
1999
2122
                /* determin the destination */
2000
2123
                if ( path.len && (flags&sipping_flag)!=0 ) {
2001
2124
                        /* send to first URI in path */
2008
2131
                                LM_ERR("can't parse contact dst_uri\n");
2009
2132
                                continue;
2010
2133
                        }
 
2134
                } else if (path.len && udpping_from_path) {
 
2135
                        path_ip_str = extract_last_path_ip(path);
 
2136
                        if (path_ip_str == NULL) {
 
2137
                                LM_ERR( "ERROR:nathelper:nh_timer: unable to parse path from location\n");
 
2138
                                continue;
 
2139
                        }
 
2140
                        if (get_natping_socket(path_ip_str, &path_ip, &path_port)) {
 
2141
                                LM_ERR("could not parse path host for udpping_from_path\n");
 
2142
                                continue;
 
2143
                        }
 
2144
                        if (parse_uri(c.s, c.len, &curi) < 0) {
 
2145
                                LM_ERR("can't parse contact uri\n");
 
2146
                                continue;
 
2147
                        }
2011
2148
                } else {
2012
2149
                        /* send to the contact/received */
2013
2150
                        if (parse_uri(c.s, c.len, &curi) < 0) {
2051
2188
                                                 raw_port)<0) {
2052
2189
                                LM_ERR("send_raw failed\n");
2053
2190
                        }
 
2191
                } else if (udpping_from_path) {
 
2192
                        if (send_raw((char*)sbuf, sizeof(sbuf), &dst.to, path_ip, 
 
2193
                                                 path_port)<0) {
 
2194
                                LM_ERR("send_raw from path failed\n");
 
2195
                        }
2054
2196
                } else {
2055
2197
                        if (udp_send(&dst, (char *)sbuf, sizeof(sbuf))<0 ) {
2056
2198
                                LM_ERR("udp_send failed\n");