void wap_map_url(Octstr **osp, Octstr **send_msisdn_query, Octstr **send_msisdn_header, Octstr **send_msisdn_format, int *accept_cookies) { long i; Octstr *newurl, *tmp1, *tmp2; newurl = tmp1 = tmp2 = NULL; *send_msisdn_query = *send_msisdn_header = *send_msisdn_format = NULL; *accept_cookies = -1; debug("wsp",0,"WSP: Mapping url <%s>", octstr_get_cstr(*osp)); for (i = 0; url_map && i < gwlist_len(url_map); i++) { struct url_map_struct *entry; entry = gwlist_get(url_map, i); /* debug("wsp",0,"WSP: matching <%s> with <%s>", octstr_get_cstr(entry->url), octstr_get_cstr(entry->map_url)); */ /* DAVI: I only have '*' terminated entry->url implementation for now */ tmp1 = octstr_duplicate(entry->url); octstr_delete(tmp1, octstr_len(tmp1)-1, 1); /* remove last '*' */ tmp2 = octstr_copy(*osp, 0, octstr_len(tmp1)); debug("wsp",0,"WSP: Matching <%s> with <%s>", octstr_get_cstr(tmp1), octstr_get_cstr(tmp2)); if (octstr_case_compare(tmp2, tmp1) == 0) { /* rewrite url if configured to do so */ if (entry->map_url != NULL) { if (octstr_get_char(entry->map_url, octstr_len(entry->map_url)-1) == '*') { newurl = octstr_duplicate(entry->map_url); octstr_delete(newurl, octstr_len(newurl)-1, 1); octstr_append(newurl, octstr_copy(*osp, octstr_len(entry->url)-1, octstr_len(*osp)-octstr_len(entry->url)+1)); } else { newurl = octstr_duplicate(entry->map_url); } debug("wsp",0,"WSP: URL Rewriten from <%s> to <%s>", octstr_get_cstr(*osp), octstr_get_cstr(newurl)); octstr_destroy(*osp); *osp = newurl; } *accept_cookies = entry->accept_cookies; *send_msisdn_query = octstr_duplicate(entry->send_msisdn_query); *send_msisdn_header = octstr_duplicate(entry->send_msisdn_header); *send_msisdn_format = octstr_duplicate(entry->send_msisdn_format); octstr_destroy(tmp1); octstr_destroy(tmp2); break; } octstr_destroy(tmp1); octstr_destroy(tmp2); } }
static inline void get_octstr_value(Octstr **os, const List *r, const int i) { *os = octstr_duplicate(gwlist_get((List*)r, i)); if (octstr_str_compare(*os, "_NULL_") == 0) { octstr_destroy(*os); *os = NULL; } }
static void main_for_extract(void) { static char *items[] = { "one", "two", "three", }; int num_items = sizeof(items) / sizeof(items[0]); int num_repeats = 3; int i, j; char *p; List *list, *extracted; list = gwlist_create(); for (j = 0; j < num_repeats; ++j) for (i = 0; i < num_items; ++i) gwlist_append(list, items[i]); for (j = 0; j < num_items; ++j) { extracted = gwlist_extract_matching(list, items[j], compare_cstr); if (extracted == NULL) panic(0, "no extracted elements, should have!"); for (i = 0; i < gwlist_len(list); ++i) { p = gwlist_get(list, i); if (strcmp(p, items[j]) == 0) panic(0, "list contains `%s' after " "extracting it!", items[j]); } for (i = 0; i < gwlist_len(extracted); ++i) { p = gwlist_get(extracted, i); if (strcmp(p, items[j]) != 0) panic(0, "extraction returned wrong element!"); } gwlist_destroy(extracted, NULL); } if (gwlist_len(list) != 0) panic(0, "list is not empty after extracting everything"); gwlist_destroy(list, NULL); }
static void mime_entity_dump_real(MIMEEntity *m, unsigned int level) { long i, items; Octstr *prefix, *type, *charset; Octstr *tmp; unsigned int j; gw_assert(m != NULL && m->headers != NULL); prefix = octstr_create(""); for (j = 0; j < level * 2; j++) octstr_append_cstr(prefix, " "); http_header_get_content_type(m->headers, &type, &charset); debug("mime.dump",0,"%sContent-Type `%s'", octstr_get_cstr(prefix), octstr_get_cstr(type)); for(i = 0; i < gwlist_len(m->headers); i++) { tmp = gwlist_get(m->headers,i); debug("mime.dump",0, "Header: %s", octstr_get_cstr(tmp)); } if (m->start != NULL) { Octstr *cid = http_header_value(m->start->headers, octstr_imm("Content-ID")); debug("mime.dump",0,"%sRelated to Content-ID <%s> MIMEEntity at address `%p'", octstr_get_cstr(prefix), octstr_get_cstr(cid), m->start); octstr_destroy(cid); } items = gwlist_len(m->multiparts); debug("mime.dump",0,"%sBody contains %ld MIME entities, size %ld", octstr_get_cstr(prefix), items, (items == 0 && m->body) ? octstr_len(m->body) : -1); octstr_destroy(prefix); octstr_destroy(type); octstr_destroy(charset); for (i = 0; i < items; i++) { MIMEEntity *e = gwlist_get(m->multiparts, i); mime_entity_dump_real(e, level + 1); } }
static List *make_mm_flags(List *oflags, List *flag_cmds) { List *l = oflags ? oflags : gwlist_create(); int i, n; for (i = 0, n = gwlist_len(l); i < n; i++) { /* cleanup list. */ Octstr *x = gwlist_get(l,i); int ch = octstr_get_char(x, 0); if (ch == '+' || ch == '-' || ch == '/') octstr_delete(x,0,1); } for (i = 0, n = (flag_cmds ? gwlist_len(flag_cmds) : 0); i<n; i++) { Octstr *x = gwlist_get(flag_cmds,i); int ch = octstr_get_char(x, 0); char *s = octstr_get_cstr(x); int j, m, cmd; if (ch == '+' || ch == '-' || ch == '/') { s++; cmd = ch; } else cmd = '+'; /* Find it in original. If existent, remove it. */ for (j = 0, m = gwlist_len(l); j < m; j++) if (octstr_str_compare(gwlist_get(l,j),s) == 0) { Octstr *y = gwlist_get(l,j); gwlist_delete(l,j,1); octstr_destroy(y); j--; m--; } if (cmd == '+' || cmd == '/') gwlist_append(l, octstr_create(s)); } return l; }
/* Get part i in list of body parts. Copy is made*/ MIMEEntity *mime_entity_get_part(MIMEEntity *e, int i) { MIMEEntity *m; gw_assert(e != NULL); gw_assert(i >= 0); gw_assert(i < gwlist_len(e->multiparts)); m = gwlist_get(e->multiparts, i); gw_assert(m); return mime_entity_duplicate(m); }
/* Returns (copy of) the 'start' element of a multi-part entity. */ MIMEEntity *mime_multipart_start_elem(MIMEEntity *e) { gw_assert(e != NULL); /* If e->start element is not yet set, set it as follows: * - if content type is not set, then set it to NULL * - if the start element is not set but this is a multipart object, set * it to first multipart element, else set it to null * - if the start element of the content type is set, find a matching object * and set e->start accordingly. * Finally, return a copy of it. */ if (!e->start) { Octstr *ctype = http_header_value(e->headers, octstr_imm("Content-Type")); Octstr *start = get_start_param(ctype); int i; if (!ctype) e->start = NULL; else if (!start) { if (gwlist_len(e->multiparts) > 0) e->start = gwlist_get(e->multiparts, 0); else e->start = NULL; } else for (i = 0; i < gwlist_len(e->multiparts); i++) { MIMEEntity *x = gwlist_get(e->multiparts, i); if (cid_matches(x->headers, start)) { e->start = x; break; } } if (ctype) octstr_destroy(ctype); if (start) octstr_destroy(start); } return (e->start) ? mime_entity_duplicate(e->start) : NULL; }
static int is_single_group(Octstr *query) { long i; int r = 0; for (i = 0; i < gwlist_len(single_hooks); ++i) { r += ((int(*)(Octstr *)) gwlist_get(single_hooks, i))(query); } return (r > 0); }
static void msg_list_destroy(List *l) { long i, len; Msg *item; i = 0; len = gwlist_len(l); while (i < len) { item = gwlist_get(l, i); msg_destroy(item); item = NULL; ++i; } }
URLTranslation *urltrans_find_service(URLTranslationList *trans, Msg *msg) { URLTranslation *t; List *list; list = dict_get(trans->names, msg->sms.service); if (list != NULL) { t = gwlist_get(list, 0); } else { t = NULL; } return t; }
void *wtls_payloadlist_destroy(List* payloadList) { wtls_Payload* currentPayload; int listLen, i; listLen = gwlist_len(payloadList); for( i=0; i<listLen; i++) { currentPayload = (wtls_Payload *)gwlist_get(payloadList, i); wtls_payload_destroy(currentPayload); } /* delete the list itself */ gw_free(payloadList); }
static Octstr *linearise_string_list(List *l, char *sep) { int i, n; Octstr *s = octstr_create(""); for (i = 0, n = gwlist_len(l); i < n; i++) { Octstr *p = gwlist_get(l,i); if (p) octstr_format_append(s, "%s%S", (i == 0) ? "" : sep, p); } return s; }
/* Generate a refusal for all requested capabilities that are not * replied to. */ static void refuse_unreplied_capabilities(List *caps, List *req) { long i, len; Capability *cap; len = gwlist_len(req); for (i = 0; i < len; i++) { cap = gwlist_get(req, i); if (wsp_cap_count(caps, cap->id, cap->name) == 0) { cap = wsp_cap_create(cap->id, cap->name, NULL); gwlist_append(caps, cap); } } }
/* Make a copy of a mime object. recursively. */ MIMEEntity *mime_entity_duplicate(MIMEEntity *e) { MIMEEntity *copy = mime_entity_create(); int i, n; mime_replace_headers(copy, e->headers); copy->body = e->body ? octstr_duplicate(e->body) : NULL; for (i = 0, n = gwlist_len(e->multiparts); i < n; i++) gwlist_append(copy->multiparts, mime_entity_duplicate(gwlist_get(e->multiparts, i))); return copy; }
static int is_allowed_in_group(Octstr *group, Octstr *variable) { long i; int x, r = -1; for (i = 0; i < gwlist_len(allowed_hooks); ++i) { x = ((int(*)(Octstr *, Octstr *)) gwlist_get(allowed_hooks, i))(group, variable); r = (x == -1 ? (r == -1 ? x : r) : (r == -1 ? x : r + x)); } return r; }
static int have_cookie(List *cookies, Cookie *cookie) { Cookie *value = NULL; long pos = 0; if (cookies == NULL || cookie == NULL) { error(0, "have_cookie: Null argument(s) - no Cookie list, Cookie or both"); return 0; } /* Walk through the cookie cache, comparing cookie */ while (pos < gwlist_len(cookies)) { value = gwlist_get(cookies, pos); /* octstr_compare() now only returns 0 on an exact match or if both args are 0 */ debug ("wap.wsp.http", 0, "have_cookie: Comparing name (%s:%s), path (%s:%s), domain (%s:%s)", octstr_get_cstr(cookie->name), octstr_get_cstr(value->name), octstr_get_cstr(cookie->path), octstr_get_cstr(value->path), octstr_get_cstr(cookie->domain), octstr_get_cstr(value->domain)); /* Match on no value or value and value equality for name, path and domain */ if ( (value->name == NULL || ((value->name != NULL && cookie->name != NULL) && octstr_compare(value->name, cookie->name) == 0)) && (value->path == NULL || ((value->path != NULL && cookie->path != NULL) && octstr_compare(value->path, cookie->path) == 0)) && (value->domain == NULL || ((value->domain != NULL && cookie->domain != NULL) && octstr_compare(value->domain, cookie->domain) == 0)) ) { /* We have a match according to 4.3.3 - discard the old one */ cookie_destroy(value); gwlist_delete(cookies, pos, 1); /* Discard the new cookie also if max-age is 0 - set if expiry date is up */ if (cookie->max_age == 0) { debug("wap.wsp.http", 0, "have_cookie: Discarding expired cookie (%s)", octstr_get_cstr(cookie->name)); return 1; } debug("wap.wsp.http", 0, "have_cookie: Updating cached cookie (%s)", octstr_get_cstr (cookie->name)); break; } else { pos++; } } return 0; }
Octstr *wsp_cap_pack_list(List *caps_list) { Octstr *result; Capability *cap; long i, len; result = octstr_create(""); len = gwlist_len(caps_list); for (i = 0; i < len; i++) { long datalen; cap = gwlist_get(caps_list, i); datalen = 0; if (cap->data) datalen = octstr_len(cap->data); if (datalen == 0 && cap->accept) continue; if (cap->name) { if (octstr_get_char(cap->name, 0) >= 0x80 || octstr_search_char(cap->name, 0, 0) >= 0) { error(0, "WSP: Bad capability."); wsp_cap_dump(cap); continue; } /* Add length */ octstr_append_uintvar(result, octstr_len(cap->name) + 1 + datalen); /* Add identifier */ octstr_append(result, cap->name); octstr_append_char(result, 0); } else { if (cap->id >= 0x80 || cap->id < 0) { error(0, "WSP: Bad capability."); wsp_cap_dump(cap); continue; } /* Add length */ octstr_append_uintvar(result, 1 + datalen); /* Add identifier */ octstr_append_char(result, 0x80 | cap->id); } /* Add payload, if any */ if (cap->data) { octstr_append(result, cap->data); } } return result; }
void wsp_cap_dump_list(List *caps_list) { long i; if (caps_list == NULL) { debug("wsp", 0, "NULL capability list"); return; } debug("wsp", 0, "Dumping capability list at %p, length %ld", caps_list, gwlist_len(caps_list)); for (i = 0; i < gwlist_len(caps_list); i++) { wsp_cap_dump(gwlist_get(caps_list, i)); } debug("wsp", 0, "End of capability list dump"); }
List *cfg_get_multi_group(Cfg *cfg, Octstr *name) { List *list, *copy; long i; list = dict_get(cfg->multi_groups, name); if (list == NULL) return NULL; copy = gwlist_create(); for (i = 0; i < gwlist_len(list); ++i) gwlist_append(copy, gwlist_get(list, i)); return copy; }
static long dlr_pgsql_messages(void) { Octstr *sql; long ret; List *res; sql = octstr_format("SELECT count(*) FROM %s;", octstr_get_cstr(fields->table)); res = pgsql_select(sql); octstr_destroy(sql); if (res == NULL || gwlist_len(res) < 1) { error(0, "PGSQL: Could not get count of DLR table"); ret = -1; } else { ret = atol(octstr_get_cstr(gwlist_get(gwlist_get(res, 0), 0))); } gwlist_destroy(gwlist_extract_first(res), octstr_destroy_item); gwlist_destroy(res, NULL); return ret; }
/* Counts the number of elements produced to any deliver threads (live elements) * Used to control how many new queueings to accept. * Each of these may use upto 3 filehandles * - One for the qf * - One for the df file * - Delivery notification file * Does sockets count as filehandles? */ static int mms_queue_live_count() { int i,j,count = 0; static struct Qthread_t *tlist; List *queue_run_thread_groups = dict_keys(tlists); for (i = 0; i < gwlist_len(queue_run_thread_groups); i++) { tlist = dict_get(tlists, gwlist_get(queue_run_thread_groups, i)); for (j = 0; j < number_of_threads; j++) count += gwlist_len(tlist[j].l); } gwlist_destroy(queue_run_thread_groups, octstr_destroy_item); return count; }
/* * this thread listens to incoming_wdp list * and then routs messages to proper wapbox */ static void wdp_to_wapboxes(void *arg) { List *route_info; AddrPar *ap; Boxc *conn; Msg *msg; int i; gwlist_add_producer(flow_threads); gwlist_add_producer(wapbox_list); route_info = gwlist_create(); while(bb_status != BB_DEAD) { gwlist_consume(suspended); /* block here if suspended */ if ((msg = gwlist_consume(incoming_wdp)) == NULL) break; gw_assert(msg_type(msg) == wdp_datagram); conn = route_msg(route_info, msg); if (conn == NULL) { warning(0, "Cannot route message, discard it"); msg_destroy(msg); continue; } gwlist_produce(conn->incoming, msg); } debug("bb", 0, "wdp_to_wapboxes: destroying lists"); while((ap = gwlist_extract_first(route_info)) != NULL) ap_destroy(ap); gw_assert(gwlist_len(route_info) == 0); gwlist_destroy(route_info, NULL); gwlist_lock(wapbox_list); for(i=0; i < gwlist_len(wapbox_list); i++) { conn = gwlist_get(wapbox_list, i); gwlist_remove_producer(conn->incoming); conn->alive = 0; } gwlist_unlock(wapbox_list); gwlist_remove_producer(wapbox_list); gwlist_remove_producer(flow_threads); }
int get_cookies(List *headers, const WSPMachine *sm) { Octstr *header = NULL; Octstr *value = NULL; Cookie *cookie = NULL; long pos = 0; /* * This can happen if the user aborts while the HTTP request is pending from the server. * In that case, the session machine is destroyed and is not available to this function * for cookie caching. */ if (sm == NULL) { info (0, "No session machine for cookie retrieval"); return 0; } for (pos = 0; pos < gwlist_len(headers); pos++) { header = gwlist_get(headers, pos); /* debug ("wap.wsp.http", 0, "get_cookies: Examining header (%s)", octstr_get_cstr (header)); */ if (strncasecmp ("set-cookie", octstr_get_cstr (header),10) == 0) { debug ("wap.wsp.http", 0, "Caching cookie (%s)", octstr_get_cstr (header)); if ((value = get_header_value (header)) == NULL) { error (0, "get_cookies: No value in (%s)", octstr_get_cstr(header)); continue; } /* Parse the received cookie */ if ((cookie = parse_cookie(value)) != NULL) { /* Check to see if this cookie is already present */ if (have_cookie(sm->cookies, cookie) == 1) { debug("wap.wsp.http", 0, "parse_cookie: Cookie present"); cookie_destroy(cookie); continue; } else { add_cookie_to_cache(sm, cookie); debug("wap.wsp.http", 0, "get_cookies: Added (%s)", octstr_get_cstr(cookie -> name)); } } } } debug("wap.wsp.http", 0, "get_cookies: End"); return 0; }
static void dump_tpis(List *tpis, int level) { int i; int num_tpis; WTP_TPI *tpi; if (tpis == NULL) return; num_tpis = gwlist_len(tpis); for (i = 0; i < num_tpis; i++) { tpi = gwlist_get(tpis, i); debug("wap.wtp", 0, "%*s TPI type %u:", level, "", tpi->type); octstr_dump(tpi->data, level + 1); } }
/* Remove part i in list of body parts. */ void mime_entity_remove_part(MIMEEntity *e, int i) { MIMEEntity *m; gw_assert(e != NULL); gw_assert(i >= 0); gw_assert(i < gwlist_len(e->multiparts)); m = gwlist_get(e->multiparts, i); gwlist_delete(e->multiparts, i, 1); if (m == e->start) e->start = NULL; mime_entity_destroy(m); }
void mms_cfg_destroy(mCfg *cfg) { List *l; int i, n; gw_assert(cfg); for (i = 0, l = dict_keys(cfg->grps), n = gwlist_len(l); i < n; i++) { Octstr *grpname = gwlist_get(l, i); void *val = dict_get(cfg->grps, grpname); if (is_multigroup(grpname)) { /* item is a list. */ List *gl = val; int j, m = gwlist_len(gl); for (j = 0; j < m; j++) mGrp_destroy(gwlist_get(gl, j)); gwlist_destroy(gl, NULL); } else mGrp_destroy(val); } gwlist_destroy(l, (gwlist_item_destructor_t *)octstr_destroy); dict_destroy(cfg->grps); octstr_destroy(cfg->file); gw_free(cfg); }
List *mms_cfg_get_multi(mCfg *cfg, Octstr *name) { List *l = NULL, *r; int i; gw_assert(name); gw_assert(is_multigroup(name) == 1); r = dict_get(cfg->grps, name); if (r) for (i = 0, l = gwlist_create(); i < gwlist_len(r); i++) gwlist_append(l, gwlist_get(r,i)); return l; }
int boxc_incoming_wdp_queue(void) { int i, q = 0; Boxc *boxc; if (wapbox_list) { gwlist_lock(wapbox_list); for(i=0; i < gwlist_len(wapbox_list); i++) { boxc = gwlist_get(wapbox_list, i); q += gwlist_len(boxc->incoming); } gwlist_unlock(wapbox_list); } return q; }
URLTranslation *urltrans_find_username(URLTranslationList *trans, Octstr *name) { URLTranslation *t; int i; gw_assert(name != NULL); for (i = 0; i < gwlist_len(trans->list); ++i) { t = gwlist_get(trans->list, i); if (t->type == TRANSTYPE_SENDSMS) { if (octstr_compare(name, t->username) == 0) return t; } } return NULL; }
static int sqlite3_update(void *theconn, const Octstr *sql, List *binds) { sqlite3 *db = theconn; sqlite3_stmt *stmt; const char *rem; int status; int rows; int i; int binds_len = (binds ? gwlist_len(binds) : 0); /* prepare statement */ #if SQLITE_VERSION_NUMBER >= 3003009 status = sqlite3_prepare_v2(db, octstr_get_cstr(sql), octstr_len(sql) + 1, &stmt, &rem); #else status = sqlite3_prepare(db, octstr_get_cstr(sql), octstr_len(sql) + 1, &stmt, &rem); #endif if (SQLITE_OK != status) { error(0, "SQLite3: %s", sqlite3_errmsg(db)); return -1; } debug("dbpool.sqlite3",0,"sqlite3_prepare done"); /* bind variables */ for (i = 0; i < binds_len; i++) { Octstr *bind = gwlist_get(binds, i); status = sqlite3_bind_text(stmt, i + 1, octstr_get_cstr(bind), octstr_len(bind), SQLITE_STATIC); if (SQLITE_OK != status) { error(0, "SQLite3: %s", sqlite3_errmsg(db)); sqlite3_finalize(stmt); return -1; } } /* execute our statement */ if ((status = sqlite3_step(stmt)) != SQLITE_DONE) { error(0, "SQLite3: %s", sqlite3_errmsg(db)); sqlite3_finalize(stmt); return -1; } debug("dbpool.sqlite3",0,"sqlite3_step done"); rows = sqlite3_changes(db); debug("dbpool.sqlite3",0,"rows processed = %d", rows); sqlite3_finalize(stmt); return rows; }