/* * Function to allocate a new struct dlr_entry entry * and intialize it to zero */ struct dlr_entry *dlr_entry_create(void) { struct dlr_entry *dlr; dlr = gw_malloc(sizeof(*dlr)); gw_assert(dlr != NULL); /* set all values to NULL */ memset(dlr, 0, sizeof(*dlr)); return dlr; }
static struct area *find_area(unsigned char *p) { long index; struct area *area; long suspicious_pointer; unsigned long p_ul; gw_assert(p != NULL); p_ul = (unsigned long) p; suspicious_pointer = (sizeof(p) == sizeof(long) && (p_ul == NEW_AREA_PATTERN || p_ul == FREE_AREA_PATTERN || p_ul == START_MARK_PATTERN || p_ul == END_MARK_PATTERN)); if (slow || suspicious_pointer) { /* Extra check, which does not touch the (perhaps not allocated) * memory area. It's slow, but may help pinpoint problems that * would otherwise cause segfaults. */ for (index = 0; index < num_allocations; index++) { if (allocated[index].area == p) break; } if (index == num_allocations) { error(0, "Area %p not found in allocation table.", p); return NULL; } } index = check_startmark(p); if (index >= 0 && index < num_allocations && allocated[index].area == p) { area = &allocated[index]; if (check_endmark(p, area->area_size) < 0) { error(0, "End marker was damaged for area %p", p); dump_area(area); } return area; } error(0, "Start marker was damaged for area %p", p); for (index = 0; index < num_allocations; index++) { if (allocated[index].area == p) { area = &allocated[index]; dump_area(area); return area; } } error(0, "Could not find area information."); return NULL; }
static struct dlr_entry *dlr_pgsql_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst) { struct dlr_entry *res = NULL; Octstr *sql; List *result, *row; sql = octstr_format("SELECT %s, %s, %s, %s, %s, %s FROM %s WHERE %s='%s' AND %s='%s' LIMIT 1;", octstr_get_cstr(fields->field_mask), octstr_get_cstr(fields->field_serv), octstr_get_cstr(fields->field_url), octstr_get_cstr(fields->field_src), octstr_get_cstr(fields->field_dst), octstr_get_cstr(fields->field_boxc), octstr_get_cstr(fields->table), octstr_get_cstr(fields->field_smsc), octstr_get_cstr(smsc), octstr_get_cstr(fields->field_ts), octstr_get_cstr(ts)); result = pgsql_select(sql); octstr_destroy(sql); if (result == NULL || gwlist_len(result) < 1) { debug("dlr.pgsql", 0, "no rows found"); while((row = gwlist_extract_first(result))) gwlist_destroy(row, octstr_destroy_item); gwlist_destroy(result, NULL); return NULL; } row = gwlist_get(result, 0); debug("dlr.pgsql", 0, "Found entry, col1=%s, col2=%s, col3=%s, col4=%s, col5=%s col6=%s", octstr_get_cstr(gwlist_get(row, 0)), octstr_get_cstr(gwlist_get(row, 1)), octstr_get_cstr(gwlist_get(row, 2)), octstr_get_cstr(gwlist_get(row, 3)), octstr_get_cstr(gwlist_get(row, 4)), octstr_get_cstr(gwlist_get(row, 5)) ); res = dlr_entry_create(); gw_assert(res != NULL); res->mask = atoi(octstr_get_cstr(gwlist_get(row, 0))); res->service = octstr_duplicate(gwlist_get(row, 1)); res->url = octstr_duplicate(gwlist_get(row, 2)); res->source = octstr_duplicate(gwlist_get(row, 3)); res->destination = octstr_duplicate(gwlist_get(row, 4)); res->boxc_id = octstr_duplicate(gwlist_get(row, 5)); res->smsc = octstr_duplicate(smsc); while((row = gwlist_extract_first(result))) gwlist_destroy(row, octstr_destroy_item); gwlist_destroy(result, NULL); return res; }
void mime_entity_destroy(MIMEEntity *e) { gw_assert(e != NULL); if (e->headers != NULL) gwlist_destroy(e->headers, octstr_destroy_item); if (e->multiparts != NULL) gwlist_destroy(e->multiparts, mime_entity_destroy_item); octstr_destroy(e->body); e->start = NULL; /* will be destroyed on it's own via gwlist_destroy */ gw_free(e); }
void gwlist_sort(List *list, int(*cmp)(const void *, const void *)) { gw_assert(list != NULL && cmp != NULL); lock(list); if (list->len == 0) { /* nothing to sort */ unlock(list); return; } quicksort(list, 0, list->len - 1, cmp); unlock(list); }
static WAPEvent *pack_error(WAPEvent *datagram) { WAPEvent *event; gw_assert(datagram->type == T_DUnitdata_Ind); event = wap_event_create(RcvErrorPDU); event->u.RcvErrorPDU.tid = deduce_tid(datagram->u.T_DUnitdata_Ind.user_data); event->u.RcvErrorPDU.addr_tuple = wap_addr_tuple_duplicate(datagram->u.T_DUnitdata_Ind.addr_tuple); return event; }
int gwthread_cancel(long thread) { struct threadinfo *threadinfo; gw_assert(thread >= 0); threadinfo = THREAD(thread); if (threadinfo == NULL || threadinfo->number != thread) { return -1; } else { return pthread_cancel(threadinfo->self); } }
int udp_sendto(int s, Octstr *datagram, Octstr *addr) { struct sockaddr_in sa; gw_assert(octstr_len(addr) == sizeof(sa)); memcpy(&sa, octstr_get_cstr(addr), sizeof(sa)); if (sendto(s, octstr_get_cstr(datagram), octstr_len(datagram), 0, (struct sockaddr *) &sa, (int) sizeof(sa)) == -1) { error(errno, "Couldn't send UDP packet"); return -1; } return 0; }
int parse_skip(ParseContext *context, long count) { gw_assert(context != NULL); if (context->pos + count > context->limit) { context->pos = context->limit; context->error = 1; return -1; } context->pos += count; return 0; }
void *gw_check_malloc(size_t size, const char *filename, long lineno, const char *function) { unsigned char *p; gw_assert(initialized); /* ANSI C89 says malloc(0) is implementation-defined. Avoid it. */ gw_assert(size > 0); p = malloc(size + 2 * MARKER_SIZE); if (p == NULL) panic(errno, "Memory allocation of %ld bytes failed.", (long)size); p += MARKER_SIZE; lock(); fill(p, size, NEW_AREA_PATTERN); record_allocation(p, size, filename, lineno, function); unlock(); return p; }
/* * Send a message to the bearerbox for delivery to a phone. * Return >= 0 for success & count of splitted sms messages, * -1 for failure. Does not destroy the msg. */ static int send_message(Msg *msg) { unsigned long msg_count; List *list; gw_assert(msg != NULL); gw_assert(msg_type(msg) == sms); /* * Encode our smsbox-id to the msg structure. * This will allow bearerbox to return specific answers to the * same smsbox, mainly for DLRs and SMS proxy modes. * * In addition the -x flag can be used to identify the mtbatch * instance with an own smsbox-id, but let the normal smsbox * daemons handle the DLRs coming back, as the mtbatch shuts down * after all MTs have been injected. It's not meant to process * the DLR messages. */ if (no_smsbox_id == 0 && smsbox_id != NULL) { msg->sms.boxc_id = octstr_duplicate(smsbox_id); } list = sms_split(msg, NULL, NULL, NULL, NULL, 1, 0, 100, MAX_SMS_OCTETS); msg_count = gwlist_len(list); gwlist_destroy(list, msg_destroy_item); debug("sms", 0, "message length %ld, sending %ld messages", octstr_len(msg->sms.msgdata), msg_count); if (delay > 0) gwthread_sleep(delay); /* pass message to bearerbox */ if (deliver_to_bearerbox(msg) != 0) return -1; return msg_count; }
void list_sort(List *list, int(*cmp)(const void *, const void *)) { gw_assert(list != NULL && cmp != NULL); list_lock(list); if (list->len == 0) { /* nothing to sort */ list_unlock(list); return; } qsort(&GET(list, 0), list->len, sizeof(void*), cmp); list_unlock(list); }
static void udpc_destroy(Udpc *udpc) { if (udpc == NULL) return; if (udpc->fd >= 0) close(udpc->fd); octstr_destroy(udpc->addr); gw_assert(gwlist_len(udpc->outgoing_list) == 0); gwlist_destroy(udpc->outgoing_list, NULL); gw_free(udpc); }
static unsigned long decode_integer(Octstr *os, long pos, int octets) { unsigned long u; int i; gw_assert(octstr_len(os) >= pos + octets); u = 0; for (i = 0; i < octets; ++i) u = (u << 8) | octstr_get_char(os, pos + i); return u; }
/* Case-insensitive string lookup */ static long string_to_number(Octstr *ostr, struct table *table) { long i; gw_assert(initialized); for (i = 0; i < table->size; i++) { if (octstr_case_compare(ostr, table->strings[i]) == 0) { return table->linear ? i : table->numbers[i]; } } return -1; }
/* * Add new dlr entry into dlr storage */ void dlr_add(const Octstr *smsc, const Octstr *ts, Msg *msg) { struct dlr_entry *dlr = NULL; /* Add the foreign_id so all SMSC modules can use it. * Obey also the original message in the split_parts list. */ if (msg->sms.foreign_id != NULL) octstr_destroy(msg->sms.foreign_id); msg->sms.foreign_id = octstr_duplicate(ts); if (msg->sms.split_parts != NULL) { struct split_parts *split = msg->sms.split_parts; if (split->orig->sms.foreign_id != NULL) octstr_destroy(split->orig->sms.foreign_id); split->orig->sms.foreign_id = octstr_duplicate(ts); } if(octstr_len(smsc) == 0) { warning(0, "DLR[%s]: Can't add a dlr without smsc-id", dlr_type()); return; } /* sanity check */ if (handles == NULL || handles->dlr_add == NULL || msg == NULL) return; /* check if delivery receipt requested */ if (!DLR_IS_ENABLED(msg->sms.dlr_mask)) return; /* allocate new struct dlr_entry struct */ dlr = dlr_entry_create(); gw_assert(dlr != NULL); /* now copy all values, we are interested in */ dlr->smsc = (smsc ? octstr_duplicate(smsc) : octstr_create("")); dlr->timestamp = (ts ? octstr_duplicate(ts) : octstr_create("")); dlr->source = (msg->sms.sender ? octstr_duplicate(msg->sms.sender) : octstr_create("")); dlr->destination = (msg->sms.receiver ? octstr_duplicate(msg->sms.receiver) : octstr_create("")); dlr->service = (msg->sms.service ? octstr_duplicate(msg->sms.service) : octstr_create("")); dlr->url = (msg->sms.dlr_url ? octstr_duplicate(msg->sms.dlr_url) : octstr_create("")); dlr->boxc_id = (msg->sms.boxc_id ? octstr_duplicate(msg->sms.boxc_id) : octstr_create("")); dlr->mask = msg->sms.dlr_mask; debug("dlr.dlr", 0, "DLR[%s]: Adding DLR smsc=%s, ts=%s, src=%s, dst=%s, mask=%d, boxc=%s", dlr_type(), octstr_get_cstr(dlr->smsc), octstr_get_cstr(dlr->timestamp), octstr_get_cstr(dlr->source), octstr_get_cstr(dlr->destination), dlr->mask, octstr_get_cstr(dlr->boxc_id)); /* call registered function */ handles->dlr_add(dlr); }
/* Tell the fdset whether we are interested in POLLOUT events, but only * if the status changed. (Calling fdset_listen can be expensive if * it requires synchronization with the polling thread.) * We must already have the outlock. */ static void unlocked_register_pollout(Connection *conn, int onoff) { gw_assert(conn->registered); if (onoff == 1 && !conn->listening_pollout) { /* Turn it on */ conn->listening_pollout = 1; fdset_listen(conn->registered, conn->fd, POLLOUT, POLLOUT); } else if (onoff == 0 && conn->listening_pollout) { /* Turn it off */ conn->listening_pollout = 0; fdset_listen(conn->registered, conn->fd, POLLOUT, 0); } }
/****************************************************************************** * * EXTERNAL FUNCTIONS: * * Handles a possible concatenated message. Creates a list of wap events. */ List *wtp_unpack_wdp_datagram(WAPEvent *datagram) { List *events = NULL; WAPEvent *event = NULL; WAPEvent *subdgram = NULL; Octstr *data = NULL; long pdu_len; gw_assert(datagram->type == T_DUnitdata_Ind); events = gwlist_create(); if (concatenated_message(datagram->u.T_DUnitdata_Ind.user_data)) { data = octstr_duplicate(datagram->u.T_DUnitdata_Ind.user_data); octstr_delete(data, 0, 1); while (octstr_len(data) != 0) { if (octstr_get_bits(data, 0, 1) == 0) { pdu_len = octstr_get_char(data, 0); octstr_delete(data, 0, 1); } else { pdu_len = octstr_get_bits(data, 1, 15); octstr_delete(data, 0, 2); } subdgram = wap_event_duplicate(datagram); octstr_destroy(subdgram->u.T_DUnitdata_Ind.user_data); subdgram->u.T_DUnitdata_Ind.user_data = octstr_copy(data, 0, pdu_len); wap_event_assert(subdgram); if ((event = unpack_wdp_datagram_real(subdgram)) != NULL) { wap_event_assert(event); gwlist_append(events, event); } octstr_delete(data, 0, pdu_len); wap_event_destroy(subdgram); } octstr_destroy(data); } else if ((event = unpack_wdp_datagram_real(datagram)) != NULL) { wap_event_assert(event); gwlist_append(events, event); } else { warning(0, "WTP: Dropping unhandled datagram data:"); octstr_dump(datagram->u.T_DUnitdata_Ind.user_data, 0, GW_WARNING); } return events; }
void smscconn_start(SMSCConn *conn) { gw_assert(conn != NULL); mutex_lock(conn->flow_mutex); if (conn->status == SMSCCONN_DEAD || conn->is_stopped == 0) { mutex_unlock(conn->flow_mutex); return; } conn->is_stopped = 0; mutex_unlock(conn->flow_mutex); if (conn->start_conn) conn->start_conn(conn); }
void wsp_push_client_shutdown(void) { gw_assert(push_client_run_status == running); push_client_run_status = terminating; gwlist_remove_producer(push_client_queue); gwthread_join_every(main_thread); debug("wap.wsp", 0, "wsp_push_client_shutdown: %ld push client machines" "left", gwlist_len(push_client_machines)); gwlist_destroy(push_client_machines, push_client_machine_destroy); gwlist_destroy(push_client_queue, wap_event_destroy_item); counter_destroy(push_client_machine_id_counter); }
List *mime_entity_headers(MIMEEntity *m) { List *headers; gw_assert(m != NULL && m->headers != NULL); /* Need a fixup before hand over. */ if (mime_entity_num_parts(m) > 0) fix_boundary_element(m->headers, NULL); headers = http_header_duplicate(m->headers); return headers; }
long semaphore_getvalue(Semaphore *semaphore) { gw_assert(semaphore != NULL); #ifdef HAVE_SEMAPHORE { int val; if (sem_getvalue(&semaphore->sem, &val) != 0) panic(errno, "Could not get semaphore value."); return val; } #else return list_len(semaphore->list); #endif }
int gw_pcre_match_pre_real(const pcre *preg, const Octstr *os, const char *file, long line, const char *func) { int rc; int ovector[PCRE_OVECCOUNT]; gw_assert(preg != NULL); /* execute and match */ rc = gw_pcre_exec_real(preg, os, 0, 0, ovector, PCRE_OVECCOUNT, file, line, func); return (rc > 0) ? 1 : 0; }
void dbpool_destroy(DBPool *p) { if (p == NULL) return; /* nothing todo here */ gw_assert(p->pool != NULL && p->db_ops != NULL); gwlist_remove_producer(p->pool); gwlist_destroy(p->pool, (void*) dbpool_conn_destroy); p->db_ops->conf_destroy(p->conf); gw_free(p); }
void wsp_session_shutdown(void) { gw_assert(run_status == running); run_status = terminating; gwlist_remove_producer(queue); gwthread_join_every(main_thread); gwlist_destroy(queue, wap_event_destroy_item); debug("wap.wsp", 0, "WSP: %ld session machines left.", gwlist_len(session_machines)); gwlist_destroy(session_machines, machine_destroy); counter_destroy(session_id_counter); wsp_strings_shutdown(); }
static void oracle_close_conn(void *theconn) { struct ora_conn *conn = (struct ora_conn*) theconn; gw_assert(conn != NULL); if (conn->svchp != NULL) oracle_checkerr(conn->errhp, OCILogoff(conn->svchp, conn->errhp)); OCIHandleFree(conn->errhp, OCI_HTYPE_ERROR); OCIHandleFree(conn->envp, OCI_HTYPE_ENV); /* OCITerminate(OCI_DEFAULT); */ gw_free(conn); }
Octstr *parse_get_octets(ParseContext *context, long length) { Octstr *result; gw_assert(context != NULL); if (context->pos + length > context->limit) { context->error = 1; return NULL; } result = octstr_copy(context->data, context->pos, length); context->pos += length; return result; }
void *list_extract_first(List *list) { void *item; gw_assert(list != NULL); lock(list); if (list->len == 0) item = NULL; else { item = GET(list, 0); delete_items_from_list(list, 0, 1); } unlock(list); return item; }
void wap_push_ota_init(wap_dispatch_func_t *wsp_dispatch, wap_dispatch_func_t *wsp_unit_dispatch) { ota_queue = list_create(); list_add_producer(ota_queue); dispatch_to_wsp = wsp_dispatch; dispatch_to_wsp_unit = wsp_unit_dispatch; bearerbox = bearerbox_address_create(); gw_assert(run_status == limbo); run_status = running; gwthread_create(main_thread, NULL); }
void gwlib_init(void) { gw_assert(!init); gw_init_mem(); uuid_init(); octstr_init(); gwlib_protected_init(); gwthread_init(); log_init(); http_init(); socket_init(); charset_init(); cfg_init(); init = 1; }