Mercurial > hg > freeDiameter
annotate extensions/rt_ereg/rtereg.c @ 1027:0117a7746b21
Fix a number of errors and warnings introduced/highlighted by recent commits
author | Sebastien Decugis <sdecugis@freediameter.net> |
---|---|
date | Mon, 15 Apr 2013 15:17:07 +0800 |
parents | d59decafe9c0 |
children | 1af09cc156d6 |
rev | line source |
---|---|
525 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
740
4a9f08d6b6ba
Updated my mail address
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
584
diff
changeset
|
3 * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
525 | 4 * * |
741
d59decafe9c0
Updated copyright information
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
5 * Copyright (c) 2011, WIDE Project and NICT * |
525 | 6 * All rights reserved. * |
7 * * | |
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * | |
9 * permitted provided that the following conditions are met: * | |
10 * * | |
11 * * Redistributions of source code must retain the above * | |
12 * copyright notice, this list of conditions and the * | |
13 * following disclaimer. * | |
14 * * | |
15 * * Redistributions in binary form must reproduce the above * | |
16 * copyright notice, this list of conditions and the * | |
17 * following disclaimer in the documentation and/or other * | |
18 * materials provided with the distribution. * | |
19 * * | |
20 * * Neither the name of the WIDE Project or NICT nor the * | |
21 * names of its contributors may be used to endorse or * | |
22 * promote products derived from this software without * | |
23 * specific prior written permission of WIDE Project and * | |
24 * NICT. * | |
25 * * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * | |
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * | |
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * | |
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * | |
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * | |
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * | |
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * | |
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | |
34 *********************************************************************************************************/ | |
35 | |
36 /* | |
37 * This extension allows to perform some pattern-matching on an AVP | |
38 * and send the message to a server accordingly. | |
39 * See rt_ereg.conf.sample file for the format of the configuration file. | |
40 */ | |
41 | |
42 #include "rtereg.h" | |
43 | |
44 /* The configuration structure */ | |
45 struct rtereg_conf rtereg_conf; | |
46 | |
47 #ifndef HAVE_REG_STARTEND | |
48 static char * buf = NULL; | |
49 static size_t bufsz; | |
50 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; | |
51 #endif /* HAVE_REG_STARTEND */ | |
52 | |
53 static int proceed(char * value, size_t len, struct fd_list * candidates) | |
54 { | |
55 int i; | |
56 | |
57 for (i = 0; i < rtereg_conf.rules_nb; i++) { | |
58 /* Does this pattern match the value? */ | |
59 struct rtereg_rule * r = &rtereg_conf.rules[i]; | |
60 int err = 0; | |
61 struct fd_list * c; | |
62 | |
1027
0117a7746b21
Fix a number of errors and warnings introduced/highlighted by recent commits
Sebastien Decugis <sdecugis@freediameter.net>
parents:
741
diff
changeset
|
63 TRACE_DEBUG(ANNOYING, "Attempt pattern matching of '%.*s' with rule '%s'", (int)len, value, r->pattern); |
526
6fe3e5cf9fb2
Added a flag to disable NAI routing in RADIUS/Diameter gw
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
525
diff
changeset
|
64 |
525 | 65 #ifdef HAVE_REG_STARTEND |
66 { | |
67 regmatch_t pmatch[1]; | |
68 memset(pmatch, 0, sizeof(pmatch)); | |
69 pmatch[0].rm_so = 0; | |
70 pmatch[0].rm_eo = len; | |
71 err = regexec(&r->preg, value, 0, pmatch, REG_STARTEND); | |
72 } | |
73 #else /* HAVE_REG_STARTEND */ | |
74 { | |
75 /* We have a 0-terminated string */ | |
76 err = regexec(&r->preg, value, 0, NULL, 0); | |
77 } | |
78 #endif /* HAVE_REG_STARTEND */ | |
79 | |
80 if (err == REG_NOMATCH) | |
81 continue; | |
82 | |
83 if (err != 0) { | |
84 char * errstr; | |
85 size_t bl; | |
86 | |
87 /* Error while compiling the regex */ | |
88 TRACE_DEBUG(INFO, "Error while executing the regular expression '%s':", r->pattern); | |
89 | |
90 /* Get the error message size */ | |
91 bl = regerror(err, &r->preg, NULL, 0); | |
92 | |
93 /* Alloc the buffer for error message */ | |
94 CHECK_MALLOC( errstr = malloc(bl) ); | |
95 | |
96 /* Get the error message content */ | |
97 regerror(err, &r->preg, errstr, bl); | |
98 TRACE_DEBUG(INFO, "\t%s", errstr); | |
99 | |
100 /* Free the buffer, return the error */ | |
101 free(errstr); | |
102 | |
103 return (err == REG_ESPACE) ? ENOMEM : EINVAL; | |
104 } | |
105 | |
106 /* From this point, the expression matched the AVP value */ | |
107 TRACE_DEBUG(FULL, "[rt_ereg] Match: '%s' to value '%.*s' => '%s' += %d", | |
108 r->pattern, | |
1027
0117a7746b21
Fix a number of errors and warnings introduced/highlighted by recent commits
Sebastien Decugis <sdecugis@freediameter.net>
parents:
741
diff
changeset
|
109 (int)len, |
525 | 110 value, |
111 r->server, | |
112 r->score); | |
113 | |
114 for (c = candidates->next; c != candidates; c = c->next) { | |
115 struct rtd_candidate * cand = (struct rtd_candidate *)c; | |
116 | |
117 if (strcmp(r->server, cand->diamid) == 0) { | |
118 cand->score += r->score; | |
119 break; | |
120 } | |
121 } | |
122 }; | |
123 | |
124 return 0; | |
125 } | |
126 | |
127 /* The callback called on new messages */ | |
128 static int rtereg_out(void * cbdata, struct msg * msg, struct fd_list * candidates) | |
129 { | |
130 struct avp * avp = NULL; | |
131 | |
132 TRACE_ENTRY("%p %p %p", cbdata, msg, candidates); | |
133 | |
134 CHECK_PARAMS(msg && candidates); | |
135 | |
136 /* Check if it is worth processing the message */ | |
137 if (FD_IS_LIST_EMPTY(candidates)) { | |
138 return 0; | |
139 } | |
140 | |
141 /* Now search the AVP in the message */ | |
142 CHECK_FCT( fd_msg_search_avp ( msg, rtereg_conf.avp, &avp ) ); | |
143 if (avp != NULL) { | |
144 struct avp_hdr * ahdr = NULL; | |
145 CHECK_FCT( fd_msg_avp_hdr ( avp, &ahdr ) ); | |
146 if (ahdr->avp_value != NULL) { | |
584
15b59f928383
Fix a couple of compilation warnings
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
526
diff
changeset
|
147 #ifndef HAVE_REG_STARTEND |
525 | 148 int ret; |
149 | |
150 /* Lock the buffer */ | |
151 CHECK_POSIX( pthread_mutex_lock(&mtx) ); | |
152 | |
153 /* Augment the buffer if needed */ | |
154 if (ahdr->avp_value->os.len >= bufsz) { | |
155 CHECK_MALLOC_DO( buf = realloc(buf, ahdr->avp_value->os.len + 1), | |
156 { pthread_mutex_unlock(&mtx); return ENOMEM; } ); | |
157 } | |
158 | |
159 /* Copy the AVP value */ | |
160 memcpy(buf, ahdr->avp_value->os.data, ahdr->avp_value->os.len); | |
161 buf[ahdr->avp_value->os.len] = '\0'; | |
162 | |
163 /* Now apply the rules */ | |
164 ret = proceed(buf, ahdr->avp_value->os.len, candidates); | |
165 | |
166 CHECK_POSIX(pthread_mutex_unlock(&mtx)); | |
167 | |
168 CHECK_FCT(ret); | |
169 #else /* HAVE_REG_STARTEND */ | |
584
15b59f928383
Fix a couple of compilation warnings
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
526
diff
changeset
|
170 CHECK_FCT( proceed((char *) ahdr->avp_value->os.data, ahdr->avp_value->os.len, candidates) ); |
525 | 171 #endif /* HAVE_REG_STARTEND */ |
172 } | |
173 } | |
174 | |
175 return 0; | |
176 } | |
177 | |
178 /* handler */ | |
179 static struct fd_rt_out_hdl * rtereg_hdl = NULL; | |
180 | |
181 /* entry point */ | |
182 static int rtereg_entry(char * conffile) | |
183 { | |
184 TRACE_ENTRY("%p", conffile); | |
185 | |
186 /* Initialize the configuration */ | |
187 memset(&rtereg_conf, 0, sizeof(rtereg_conf)); | |
188 | |
189 /* Parse the configuration file */ | |
190 CHECK_FCT( rtereg_conf_handle(conffile) ); | |
191 | |
192 /* Register the callback */ | |
193 CHECK_FCT( fd_rt_out_register( rtereg_out, NULL, 1, &rtereg_hdl ) ); | |
194 | |
195 /* We're done */ | |
196 return 0; | |
197 } | |
198 | |
199 /* Unload */ | |
200 void fd_ext_fini(void) | |
201 { | |
202 int i; | |
203 TRACE_ENTRY(); | |
204 | |
205 /* Unregister the cb */ | |
206 CHECK_FCT_DO( fd_rt_out_unregister ( rtereg_hdl, NULL ), /* continue */ ); | |
207 | |
208 /* Destroy the data */ | |
209 if (rtereg_conf.rules) | |
210 for (i = 0; i < rtereg_conf.rules_nb; i++) { | |
211 free(rtereg_conf.rules[i].pattern); | |
212 free(rtereg_conf.rules[i].server); | |
213 regfree(&rtereg_conf.rules[i].preg); | |
214 } | |
215 free(rtereg_conf.rules); | |
216 #ifndef HAVE_REG_STARTEND | |
217 free(buf); | |
218 #endif /* HAVE_REG_STARTEND */ | |
219 | |
220 /* Done */ | |
221 return ; | |
222 } | |
223 | |
224 EXTENSION_ENTRY("rt_ereg", rtereg_entry); |