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; }
long octstr_oct_index(octstr_t *s, long pos, unsigned int oct) { unsigned char *p = (unsigned char *)octstr_get_cstr(s); long len = octstr_len(s); int i; for( i = pos ; i < len ; i++ ) { if( p[i] == oct ) return i; } return -1; }
static octstr_t * octstr_find_and_replace1(octstr_t *s, octstr_t *what, octstr_t *replace, long (*find)(octstr_t *, long, octstr_t *)) { long pos, pos1; octstr_t *s1, *tmp; s1 = octstr_create(NULL, 0); pos = 0; while( (pos1 = find(s, pos, what)) > 0 ) { tmp = octstr_substr(s, pos, pos1-pos); octstr_append(s1, tmp); octstr_append(s1, replace); octstr_destroy(tmp); pos = pos1 + octstr_len(what); } tmp = octstr_substr(s, pos, octstr_len(s)-pos); octstr_append(s1, tmp); octstr_destroy(tmp); return s1; }
ParseContext *parse_context_create(Octstr *str) { ParseContext *result; result = gw_malloc(sizeof(*result)); result->data = str; result->pos = 0; result->limit = octstr_len(str); result->limit_stack = NULL; result->error = 0; return result; }
void octstr_print(octstr_t *octstr) { int i; unsigned char *ptr; ptr = octstr_ptr(octstr); for (i = 0; i < octstr_len(octstr); i++) { printf("\\x%2.2X", *ptr++ & 0xff); } printf("\n"); }
List *wsp_cap_unpack_list(Octstr *caps) { List *caps_list; long pos, capslen; caps_list = gwlist_create(); if (caps == NULL) return caps_list; capslen = octstr_len(caps); pos = 0; while (pos < capslen) { unsigned long length; int id; Octstr *name; Octstr *data; pos = octstr_extract_uintvar(caps, &length, pos); if (pos < 0 || length == 0) goto error; id = octstr_get_char(caps, pos); if (id >= 0x80) { id &= 0x7f; /* It's encoded as a short-integer */ name = NULL; data = octstr_copy(caps, pos + 1, length - 1); } else { long nullpos; id = -1; /* It's encoded as token-text */ nullpos = octstr_search_char(caps, 0, pos); if (nullpos < 0) goto error; /* check length * FIXME: If it's not allowed that data is empty then change check * to <= . */ if (length < (nullpos + 1 - pos)) goto error; name = octstr_copy(caps, pos, nullpos - pos); data = octstr_copy(caps, nullpos + 1, length - (nullpos + 1 - pos)); } gwlist_append(caps_list, wsp_cap_create(id, name, data)); pos += length; } return caps_list; error: warning(0, "WSP: Error unpacking capabilities"); return caps_list; }
static int sms_to_client(Connection *client, Msg *msg) { Octstr *line; Octstr *msgdata = NULL; /* NULL to allow octstr_destroy */ debug("bb.sms", 0, "smsc_fake: sending message to client"); /* msg_dump(msg, 0); */ line = octstr_duplicate(msg->sms.sender); octstr_append_char(line, ' '); octstr_append(line, msg->sms.receiver); if (octstr_len(msg->sms.udhdata)) { octstr_append(line, octstr_imm(" udh ")); msgdata = octstr_duplicate(msg->sms.udhdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); octstr_destroy(msgdata); octstr_append(line, octstr_imm(" data ")); msgdata = octstr_duplicate(msg->sms.msgdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); } else { if (msg->sms.coding == DC_8BIT) { octstr_append(line, octstr_imm(" data ")); msgdata = octstr_duplicate(msg->sms.msgdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); } else if (msg->sms.coding == DC_UCS2) { octstr_append(line, octstr_imm(" ucs-2 ")); msgdata = octstr_duplicate(msg->sms.msgdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); } else { octstr_append(line, octstr_imm(" text ")); octstr_append(line, msg->sms.msgdata); } } octstr_append_char(line, 10); if (conn_write(client, line) == -1) { octstr_destroy(msgdata); octstr_destroy(line); return -1; } octstr_destroy(msgdata); octstr_destroy(line); return 1; }
long octstr_str_caseindex(octstr_t *s, long pos, const char *str) { char *p, *ptr; if( pos >= octstr_len(s) ) return -1; p = octstr_get_cstr(s); if( (ptr = strcasestr(p + pos, str)) != NULL ) { return ptr - p; } return -1; }
int conn_write_withlen(Connection *conn, Octstr *data) { int ret; unsigned char lengthbuf[4]; encode_network_long(lengthbuf, octstr_len(data)); lock_out(conn); octstr_append_data(conn->outbuf, lengthbuf, 4); octstr_append(conn->outbuf, data); ret = unlocked_try_write(conn); unlock_out(conn); return ret; }
static long pass_field_value(Octstr **body_part, Octstr **header, long pos) { int c; long start; Octstr *field = NULL; start = pos; while (!is_cr(c = octstr_get_char(*body_part, pos)) && pos < octstr_len(*body_part)) { ++pos; } if (pos == octstr_len(*body_part)) { return -1; } field = octstr_copy(*body_part, start, pos - start); octstr_append(*header, field); octstr_destroy(field); return pos; }
static long decode_integer(Octstr *os, long pos, int octets) { unsigned long u; int i; if (octstr_len(os) < pos + octets) return -1; u = 0; for (i = 0; i < octets; ++i) u = (u << 8) | octstr_get_char(os, pos + i); return u; }
static int ois_append_submission_options(char *raw, const Msg *msg) { SAY(3, "ois_append_submission_options"); /* bit field, bit 0=reply path, bit 1=udh, bits 3-4=dcs interpretation */ raw[0] = (char) 0x00; if (octstr_len(msg->sms.udhdata)) { raw[0] |= (char) 0x02; } if (msg->sms.coding == DC_8BIT) { /* XXX and UCS-2? */ raw[0] |= (char) 0x10; } return 1; }
/****************************************************************************** * * 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; }
static long pass_field_name(Octstr **body_part, Octstr **field_part, long pos) { int c; long start; Octstr *name = NULL; start = pos; while (((c = octstr_get_char(*body_part, pos)) != ':') && pos < octstr_len(*body_part)) { ++pos; } if (pos == octstr_len(*body_part)) { return -1; } name = octstr_copy(*body_part, start, pos - start); octstr_append(*field_part, name); octstr_destroy(name); return pos; }
static void write_msg(Msg *msg) { Octstr *pack; unsigned char buf[4]; pack = store_msg_pack(msg); encode_network_long(buf, octstr_len(pack)); octstr_insert_data(pack, 0, (char*)buf, 4); octstr_print(file, pack); fflush(file); octstr_destroy(pack); }
static long parse_field_value(Octstr *pap_content, long pos) { int c; while (!is_cr(c = octstr_get_char(pap_content, pos)) && pos < octstr_len(pap_content)) { ++pos; } if (is_lf(c)) { if (is_lf(octstr_get_char(pap_content, pos))) { ++pos; } else { return -1; } } if (pos == octstr_len(pap_content)) { return -1; } return pos; }
/* * 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); }
/* * Trans being NULL means that we are servicing ppg (doing dlr, but this does not * concern us here). */ Octstr *urltrans_get_pattern(URLTranslation *t, Msg *request) { Octstr *result, *pattern; if (request->sms.sms_type != report_mo && t->type == TRANSTYPE_SENDSMS) return octstr_create(""); /* check if this is a delivery report message or not */ if (request->sms.sms_type != report_mo) { pattern = t->pattern; } else { /* this is a DLR message */ pattern = request->sms.dlr_url; if (octstr_len(pattern) == 0) { if (t && octstr_len(t->dlr_url)) { pattern = t->dlr_url; } else { return octstr_create(""); } } } /* We have pulled this out into an own exported function. This * gives other modules the chance to use the same escape code * semantics for Msgs. */ result = urltrans_fill_escape_codes(pattern, request); /* * this SHOULD be done in smsbox, not here, but well, * much easier to do here */ if (t && (t->type == TRANSTYPE_POST_URL || t->type == TRANSTYPE_POST_XML) && t->strip_keyword) strip_keyword(request); return result; }
/* * In the case of SI documents, only attribute values to be tokenized are * parts of urls (see si, chapter 9.3.3). The caller romoves the start of an * url. Check whether we can find parts in the value. If not, parse value a an * inline string, otherwise parse parts before and after tokenizable parts as * inline strings. */ void parse_url_value(Octstr *value, simple_binary_t **sibxml) { size_t i; long pos; Octstr *urlos, *first_part, *last_part; size_t first_part_len; i = 0; first_part_len = 0; first_part = NULL; last_part = NULL; while (i < NUMBER_OF_URL_VALUES) { pos = octstr_search(value, urlos = octstr_imm(si_URL_values[i].name), 0); if (pos >= 0) { first_part = octstr_duplicate(value); octstr_delete(first_part, pos, octstr_len(first_part) - pos); first_part_len = octstr_len(first_part); parse_inline_string(first_part, sibxml); output_char(si_URL_values[i].token, sibxml); last_part = octstr_duplicate(value); octstr_delete(last_part, 0, first_part_len + octstr_len(urlos)); parse_inline_string(last_part, sibxml); octstr_destroy(first_part); octstr_destroy(last_part); break; } octstr_destroy(urlos); ++i; } if (pos < 0) parse_inline_string(value, sibxml); }
long octstr_unpack_ntohl(octstr_t *s, long pos, long len, unsigned long *value) { uint32_t v; unsigned char *ptr; if( pos+len >= octstr_len(s) ) return -1; ptr = (unsigned char *)octstr_get_cstr(s); v = data_ntohl(ptr+pos, len); if( value ) *value = v; return pos + len; }
octstr_t * octstr_to_hex(octstr_t *s) { octstr_t *hex; int len, i; char buf[3]; hex = octstr_create(NULL, 0); len = octstr_len(s); for( i = 0 ; i < len ; i++ ) { sprintf(buf, "%.2x", (unsigned int)s->s[i]); octstr_str_append(hex, buf, 2); } return hex; }
/* * Count the number of times `pat' occurs in `str'. */ static long count_occurences(Octstr *str, Octstr *pat) { long count; long pos; long len; count = 0; pos = 0; len = octstr_len(pat); while ((pos = octstr_search(str, pat, pos)) != -1) { ++count; pos += len; } return count; }
static long eat_int_parm(Octstr *packet, int parm, int maxlen) { Octstr *value; long result; value = eat_string_parm(packet, parm, maxlen); if (!value) return INT_MIN; result = eat_number(value); if (octstr_len(value) > 0) result = INT_MIN; octstr_destroy(value); return result; }
static Octstr *eat_string_parm(Octstr *packet, int parm, int maxlen) { long start, datastart; long tab; Octstr *result; Octstr *parmheader; parmheader = octstr_format("%c%03d:", TAB, parm); start = octstr_search(packet, parmheader, 0); if (start < 0) { octstr_destroy(parmheader); return NULL; } datastart = start + octstr_len(parmheader); tab = octstr_search_char(packet, TAB, datastart + 1); if (tab < 0) { tab = octstr_len(packet); } result = octstr_copy(packet, datastart, tab - datastart); octstr_delete(packet, start, tab - start); octstr_destroy(parmheader); return result; }
static void sql_list(Boxc *boxc) { Msg *msg; List *qlist, *save_list; qlist = gwlist_create(); gwlist_add_producer(qlist); save_list = gwlist_create(); gwlist_add_producer(save_list); while (sqlbox_status == SQL_RUNNING && boxc->alive) { if ( gw_sql_fetch_msg_list(qlist, limit_per_cycle) > 0 ) { while((gwlist_len(qlist)>0) && ((msg = gwlist_consume(qlist)) != NULL )) { if (charset_processing(msg) == -1) { error(0, "Could not charset process message, dropping it!"); msg_destroy(msg); continue; } if (global_sender != NULL && (msg->sms.sender == NULL || octstr_len(msg->sms.sender) == 0)) { msg->sms.sender = octstr_duplicate(global_sender); } /* convert validity and deferred to unix timestamp */ if (msg->sms.validity != SMS_PARAM_UNDEFINED) msg->sms.validity = time(NULL) + msg->sms.validity * 60; if (msg->sms.deferred != SMS_PARAM_UNDEFINED) msg->sms.deferred = time(NULL) + msg->sms.deferred * 60; send_msg(boxc->bearerbox_connection, boxc, msg); /* convert validity & deferred back to minutes */ if (save_mt && msg->sms.validity != SMS_PARAM_UNDEFINED) msg->sms.validity = (msg->sms.validity - time(NULL))/60; if (save_mt && msg->sms.deferred != SMS_PARAM_UNDEFINED) msg->sms.deferred = (msg->sms.deferred - time(NULL))/60; gwlist_produce(save_list, msg); } /* save_list also deletes and destroys messages */ gw_sql_save_list(save_list, octstr_imm("MT"), save_mt); } else { gwthread_sleep(SLEEP_BETWEEN_EMPTY_SELECTS); } } gwlist_remove_producer(qlist); gwlist_remove_producer(save_list); gwlist_destroy(qlist, msg_destroy_item); gwlist_destroy(save_list, msg_destroy_item); }
size_t octstr_replace_substr(octstr_t *dst, octstr_t *src, int pos, int n) { size_t len; len = octstr_len(src); if( pos >= len ) { octstr_str_replace(dst, NULL, 0); n = 0; } else { n = pos + n <= len ? n : len - pos; octstr_str_replace(dst, src->s+pos, n); } return n; }
static void remove_crs(Octstr *os) { long i; if (os == NULL) return; i = 0; while (i < octstr_len(os)) { if (octstr_get_char(os, i) == '\r') { octstr_delete(os, i, 1); --i; } ++i; } }
static void eat_checksum(Octstr *packet) { int len; int ch1, ch2, ch3; len = octstr_len(packet); if (len < 3) return; ch1 = octstr_get_char(packet, len - 3); ch2 = octstr_get_char(packet, len - 2); ch3 = octstr_get_char(packet, len - 1); if (isxdigit(ch3) && isxdigit(ch2) && ch1 == TAB) octstr_delete(packet, len - 3, 3); }
void smpp_listener_auth_failed(SMPPServer *smpp_server, Octstr *ip) { if (octstr_len(smpp_server->ip_blocklist_exempt_ips) && (octstr_search(smpp_server->ip_blocklist_exempt_ips, ip, 0) > -1)) { debug("smpp.listener.auth.failed", 0, "IP address %s is exempt from the IP block list", octstr_get_cstr(ip)); return; } gw_rwlock_wrlock(smpp_server->ip_blocklist_lock); SMPPBlockedIp *smpp_blocked_ip = dict_get(smpp_server->ip_blocklist, ip); if(smpp_blocked_ip == NULL) { smpp_blocked_ip = smpp_blocked_ip_create(); dict_put(smpp_server->ip_blocklist, ip, smpp_blocked_ip); } smpp_blocked_ip->attempts++; smpp_blocked_ip->time_blocked = time(NULL); debug("smpp.listener.auth.failed", 0, "IP address %s, attempts %ld have failed", octstr_get_cstr(ip), smpp_blocked_ip->attempts); gw_rwlock_unlock(smpp_server->ip_blocklist_lock); }
octstr_t * octstr_substr(octstr_t *s, int pos, int n) { octstr_t *substr; size_t len; len = octstr_len(s); if( pos >= len ) { substr = octstr_create(NULL, 0); } else { n = pos + n <= len ? n : len - pos; substr = octstr_create(s->s+pos, n); } return substr; }