Debian Codegraph @

src/modules/lost/naptr.c

raw symbols SCIP
1 /*
2 * lost module naptr functions
3 * thankfully taken over from the enum module
4 *
5 * Copyright (C) 2002-2010 Juha Heinanen
6 *
7 * Copyright (C) 2021 Wolfgang Kampichler
8 * DEC112, FREQUENTIS AG
9 *
10 * This file is part of Kamailio, a free SIP server.
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * Kamailio is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version
18 *
19 * Kamailio is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 *
28 */
29
30 /*!
31 * \file
32 * \brief Kamailio lost :: naptr
33 * \ingroup lost
34 * Module: \ref lost
35 */
36
37 #include "../../core/resolve.h"
38 #include "../../core/strutils.h"
39 #include "../../core/qvalue.h"
40
41 /* Checks if NAPTR record has flag u and its services field
42 * LoST:https or LOST:http
43 * LIS:HELD
44 */
45 static inline int service_match(struct naptr_rdata *naptr, str *service)
46 {
47 if(service->len == 0) {
48 return 0;
49 }
50
51 /* LoST:https or LOST:http or LIS:HELD */
52 if(naptr->flags_len == 1) {
53 return (((naptr->flags[0] == 'u') || (naptr->flags[0] == 'U'))
54 && (naptr->services_len == service->len)
55 && (strncasecmp(naptr->services, service->s, service->len)
56 == 0));
57 }
58
59 /* LIS:HELD domain */
60 if(naptr->flags_len == 0) {
61 return ((naptr->services_len == service->len)
62 && (strncasecmp(naptr->services, service->s, service->len)
63 == 0));
64 }
65
66 /* no matching service found */
67 return 0;
68 }
69
70 /* Parse NAPTR regexp field of the form !pattern!replacement! and return its
71 * components in pattern and replacement parameters. Regexp field starts at
72 * address first and is len characters long.
73 */
74 static inline int parse_naptr_regexp(
75 char *first, int len, str *pattern, str *replacement)
76 {
77 char *second, *third;
78
79 if(len > 0) {
80 if(*first == '!') {
81 second = (char *)memchr((void *)(first + 1), '!', len - 1);
82 if(second) {
83 len = len - (second - first + 1);
84 if(len > 0) {
85 third = memchr(second + 1, '!', len);
86 if(third) {
87 pattern->len = second - first - 1;
88 pattern->s = first + 1;
89 replacement->len = third - second - 1;
90 replacement->s = second + 1;
91 return 1;
92 } else {
93 LM_ERR("Third ! missing from regexp\n");
94 return -1;
95 }
96 } else {
97 LM_ERR("Third ! missing from regexp\n");
98 return -2;
99 }
100 } else {
101 LM_ERR("Second ! missing from regexp\n");
102 return -3;
103 }
104 } else {
105 LM_ERR("First ! missing from regexp\n");
106 return -4;
107 }
108 } else {
109 LM_ERR("Regexp missing\n");
110 return -5;
111 }
112 }
113
114 /*
115 * Tests if one result record is "greater" that the other. Non-NAPTR records
116 * greater that NAPTR record. An invalid NAPTR record is greater than a
117 * valid one. Valid NAPTR records are compared based on their
118 * (order,preference).
119 */
120 static inline int naptr_greater(struct rdata *a, struct rdata *b)
121 {
122 struct naptr_rdata *na, *nb;
123
124 if(a->type != T_NAPTR)
125 return 1;
126 if(b->type != T_NAPTR)
127 return 0;
128
129 na = (struct naptr_rdata *)a->rdata;
130 if(na == 0)
131 return 1;
132
133 nb = (struct naptr_rdata *)b->rdata;
134 if(nb == 0)
135 return 0;
136
137 return (((na->order) << 16) + na->pref) > (((nb->order) << 16) + nb->pref);
138 }
139
140 /*
141 * Bubble sorts result record list according to naptr (order,preference).
142 */
143 static inline void naptr_sort(struct rdata **head)
144 {
145 struct rdata *p, *q, *r, *s, *temp, *start;
146
147 /* r precedes p and s points to the node up to which comparisons
148 are to be made */
149
150 s = NULL;
151 start = *head;
152 while(s != start->next) {
153 r = p = start;
154 q = p->next;
155 while(p != s) {
156 if(naptr_greater(p, q)) {
157 if(p == start) {
158 temp = q->next;
159 q->next = p;
160 p->next = temp;
161 start = q;
162 r = q;
163 } else {
164 temp = q->next;
165 q->next = p;
166 p->next = temp;
167 r->next = q;
168 r = q;
169 }
170 } else {
171 r = p;
172 p = p->next;
173 }
174 q = p->next;
175 if(q == s)
176 s = p;
177 }
178 }
179 *head = start;
180 }
181
182 /*
183 * NAPTR lookup on hostname & service, returns result as string
184 */
185 int lost_naptr_lookup(str hostname, str *service, str *result)
186 {
187 struct rdata *head;
188 struct rdata *l;
189 struct naptr_rdata *naptr;
190
191 str pattern, replacement;
192
193 head = get_record(hostname.s, T_NAPTR, RES_ONLY_TYPE);
194
195 if(head == 0) {
196 LM_DBG("no NAPTR record found for [%.*s]\n", hostname.len, hostname.s);
197 return 0;
198 }
199
200 naptr_sort(&head);
201
202 /* we have the naptr records, loop and find an srv record with */
203 /* same ip address as source ip address, if we do then true is returned */
204
205 for(l = head; l; l = l->next) {
206
207 if(l->type != T_NAPTR)
208 continue; /*should never happen*/
209 naptr = (struct naptr_rdata *)l->rdata;
210
211 if(naptr == 0) {
212 LM_ERR("no rdata in DNS response\n");
213 free_rdata_list(head);
214 return 0;
215 }
216
217 LM_DBG("NAPTR query on %.*s: order %u, pref %u, flen %u, flags "
218 "'%.*s', slen %u, services '%.*s', rlen %u, "
219 "regexp '%.*s'\n",
220 hostname.len, hostname.s, naptr->order, naptr->pref,
221 naptr->flags_len, (int)(naptr->flags_len), ZSW(naptr->flags),
222 naptr->services_len, (int)(naptr->services_len),
223 ZSW(naptr->services), naptr->regexp_len,
224 (int)(naptr->regexp_len), ZSW(naptr->regexp));
225
226 if(service_match(naptr, service) == 0) {
227 continue;
228 }
229
230 if(parse_naptr_regexp(&(naptr->regexp[0]), naptr->regexp_len, &pattern,
231 &replacement)
232 < 0) {
233 free_rdata_list(head); /*clean up*/
234 LM_ERR("parsing of NAPTR regexp failed\n");
235 return 0;
236 }
237 /* Avoid making copies of pattern and replacement */
238 pattern.s[pattern.len] = (char)0;
239 replacement.s[replacement.len] = (char)0;
240 /* replace hostname */
241 if(reg_replace(pattern.s, replacement.s, &(hostname.s[0]), result)
242 < 0) {
243 pattern.s[pattern.len] = '!';
244 replacement.s[replacement.len] = '!';
245 LM_ERR("regexp replace failed\n");
246 free_rdata_list(head); /*clean up*/
247 return 0;
248 } else {
249
250 LM_DBG("resulted in replacement: '%.*s'\n", result->len,
251 ZSW(result->s));
252
253 free_rdata_list(head); /*clean up*/
254 return 1;
255 }
256 }
257
258 /* must not have found the record */
259 return 0;
260 }