static void make_url(Octstr **url) { if (use_config && !use_headers) { octstr_append(*url, octstr_imm("?username="******"default")); octstr_append(*url, octstr_imm("&password="******"default")); } }
/* * Add boundary value to the multipart header. */ static Octstr *make_multipart_value(const char *boundary) { Octstr *hos; hos = octstr_format("%s", "multipart/related; boundary="); octstr_append(hos, octstr_imm(boundary)); octstr_append(hos, octstr_imm("; type=\"application/xml\"")); return hos; }
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; }
int main() { octstr_t a, b, c; octstr_initv(&a, &b, &c, NULL); octstr_append(&a, "hello \n"); octstr_append(&b, "from the\n"); octstr_append(&c, "side"); octstr_append_octstr(&a, &b); octstr_append_octstr(&a, &c); printf("result string is = \n"); octstr_print(&a); printf("expected\n" "\\x68\\x65\\x6C\\x6F\\x20" "\\x0A\\x66\\x72\\x6F\\x6D" "\\x20\\x74\\x68\\x65\\x0A" "\\x73\\x69\\x64\\x65"); printf("====\n" "bitwise tests:\n"); octstr_reset(&a); octstr_reset(&b); octstr_append_n(&a, "\xA5\x5A", 2); octstr_append_n(&b, "\x5A\xA5", 2); octstr_or(&c, &a, &b); printf("octstr_or 0xA55A | 0x5AA5 = "); octstr_print(&c); octstr_and(&c, &a, &b); printf("0xA55A & 0x5AA5 = "); octstr_print(&c); octstr_xor(&c, &a, &b); printf("0xA55A ^ 0x5AA5 = "); octstr_print(&c); octstr_clearv(&a, &b, &c, NULL); return 0; }
/* * NB: This data includes bearer information. We use IPv4 values. Address Type * is defined in wsp, table 16, p. 65 */ static Octstr *pack_server_address(void) { Octstr *address, *ip_address; unsigned char address_len; long port; int bearer_type; bearer_type = GSM_CSD_IPV4; port = CONNECTED_PORT; mutex_lock(bearerbox->mutex); ip_address = octstr_duplicate(bearerbox->address); address_len = octstr_len(bearerbox->address); mutex_unlock(bearerbox->mutex); address = octstr_create(""); octstr_append_char(address, address_len); octstr_set_bits(address, 0, 1, 1); /* bearer type included */ octstr_set_bits(address, 1, 1, 1); /* port number included */ octstr_append_char(address, bearer_type); octstr_append_decimal(address, port); octstr_append(address, ip_address); octstr_destroy(ip_address); return address; }
/* * Read some headers, i.e., until the first empty line (read and discard * the empty line as well). Return -1 for error, 0 for all headers read. */ static int read_mime_headers(ParseContext *context, List *headers) { Octstr *line, *prev; if (gwlist_len(headers) == 0) prev = NULL; else prev = gwlist_get(headers, gwlist_len(headers) - 1); for (;;) { line = parse_get_line(context); if (line == NULL) { return -1; } if (octstr_len(line) == 0) { octstr_destroy(line); break; } if (isspace(octstr_get_char(line, 0)) && prev != NULL) { octstr_append(prev, line); octstr_destroy(line); } else { gwlist_append(headers, line); prev = line; } } return 0; }
static long pack_tpis(Octstr *data, long bitpos, List *tpis) { long length; WTP_TPI *tpi; int i; int num_tpis; num_tpis = gwlist_len(tpis); for (i = 0; i < num_tpis; i++) { tpi = gwlist_get(tpis, i); length = octstr_len(tpi->data); octstr_set_bits(data, bitpos, 1, i + 1 < num_tpis); octstr_set_bits(data, bitpos + 1, 4, tpi->type); if (length >= 4) { /* Long TPI */ octstr_set_bits(data, bitpos + 5, 1, 1); octstr_set_bits(data, bitpos + 8, 8, length); bitpos += 16; } else { /* Short TPI */ octstr_set_bits(data, bitpos + 5, 1, 0); octstr_set_bits(data, bitpos + 6, 2, length); bitpos += 8; } gw_assert(bitpos % 8 == 0); octstr_append(data, tpi->data); bitpos += 8 * length; } return bitpos; }
/* * Remove matching entry from the spool. */ static void dlr_spool_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst) { Octstr *os, *hash, *dir, *filename; /* determine target dir and filename via hash */ os = octstr_duplicate(smsc); octstr_append(os, ts); hash = our_hash_func(os); octstr_destroy(os); /* determine target dir */ dir = octstr_format("%S/%ld", spool_dir, octstr_hash_key(hash) % MAX_DIRS); /* get msg surrogate filename */ filename = get_msg_filename(dir, hash, dst); octstr_destroy(dir); octstr_destroy(hash); /* if there was no filename, then we didn't find it */ if (filename == NULL) { return; } /* remove the file from the file system */ if (unlink(octstr_get_cstr(filename)) == -1) { error(errno, "Could not unlink file `%s'.", octstr_get_cstr(filename)); octstr_destroy(filename); return; } counter_decrease(counter); octstr_destroy(filename); }
static Octstr *radius_type_convert(int type, Octstr *value) { Octstr *ret = NULL; int i; switch (type) { case t_int: ret = octstr_format("%ld", decode_integer(value, 0, 4)); break; case t_string: ret = octstr_format("%s", octstr_get_cstr(value)); break; case t_ipaddr: ret = octstr_create(""); for (i = 0; i < 4; i++) { int c = octstr_get_char(value, i); Octstr *b = octstr_format("%d", c); octstr_append(ret, b); if (i < 3) octstr_append_cstr(ret, "."); octstr_destroy(b); } break; default: panic(0, "RADIUS: Attribute type %d does not exist.", type); break; } return ret; }
/* P_hash as described in WAP WTLS section 11.3.2 */ Octstr *wtls_P_hash(Octstr * secret, Octstr * seed, int byteLength, WTLSMachine * wtls_machine) { Octstr *a; Octstr *aPrev; Octstr *aPlusSeed; Octstr *hashTemp; Octstr *hashedData; hashedData = octstr_create(""); /* start with A(1) = HMAC_hash(secret, seed) */ aPrev = octstr_duplicate(seed); do { /* A(i) */ a = wtls_hmac_hash(secret, aPrev, SHA_80); aPlusSeed = octstr_cat(a, seed); /* HMAC */ hashTemp = wtls_hmac_hash(secret, aPlusSeed, SHA_80); octstr_destroy(aPlusSeed); octstr_append(hashedData, hashTemp); octstr_destroy(hashTemp); /* Update a(i-1) */ octstr_destroy(aPrev); aPrev = a; } while (octstr_len(hashedData) < byteLength); octstr_destroy(aPrev); return (hashedData); }
/* * Will replace a binary data octet string (inplace) with a SMASI conform * ASCII representation of the data. */ static void encode_binary_data(Octstr *data) { Octstr *result = octstr_create(""); long pos = 0; while (pos < octstr_len(data)) { int encode = octstr_get_char(data, pos); int msb = (encode & 0xf0) >> 4; int lsb = (encode & 0x0f) >> 0; if (msb == 0) msb = '0'; else if (msb < 10) msb = '1' + msb - 1; else msb = 'a' + msb - 10; if (lsb == 0) lsb = '0'; else if (lsb < 10) lsb = '1' + lsb - 1; else lsb = 'a' + lsb - 10; octstr_append_char(result, ':'); octstr_append_char(result, msb); octstr_append_char(result, lsb); pos++; } /* Replace binary data octet string with ASCII representation. */ octstr_delete(data, 0, octstr_len(data)); octstr_append(data, result); octstr_destroy(result); }
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 void add_part_header(Octstr *content_keader, Octstr **wap_content) { if (use_content_header) { octstr_append(*wap_content, content_header); } add_delimiter(wap_content); }
int set_cookies(List *headers, WSPMachine *sm) { Cookie *value = NULL; Octstr *cookie = NULL; long pos = 0; if (headers == NULL || sm == NULL) { error (0, "set_cookies: Null argument(s) - no headers, WSPMachine or both"); return -1; } /* Expire cookies that have timed out */ expire_cookies(sm->cookies); /* Walk through the cookie cache, adding the cookie to the request headers */ if (gwlist_len(sm->cookies) > 0) { debug("wap.wsp.http", 0, "set_cookies: Cookies in cache"); for (pos = 0; pos < gwlist_len(sm->cookies); pos++) { value = gwlist_get(sm->cookies, pos); cookie = octstr_create("Cookie: "); if (value->version) octstr_append(cookie, value->version); octstr_append(cookie, value->name); octstr_append_char(cookie, '='); octstr_append(cookie, value->value); if (value->path) { octstr_append_char(cookie, ';'); octstr_append(cookie, value->path); } if (value->domain) { octstr_append_char(cookie, ';'); octstr_append(cookie, value->domain); } gwlist_append(headers, cookie); debug("wap.wsp.http", 0, "set_cookies: Added (%s)", octstr_get_cstr (cookie)); } } else debug("wap.wsp.http", 0, "set_cookies: No cookies in cache"); return 0; }
static Octstr *make_start_delimiter(Octstr *dash_boundary) { Octstr *start_delimiter; start_delimiter = octstr_create("--"); octstr_append(start_delimiter, dash_boundary); return start_delimiter; }
static Octstr *make_part_delimiter(Octstr *dash_boundary) { Octstr *part_delimiter; part_delimiter = octstr_create("\r\n--"); octstr_append(part_delimiter, dash_boundary); return part_delimiter; }
/* * Find matching entry in our spool and return the dlr_entry. */ static struct dlr_entry *dlr_spool_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst) { struct dlr_entry *ret = NULL; Octstr *os, *hash, *dir, *filename = NULL; Msg *msg; /* determine target dir and filename via hash */ os = octstr_duplicate(smsc); octstr_append(os, ts); hash = our_hash_func(os); octstr_destroy(os); /* determine target dir */ dir = octstr_format("%S/%ld", spool_dir, octstr_hash_key(hash) % MAX_DIRS); /* get content of msg surrogate */ os = get_msg_surrogate(dir, hash, dst, &filename); octstr_destroy(dir); octstr_destroy(hash); /* if there was no content */ if (os == NULL) { octstr_destroy(filename); return NULL; } /* unpack */ if ((msg = store_msg_unpack(os)) == NULL) { octstr_destroy(os); error(0, "Could not unpack DLR message `%s'", octstr_get_cstr(filename)); octstr_destroy(filename); return ret; } octstr_destroy(os); octstr_destroy(filename); #define MAP(to, from) \ to = from; \ from = NULL; /* map values to a struct dlr_entry */ ret = dlr_entry_create(); MAP(ret->smsc, msg->sms.smsc_id); MAP(ret->timestamp, msg->sms.foreign_id); MAP(ret->source, msg->sms.sender); MAP(ret->destination, msg->sms.receiver); MAP(ret->service, msg->sms.service); MAP(ret->url, msg->sms.dlr_url); MAP(ret->boxc_id, msg->sms.boxc_id); ret->mask = msg->sms.dlr_mask; msg_destroy(msg); return ret; }
static void start_request(HTTPCaller *caller, List *reqh, long i) { Octstr *url, *content = NULL; long *id; if ((i % 1000) == 0) info(0, "Starting fetch %ld", i); id = gw_malloc(sizeof(long)); *id = i; url = octstr_create(urls[i % num_urls]); if (file) { octstr_append(url, octstr_imm("&text=")); octstr_append(url, msg_text); } /* add the extra headers that have been read from the file */ if (split != NULL) http_header_combine(reqh, split); /* * if a body content file has been specified, then * we assume this should be a POST */ if (content_file != NULL) { content = post_content_create(); method = HTTP_METHOD_POST; } /* * if this is a POST request then pass the required content as body to * the HTTP server, otherwise skip the body, the arguments will be * urlencoded in the URL itself. */ http_start_request(caller, method, url, reqh, content, follow_redirect, id, ssl_client_certkey_file); debug("", 0, "Started request %ld with url:", *id); octstr_url_decode(url); octstr_dump(url, 0); octstr_destroy(url); octstr_destroy(msg_text); octstr_destroy(content); }
int radius_authenticate_pdu(RADIUS_PDU *pdu, Octstr **data, Octstr *secret) { int rc = 0; Octstr *stream; Octstr *attributes; Octstr *digest; stream = attributes = digest = NULL; /* first extract attributes from raw data, where * the first 20 octets are code, idendifier, length * and authenticator value as described in RFC2866, sec. 3 */ if (octstr_len(*data) > 20) attributes = octstr_copy(*data, 20, octstr_len(*data)-20); switch (pdu->type) { case 0x04: /* Accounting-Request, see RFC2866, page 6 */ stream = octstr_copy(*data, 0, 4); octstr_append_data(stream, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16); octstr_append(stream, attributes); octstr_append(stream, secret); digest = md5(stream); rc = octstr_compare(pdu->u.Accounting_Request.authenticator, digest) == 0 ? 1 : 0; break; case 0x05: /* Accounting-Response, create Response authenticator */ stream = octstr_duplicate(*data); octstr_append(stream, secret); digest = md5(stream); octstr_delete(*data, 4, 16); octstr_insert(*data, digest, 4); break; default: break; } octstr_destroy(attributes); octstr_destroy(stream); octstr_destroy(digest); return rc; }
static int sms_to_client(Connection *client, Msg *msg) { Octstr *line; Octstr *msgdata = NULL; /* NULL to allow octstr_destroy */ char *contents; int len; 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_char(line, ' '); msgdata = octstr_duplicate(msg->sms.msgdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); } else { contents = octstr_get_cstr(msg->sms.msgdata); len = octstr_len(msg->sms.msgdata); while (len > 0) { len--; if (contents[len] < 32 || contents[len] > 126) { octstr_append(line, octstr_imm(" data ")); msgdata = octstr_duplicate(msg->sms.msgdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); goto notelse; /* C lacks "else" clause for while loops */ } } octstr_append(line, octstr_imm(" text ")); octstr_append(line, msg->sms.msgdata); } notelse: 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; }
int conn_write(Connection *conn, Octstr *data) { int ret; lock_out(conn); octstr_append(conn->outbuf, data); ret = unlocked_try_write(conn); unlock_out(conn); return ret; }
void octstr_delete(octstr_t *s, int pos, int n) { octstr_t *substr1, *substr2; substr1 = octstr_substr(s, 0, pos); substr2 = octstr_substr(s, pos + n, octstr_len(s) - (pos + n)); octstr_replace(s, substr1); octstr_append(s, substr2); octstr_destroy(substr1); octstr_destroy(substr2); }
static Octstr *make_part_delimiter(Octstr *boundary) { Octstr *part_delimiter; part_delimiter = octstr_create(""); add_delimiter(&part_delimiter); octstr_format_append(part_delimiter, "%s", "--"); octstr_append(part_delimiter, boundary); add_delimiter(&part_delimiter); return part_delimiter; }
static Octstr *cgwop_tostr(struct cgwop *cgwop) { int len = cgwop->num_fields; Octstr *str; if (cgw_ops[cgwop->op] == NULL) return NULL; /* invalid operation */ str = octstr_create(""); octstr_append(str, octstr_imm("op:")); octstr_append(str, octstr_imm(cgw_ops[cgwop->op])); octstr_append_char(str, CGW_EOL); while (--len >= 0) { octstr_append(str, cgwop->name[len]); octstr_append_char(str, ':'); octstr_append(str, cgwop->value[len]); octstr_append_char(str, CGW_EOL); } octstr_append(str, octstr_imm("end:")); octstr_append(str, octstr_imm(cgw_ops[cgwop->op])); octstr_append_char(str, CGW_EOL); return str; }
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; }
static Octstr *make_close_delimiter(Octstr *boundary) { Octstr *close_delimiter; close_delimiter = octstr_create(""); add_delimiter(&close_delimiter); octstr_format_append(close_delimiter, "%s", "--"); octstr_append(close_delimiter, boundary); octstr_format_append(close_delimiter, "%s", "--"); /*add_delimiter(&close_delimiter);*/ return close_delimiter; }
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; }
/* * Return the character sets supported by the WML compiler, as a List * of Octstrs, where each string is the MIME identifier for one charset. */ List *wml_charsets(void) { int i; List *result; Octstr *charset; result = gwlist_create(); for (i = 0; character_sets[i].charset != NULL; i++) { charset = octstr_create(character_sets[i].charset); octstr_append_char(charset, '-'); octstr_append(charset, octstr_imm(character_sets[i].nro)); gwlist_append(result, charset); } return result; }
static struct cgwop *msg_to_cgwop(PrivData *privdata, Msg *msg, int trn) { struct cgwop *cgwop; Octstr *sender, *udh, *dta; cgwop = cgwop_create(CGW_OP_MSG, trn); if (cgwop == NULL) return NULL; if (!octstr_check_range(msg->sms.sender, 0, octstr_len(msg->sms.sender), gw_isdigit)) { /* If alphanumeric, simply prefix sender with '$' char */ sender = octstr_create("$"); octstr_append(sender, msg->sms.sender); } else sender = octstr_duplicate(msg->sms.sender); cgwop_add(cgwop, octstr_imm("app"), privdata->appname); cgwop_add(cgwop, octstr_imm("from"), sender); cgwop_add(cgwop, octstr_imm("to"), msg->sms.receiver); /* If delivery reports are asked, ask for them by adding a nrq:anything field */ if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask)) cgwop_add(cgwop, octstr_imm("nrq"), octstr_imm("true")); octstr_destroy(sender); if (octstr_len(msg->sms.udhdata)) { udh = octstr_duplicate(msg->sms.udhdata); octstr_binary_to_hex(udh, 1); cgwop_add(cgwop, octstr_imm("udh"), udh); octstr_destroy(udh); dta = octstr_duplicate(msg->sms.msgdata); octstr_binary_to_hex(dta, 1); cgwop_add(cgwop, octstr_imm("msg"), dta); cgwop_add(cgwop, octstr_imm("type"), octstr_imm("bin")); octstr_destroy(dta); } else { cgwop_add(cgwop, octstr_imm("msg"), cgw_encode_msg(msg->sms.msgdata)); } return cgwop; }
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; }