| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#include "rtereg.h" |
| 43 |
|
| 44 |
|
| 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 |
| 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 |
|
| 59 |
struct rtereg_rule * r = &rtereg_conf.rules[i]; |
| 60 |
int err = 0; |
| 61 |
struct fd_list * c; |
| 62 |
|
| 63 |
TRACE_DEBUG(ANNOYING, "Attempt pattern matching of '%.*s' with rule '%s'", (int)len, value, r->pattern); |
| 64 |
|
| 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 |
| 74 |
{ |
| 75 |
|
| 76 |
err = regexec(&r->preg, value, 0, NULL, 0); |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
if (err == REG_NOMATCH) |
| 81 |
continue; |
| 82 |
|
| 83 |
if (err != 0) { |
| 84 |
char * errstr; |
| 85 |
size_t bl; |
| 86 |
|
| 87 |
|
| 88 |
TRACE_DEBUG(INFO, "Error while executing the regular expression '%s':", r->pattern); |
| 89 |
|
| 90 |
|
| 91 |
bl = regerror(err, &r->preg, NULL, 0); |
| 92 |
|
| 93 |
|
| 94 |
CHECK_MALLOC( errstr = malloc(bl) ); |
| 95 |
|
| 96 |
|
| 97 |
regerror(err, &r->preg, errstr, bl); |
| 98 |
TRACE_DEBUG(INFO, "\t%s", errstr); |
| 99 |
|
| 100 |
|
| 101 |
free(errstr); |
| 102 |
|
| 103 |
return (err == REG_ESPACE) ? ENOMEM : EINVAL; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
TRACE_DEBUG(FULL, "[rt_ereg] Match: '%s' to value '%.*s' => '%s' += %d", |
| 108 |
r->pattern, |
| 109 |
(int)len, |
| 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 |
|
| 128 |
static int rtereg_out(void * cbdata, struct msg ** pmsg, struct fd_list * candidates) |
| 129 |
{ |
| 130 |
struct msg * msg = *pmsg; |
| 131 |
struct avp * avp = NULL; |
| 132 |
|
| 133 |
TRACE_ENTRY("%p %p %p", cbdata, msg, candidates); |
| 134 |
|
| 135 |
CHECK_PARAMS(msg && candidates); |
| 136 |
|
| 137 |
|
| 138 |
if (FD_IS_LIST_EMPTY(candidates)) { |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
CHECK_FCT( fd_msg_search_avp ( msg, rtereg_conf.avp, &avp ) ); |
| 144 |
if (avp != NULL) { |
| 145 |
struct avp_hdr * ahdr = NULL; |
| 146 |
CHECK_FCT( fd_msg_avp_hdr ( avp, &ahdr ) ); |
| 147 |
if (ahdr->avp_value != NULL) { |
| 148 |
#ifndef HAVE_REG_STARTEND |
| 149 |
int ret; |
| 150 |
|
| 151 |
|
| 152 |
CHECK_POSIX( pthread_mutex_lock(&mtx) ); |
| 153 |
|
| 154 |
|
| 155 |
if (ahdr->avp_value->os.len >= bufsz) { |
| 156 |
CHECK_MALLOC_DO( buf = realloc(buf, ahdr->avp_value->os.len + 1), |
| 157 |
{ pthread_mutex_unlock(&mtx); return ENOMEM; } ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
memcpy(buf, ahdr->avp_value->os.data, ahdr->avp_value->os.len); |
| 162 |
buf[ahdr->avp_value->os.len] = '\0'; |
| 163 |
|
| 164 |
|
| 165 |
ret = proceed(buf, ahdr->avp_value->os.len, candidates); |
| 166 |
|
| 167 |
CHECK_POSIX(pthread_mutex_unlock(&mtx)); |
| 168 |
|
| 169 |
CHECK_FCT(ret); |
| 170 |
#else |
| 171 |
CHECK_FCT( proceed((char *) ahdr->avp_value->os.data, ahdr->avp_value->os.len, candidates) ); |
| 172 |
#endif |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
static struct fd_rt_out_hdl * rtereg_hdl = NULL; |
| 181 |
|
| 182 |
|
| 183 |
static int rtereg_entry(char * conffile) |
| 184 |
{ |
| 185 |
TRACE_ENTRY("%p", conffile); |
| 186 |
|
| 187 |
|
| 188 |
memset(&rtereg_conf, 0, sizeof(rtereg_conf)); |
| 189 |
|
| 190 |
|
| 191 |
CHECK_FCT( rtereg_conf_handle(conffile) ); |
| 192 |
|
| 193 |
|
| 194 |
CHECK_FCT( fd_rt_out_register( rtereg_out, NULL, 1, &rtereg_hdl ) ); |
| 195 |
|
| 196 |
|
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
void fd_ext_fini(void) |
| 202 |
{ |
| 203 |
int i; |
| 204 |
TRACE_ENTRY(); |
| 205 |
|
| 206 |
|
| 207 |
CHECK_FCT_DO( fd_rt_out_unregister ( rtereg_hdl, NULL ), ); |
| 208 |
|
| 209 |
|
| 210 |
if (rtereg_conf.rules) |
| 211 |
for (i = 0; i < rtereg_conf.rules_nb; i++) { |
| 212 |
free(rtereg_conf.rules[i].pattern); |
| 213 |
free(rtereg_conf.rules[i].server); |
| 214 |
regfree(&rtereg_conf.rules[i].preg); |
| 215 |
} |
| 216 |
free(rtereg_conf.rules); |
| 217 |
#ifndef HAVE_REG_STARTEND |
| 218 |
free(buf); |
| 219 |
#endif |
| 220 |
|
| 221 |
|
| 222 |
return ; |
| 223 |
} |
| 224 |
|
| 225 |
EXTENSION_ENTRY("rt_ereg", rtereg_entry); |