/src/kamailio/src/core/name_alias.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * Copyright (C) 2001-2003 FhG Fokus |
4 | | * |
5 | | * This file is part of Kamailio, a free SIP server. |
6 | | * |
7 | | * Kamailio is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version |
11 | | * |
12 | | * Kamailio is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | /*! |
22 | | * \file |
23 | | * \brief Kamailio core :: Name/alias handling |
24 | | * \author andrei |
25 | | * \ingroup core |
26 | | * Module: \ref core |
27 | | */ |
28 | | |
29 | | |
30 | | #ifndef name_alias_h |
31 | | #define name_alias_h |
32 | | |
33 | | #include "str.h" |
34 | | #include "dprint.h" |
35 | | #include "mem/mem.h" |
36 | | |
37 | | |
38 | | typedef struct host_alias |
39 | | { |
40 | | str alias; |
41 | | unsigned short port; |
42 | | unsigned short proto; |
43 | | struct host_alias *next; |
44 | | } host_alias_t; |
45 | | |
46 | | |
47 | | extern struct host_alias *aliases; |
48 | | |
49 | | |
50 | | /** returns 1 if name is in the alias list; if port=0, port no is ignored |
51 | | * if proto=0, proto is ignored*/ |
52 | | static inline int grep_aliases( |
53 | | char *name, int len, unsigned short port, unsigned short proto) |
54 | 0 | { |
55 | 0 | struct host_alias *a; |
56 | |
|
57 | 0 | if((len > 2) && ((*name) == '[') && (name[len - 1] == ']')) { |
58 | | /* ipv6 reference, skip [] */ |
59 | 0 | name++; |
60 | 0 | len -= 2; |
61 | 0 | } |
62 | 0 | for(a = aliases; a; a = a->next) { |
63 | 0 | LM_DBG("matching (%d:%.*s:%d) vs. (%d:%.*s:%d)\n", proto, len, name, |
64 | 0 | port, a->proto, a->alias.len, a->alias.s, a->port); |
65 | 0 | if((a->alias.len == len) |
66 | 0 | && ((a->port == 0) || (port == 0) || (a->port == port)) |
67 | 0 | && ((a->proto == 0) || (proto == 0) || (a->proto == proto)) |
68 | 0 | && (strncasecmp(a->alias.s, name, len) == 0)) { |
69 | 0 | return 1; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | return 0; |
73 | 0 | } Unexecuted instantiation: main.c:grep_aliases Unexecuted instantiation: socket_info.c:grep_aliases Unexecuted instantiation: cfg.tab.c:grep_aliases Unexecuted instantiation: core_cmd.c:grep_aliases Unexecuted instantiation: forward.c:grep_aliases |
74 | | |
75 | | |
76 | | /** adds an alias to the list (only if it isn't already there) |
77 | | * if port==0, the alias will match all the ports |
78 | | * if proto==0, the alias will match all the protocols |
79 | | * returns 1 if a new alias was added, 0 if a matching alias was already on |
80 | | * the list and -1 on error */ |
81 | | static inline int add_alias( |
82 | | char *name, int len, unsigned short port, unsigned short proto) |
83 | 0 | { |
84 | 0 | struct host_alias *a; |
85 | |
|
86 | 0 | if((port) && (proto)) { |
87 | | /* don't add if there is already an alias matching it */ |
88 | 0 | if(grep_aliases(name, len, port, proto)) { |
89 | 0 | return 0; |
90 | 0 | } |
91 | 0 | } else { |
92 | | /* don't add if already in the list with port or proto ==0*/ |
93 | 0 | for(a = aliases; a; a = a->next) { |
94 | 0 | if((a->alias.len == len) && (a->port == port) && (a->proto == proto) |
95 | 0 | && (strncasecmp(a->alias.s, name, len) == 0)) { |
96 | 0 | return 0; |
97 | 0 | } |
98 | 0 | } |
99 | 0 | } |
100 | 0 | a = (struct host_alias *)pkg_malloc(sizeof(struct host_alias)); |
101 | 0 | if(a == 0) { |
102 | 0 | goto error; |
103 | 0 | } |
104 | 0 | a->alias.s = (char *)pkg_malloc(len + 1); |
105 | 0 | if(a->alias.s == 0) { |
106 | 0 | goto error; |
107 | 0 | } |
108 | 0 | a->alias.len = len; |
109 | 0 | memcpy(a->alias.s, name, len); |
110 | 0 | a->alias.s[len] = 0; /* null terminate for easier printing*/ |
111 | 0 | a->port = port; |
112 | 0 | a->proto = proto; |
113 | 0 | a->next = aliases; |
114 | 0 | aliases = a; |
115 | 0 | return 1; |
116 | 0 | error: |
117 | 0 | PKG_MEM_ERROR; |
118 | 0 | if(a) { |
119 | 0 | pkg_free(a); |
120 | 0 | } |
121 | 0 | return -1; |
122 | 0 | } Unexecuted instantiation: main.c:add_alias Unexecuted instantiation: socket_info.c:add_alias Unexecuted instantiation: cfg.tab.c:add_alias Unexecuted instantiation: core_cmd.c:add_alias Unexecuted instantiation: forward.c:add_alias |
123 | | |
124 | | #endif |