int main(int argc, char **argv) { Octstr *os1, *os2; gwlib_init(); os1 = octstr_create(""); octstr_append_from_hex(os1, "411810124550421715161a"); os2 = octstr_duplicate(os1); debug("", 0, "Orginal GSM charset data:"); octstr_dump(os1, 0); charset_gsm_to_utf8(os1); debug("", 0, "Same data mapped to URT-8 charset:"); octstr_dump(os1, 0); charset_utf8_to_gsm(os1); debug("", 0, "Same data mapped back again to GSM charset:"); octstr_dump(os1, 0); if (octstr_compare(os1, os2) != 0) panic(0, "Data is not the same after re-mapping!"); else debug("",0,"Data is same, ok."); octstr_destroy(os1); octstr_destroy(os2); gwlib_shutdown(); return 0; }
int main(void) { Octstr *os, *os2, *os3, *os4; gwlib_init(); os = octstr_format("hi, %% %-5.*s, <%*s>, %-5d + %05d = %d, -%5.2f", 3, "world", 3, "", 1, 2, 3, 3.1415927); octstr_dump(os, 0); os2 = octstr_format("<%S>", os); octstr_dump(os2, 0); octstr_format_append(os2, "yeehaa!"); octstr_dump(os2, 0); os3 = octstr_format("NULL=%p &os=%p", (void *) NULL, (void *) &os); octstr_dump(os3, 0); os4 = octstr_format("Encode %E and limited %-10.10E", os, os); octstr_dump(os4, 0); octstr_destroy(os); octstr_destroy(os2); octstr_destroy(os3); gwlib_shutdown(); return 0; }
int main(int argc, char **argv) { Octstr *data, *filename, *mac, *key; unsigned char macbuf[EVP_MAX_MD_SIZE], *p; int mac_len; #ifdef HAVE_LIBSSL HMAC_CTX ctx; #endif gwlib_init(); get_and_set_debugs(argc, argv, NULL); if (argc < 3) panic(0, "Syntax: %s <key> <file>\n", argv[0]); key = octstr_create(argv[1]); filename = octstr_create(argv[2]); data = octstr_read_file(octstr_get_cstr(filename)); if (data == NULL) panic(0, "Cannot read file."); debug("",0,"Dumping file `%s':", octstr_get_cstr(filename)); octstr_dump(data, 0); #ifdef HAVE_LIBSSL HMAC_Init(&ctx, octstr_get_cstr(key), octstr_len(key), EVP_sha1()); p = HMAC(EVP_sha1(), octstr_get_cstr(key), octstr_len(key), octstr_get_cstr(data), octstr_len(data), macbuf, &mac_len); HMAC_cleanup(&ctx); #else macbuf[0] = 0; mac_len = 0; p = macbuf; warning(0, "No SSL support. Can't calculate HMAC value."); #endif mac = octstr_create_from_data(p, mac_len); octstr_binary_to_hex(mac, 0); debug("",0,"HMAC of file `%s' and key `%s' is:", octstr_get_cstr(filename), octstr_get_cstr(key)); octstr_dump(mac, 0); octstr_destroy(data); octstr_destroy(mac); octstr_destroy(key); gwlib_shutdown(); return 0; }
void wsp_cap_dump(Capability *cap) { debug("wsp", 0, "Dumping capability at %p:", cap); if (cap) { debug("wsp", 0, " id = %d", cap->id); debug("wsp", 0, " name:"); octstr_dump(cap->name, 1); debug("wsp", 0, " data:"); octstr_dump(cap->data, 1); if (cap->data == NULL) debug("wsp", 0, " accept: %d", cap->accept); } debug("wsp", 0, "Capability dump ends."); }
Octstr *mime_entity_body(MIMEEntity *m) { Octstr *os, *body; ParseContext *context; MIMEEntity *e; gw_assert(m != NULL && m->headers != NULL); /* For non-multipart, return body directly. */ if (mime_entity_num_parts(m) == 0) return octstr_duplicate(m->body); os = mime_entity_to_octstr(m); context = parse_context_create(os); e = mime_entity_create(); /* parse the headers up to the body */ if ((read_mime_headers(context, e->headers) != 0) || e->headers == NULL) { debug("mime.parse",0,"Failed to read MIME headers in Octstr block:"); octstr_dump(os, 0); mime_entity_destroy(e); parse_context_destroy(context); return NULL; } /* the rest is the body */ body = parse_get_rest(context); octstr_destroy(os); mime_entity_destroy(e); parse_context_destroy(context); return body; }
static void start_push(HTTPCaller *caller, long i) { List *push_headers; Octstr *push_content; long *id; push_content = push_content_create(); push_headers = push_headers_create(octstr_len(push_content)); if (verbose) { debug("test.ppg", 0, "we have push content"); octstr_dump(push_content, 0); debug("test.ppg", 0, "and headers"); http_header_dump(push_headers); } id = gw_malloc(sizeof(long)); *id = i; make_url(&push_url); debug("test.ppg", 0, "TEST_PPG: starting to push job %ld", i); http_start_request(caller, HTTP_METHOD_POST, push_url, push_headers, push_content, 0, id, ssl_client_certkey_file); debug("test.ppg", 0, "push done"); octstr_destroy(push_content); http_destroy_headers(push_headers); }
static void receive_smpp_thread(void *arg) { ESME *esme; Octstr *os; long len; long sender_id; SMPP_PDU *pdu; esme = arg; sender_id = -1; len = 0; while (!quitting && conn_wait(esme->conn, -1.0) != -1) { for (;;) { if (len == 0) { len = smpp_pdu_read_len(esme->conn); if (len == -1) { error(0, "Client sent garbage, closing connection."); goto error; } else if (len == 0) { if (conn_eof(esme->conn) || conn_error(esme->conn)) goto error; break; } } gw_assert(len > 0); os = smpp_pdu_read_data(esme->conn, len); if (os != NULL) { len = 0; pdu = smpp_pdu_unpack(NULL, os); if (pdu == NULL) { error(0, "PDU unpacking failed!"); octstr_dump(os, 0); } else { handle_pdu(esme, pdu); smpp_pdu_destroy(pdu); } octstr_destroy(os); } else if (conn_eof(esme->conn) || conn_error(esme->conn)) goto error; else break; } if (!quitting && esme->receiver && sender_id == -1) sender_id = gwthread_create(send_smpp_thread, esme); } error: if (sender_id != -1) { quit(); gwthread_join(sender_id); } esme_destroy(esme); quit(); debug("test.smpp", 0, "%s terminates.", __func__); }
static int receive_reply(HTTPCaller *caller) { void *id; int ret; Octstr *final_url; List *replyh; Octstr *replyb; Octstr *type; Octstr *charset; Octstr *os; id = http_receive_result(caller, &ret, &final_url, &replyh, &replyb); octstr_destroy(final_url); if (id == NULL || ret == -1) { error(0, "http GET failed"); gw_free(id); return -1; } debug("", 0, "Done with request %ld", *(long *) id); gw_free(id); http_header_get_content_type(replyh, &type, &charset); debug("", 0, "Content-type is <%s>, charset is <%s>", octstr_get_cstr(type), octstr_get_cstr(charset)); octstr_destroy(type); octstr_destroy(charset); if (verbose) debug("", 0, "Reply headers:"); while ((os = gwlist_extract_first(replyh)) != NULL) { if (verbose) octstr_dump(os, 1); octstr_destroy(os); } gwlist_destroy(replyh, NULL); if (verbose) { debug("", 0, "Reply body:"); octstr_dump(replyb, 1); } octstr_destroy(replyb); return 0; }
int main(int argc, char **argv) { Octstr *data, *enc; gwlib_init(); get_and_set_debugs(argc, argv, NULL); if (argc < 2) panic(0, "Syntax: %s <txt>\n", argv[0]); data = octstr_create(argv[1]); enc = md5(data); debug("",0,"MD5:"); octstr_dump(enc, 0); octstr_destroy(enc); enc = md5digest(data); debug("",0,"MD5 (digest):"); octstr_dump(enc, 0); #ifdef HAVE_LIBSSL OpenSSL_add_all_digests(); octstr_destroy(enc); enc = our_hash_func(data); debug("",0,"SHA1:"); octstr_dump(enc, 0); octstr_binary_to_hex(enc, 0); debug("",0,"SHA1 (digest):"); octstr_dump(enc, 0); #endif octstr_destroy(data); octstr_destroy(enc); gwlib_shutdown(); return 0; }
static Octstr *post_content_create(void) { Octstr *content; if ((content = octstr_read_file(octstr_get_cstr(content_file))) == NULL) panic(0, "Cannot read content text"); debug("", 0, "body content is"); octstr_dump(content, 0); return content; }
int ota_pack_message(Msg **msg, Octstr *ota_doc, Octstr *doc_type, Octstr *from, Octstr *phone_number, Octstr *sec, Octstr *pin) { Octstr *ota_binary; *msg = msg_create(sms); (*msg)->sms.sms_type = mt_push; ota_pack_udh(msg, doc_type); if (ota_compile(ota_doc, octstr_imm("UTF-8"), &ota_binary) == -1) goto cerror; if (!ota_pack_push_headers(msg, doc_type, sec, pin, ota_binary)) goto herror; octstr_format_append((*msg)->sms.msgdata, "%S", ota_binary); (*msg)->sms.sender = octstr_duplicate(from); (*msg)->sms.receiver = octstr_duplicate(phone_number); (*msg)->sms.coding = DC_8BIT; (*msg)->sms.time = time(NULL); octstr_dump((*msg)->sms.msgdata, 0); info(0, "/cgi-bin/sendota: XML request for target <%s>", octstr_get_cstr(phone_number)); octstr_destroy(ota_binary); octstr_destroy(ota_doc); octstr_destroy(doc_type); octstr_destroy(from); octstr_destroy(sec); octstr_destroy(pin); return 0; herror: octstr_destroy(ota_binary); octstr_destroy(ota_doc); octstr_destroy(doc_type); octstr_destroy(from); octstr_destroy(sec); octstr_destroy(pin); return -2; cerror: octstr_destroy(ota_doc); octstr_destroy(doc_type); octstr_destroy(from); octstr_destroy(sec); octstr_destroy(pin); 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 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); } }
int main(int argc, char **argv) { Octstr *data, *filename, *hex; gwlib_init(); get_and_set_debugs(argc, argv, NULL); if (argc < 2) panic(0, "Syntax: %s <file>\n", argv[0]); filename = octstr_create(argv[1]); data = octstr_read_file(octstr_get_cstr(filename)); if (data == NULL) panic(0, "Cannot read file."); /* * We test if this is a text/plain file with hex values in it. * Therefore copy the data and trail off any CR and LF from * beginning and end and test if the result is only hex chars. * If yes, then convert to binary before dumping. */ hex = octstr_duplicate(data); octstr_strip_crlfs(hex); if (octstr_is_all_hex(hex)) { debug("",0,"Trying to converting from hex to binary."); if (octstr_hex_to_binary(hex) == 0) { FILE *f = fopen(argv[2], "w"); debug("",0,"Convertion was successfull. Writing binary content to file `%s'", argv[2]); octstr_destroy(data); data = octstr_duplicate(hex); octstr_print(f, data); fclose(f); } else { debug("",0,"Failed to convert from hex?!"); } } debug("",0,"Dumping file `%s':", octstr_get_cstr(filename)); octstr_dump(data, 0); octstr_destroy(data); octstr_destroy(hex); gwlib_shutdown(); return 0; }
/* Copied from Kannel smsc_smpp.c */ static int smpp_listener_read_pdu(SMPPEsme *smpp_esme, long *len, SMPP_PDU **pdu) { Connection *conn = smpp_esme->conn; if(conn == NULL) { return -1; } Octstr *os; if (*len == 0) { *len = smpp_pdu_read_len(conn); if (*len == -1) { error(0, "SMPP[%s:%ld]: Client sent garbage, ignored.", octstr_get_cstr(smpp_esme->system_id), smpp_esme->id); *len = 0; return -2; } else if (*len == 0) { if (conn_eof(conn) || conn_error(conn)) return -1; return 0; } } os = smpp_pdu_read_data(conn, *len); if (os == NULL) { if (conn_eof(conn) || conn_error(conn)) return -1; return 0; } *len = 0; *pdu = smpp_pdu_unpack(smpp_esme->system_id, os); if (*pdu == NULL) { error(0, "SMPP[%s]: PDU unpacking failed.", octstr_get_cstr(smpp_esme->system_id)); debug("smpp.listener.read.pdu", 0, "SMPP[%s]: Failed PDU follows.", octstr_get_cstr(smpp_esme->system_id)); octstr_dump(os, 0); octstr_destroy(os); return -2; } octstr_destroy(os); return 1; }
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); }
/* * Find the first packet in "in", delete it from "in", and return it as * a struct. Return NULL if "in" contains no packet. Always delete * leading non-packet data from "in". */ static struct packet *packet_extract(Octstr *in, SMSCConn *conn) { Octstr *packet; int size, i; static char s[4][4] = { { 0x01, 0x0b, 0x00, 0x00 }, { 0x01, 0x00, 0x00, 0x00 }, { 0x00, 0x04, 0x00, 0x00 }, { 0x00, 0x09, 0x00, 0x00 } }; /* msgtype, oper, 0, 0 */ char known_bytes[4]; if (octstr_len(in) < 10) return NULL; octstr_get_many_chars(known_bytes, in, 4, 4); /* Find s, and delete everything up to it. */ /* If packet starts with one of s, it should be good packet */ for (i = 0; i < 4; i++) { if (memcmp(s[i], known_bytes, 4) == 0) break; } if (i >= 4) { error(0, "OISD[%s]: wrong packet", octstr_get_cstr(conn->id)); octstr_dump(in, 0); return NULL; } /* Find end of packet */ size = (octstr_get_char(in, 9) << 8) | octstr_get_char(in, 8); if (size + 10 > octstr_len(in)) return NULL; packet = octstr_copy(in, 0, size + 10); octstr_delete(in, 0, size + 10); return packet_parse(packet); }
int main(int argc, char **argv) { Octstr *os; gwlib_init(); if (optind >= argc) { os = octstr_imm("foo"); } else { os = octstr_imm(argv[optind]); } /* * Note: don't destroy this, check that the log file has no * memory leaks. */ octstr_dump(os, 0); gwlib_shutdown(); return 0; }
int main(void) { Octstr *os, *os2, *os3, *os4, *os5; unsigned long long n = 18446744073709551615ull; gwlib_init(); os = octstr_format("hi, %% %-5.*s, <%*s>, %-5d + %05d = %d, -%5.2f", 3, "world", 3, "", 1, 2, 3, 3.1415927); octstr_dump(os, 0); os2 = octstr_format("<%S>", os); octstr_dump(os2, 0); octstr_format_append(os2, "yeehaa!"); octstr_dump(os2, 0); os3 = octstr_format("NULL=%p &os=%p", (void *) NULL, (void *) &os); octstr_dump(os3, 0); os4 = octstr_format("Encode %E and limited %-10.10E", os, os); octstr_dump(os4, 0); os5 = octstr_format("Encode %%llu: %llu", n); octstr_dump(os5, 0); octstr_destroy(os); octstr_destroy(os2); octstr_destroy(os3); octstr_destroy(os4); octstr_destroy(os5); gwlib_shutdown(); return 0; }
int main(int argc, char **argv) { int opt, file, have_charset, ret; FILE *fp; Octstr *output, *si_doc, *si_binary; gwlib_init(); file = 0; have_charset = 0; fp = NULL; while ((opt = getopt(argc, argv, "hf:c:v:")) != EOF) { switch (opt) { case 'h': help(); exit(1); break; case 'f': file = 1; file_name = octstr_create(optarg); fp = fopen(optarg, "a"); if (fp == NULL) panic(0, "Cannot open output file"); break; case 'c': have_charset = 1; charset = octstr_create(optarg); break; case 'v': log_set_output_level(atoi(optarg)); break; case '?': default: error(0, "Invalid option %c", opt); help(); panic(0, "Stopping"); break; } } if (optind >= argc) { error(0, "Missing arguments"); help(); panic(0, "Stopping"); } si_doc = octstr_read_file(argv[optind]); if (si_doc == NULL) panic(0, "Cannot read the si document"); if (!have_charset) charset = NULL; ret = si_compile(si_doc, charset, &si_binary); output = octstr_format("%s", "si compiler returned %d\n", ret); if (ret == 0) { if (fp == NULL) fp = stdout; octstr_append(output, octstr_imm("content being\n")); octstr_append(output, si_binary); } if (file) octstr_pretty_print(fp, output); else { debug("test.si", 0, "si binary was"); octstr_dump(si_binary, 0); } if (have_charset) octstr_destroy(charset); if (file) { fclose(fp); octstr_destroy(file_name); } octstr_destroy(si_doc); octstr_destroy(si_binary); octstr_destroy(output); gwlib_shutdown(); return 0; }
int main(int argc, char **argv) { output_t outputti = NORMAL_OUT; FILE *fp = NULL; Octstr *output = NULL; Octstr *filename = NULL; Octstr *wml_text = NULL; Octstr *charset = NULL; Octstr *wml_binary = NULL; int i, ret = 0, opt, file = 0, zero = 0, numstatus = 0, wml_strict = 1; long num = 0; /* You can give an wml text file as an argument './wml_tester main.wml' */ gwlib_init(); while ((opt = getopt(argc, argv, "hsbzrn:f:c:")) != EOF) { switch (opt) { case 'h': help(); exit(0); case 's': if (outputti == NORMAL_OUT) outputti = SOURCE_OUT; else { help(); exit(0); } break; case 'b': if (outputti == NORMAL_OUT) outputti = BINARY_OUT; else { help(); exit(0); } break; case 'z': zero = 1; break; case 'r': wml_strict = 0; break; case 'n': numstatus = octstr_parse_long(&num, octstr_imm(optarg), 0, 0); if (numstatus == -1) { /* Error in the octstr_parse_long */ error(num, "Error in the handling of argument to option n"); help(); panic(0, "Stopping."); } break; case 'f': file = 1; filename = octstr_create(optarg); fp = fopen(optarg, "a"); if (fp == NULL) panic(0, "Couldn't open output file."); break; case 'c': charset = octstr_create(optarg); break; case '?': default: error(0, "Invalid option %c", opt); help(); panic(0, "Stopping."); } } if (optind >= argc) { error(0, "Missing arguments."); help(); panic(0, "Stopping."); } if (outputti == BINARY_OUT) log_set_output_level(GW_PANIC); wml_init(wml_strict); while (optind < argc) { wml_text = octstr_read_file(argv[optind]); if (wml_text == NULL) panic(0, "Couldn't read WML source file."); if (zero) set_zero(wml_text); for (i = 0; i <= num; i++) { ret = wml_compile(wml_text, charset, &wml_binary, NULL); if (i < num) octstr_destroy(wml_binary); } optind++; output = octstr_format("wml_compile returned: %d\n\n", ret); if (ret == 0) { if (fp == NULL) fp = stdout; if (outputti != BINARY_OUT) { if (outputti == SOURCE_OUT) { octstr_insert(output, wml_text, octstr_len(output)); octstr_append_char(output, '\n'); } octstr_append(output, octstr_imm( "Here's the binary output: \n\n")); octstr_print(fp, output); } if (file && outputti != BINARY_OUT) { fclose(fp); log_open(octstr_get_cstr(filename), 0, GW_NON_EXCL); octstr_dump(wml_binary, 0); log_close_all(); fp = fopen(octstr_get_cstr(filename), "a"); } else if (outputti != BINARY_OUT) octstr_dump(wml_binary, 0); else octstr_print(fp, wml_binary); if (outputti != BINARY_OUT) { octstr_destroy(output); output = octstr_format("\n And as a text: \n\n"); octstr_print(fp, output); octstr_pretty_print(fp, wml_binary); octstr_destroy(output); output = octstr_format("\n\n"); octstr_print(fp, output); } } octstr_destroy(wml_text); octstr_destroy(output); octstr_destroy(wml_binary); } if (file) { fclose(fp); octstr_destroy(filename); } if (charset != NULL) octstr_destroy(charset); wml_shutdown(); gwlib_shutdown(); return ret; }
int main(int argc, char **argv) { Octstr *filename = NULL; unsigned long num = 1, j; int opt; Octstr *mime, *mime2; MIMEEntity *m; gwlib_init(); while ((opt = getopt(argc, argv, "hv:n:")) != EOF) { switch (opt) { case 'v': log_set_output_level(atoi(optarg)); break; case 'n': num = atoi(optarg); break; case '?': default: error(0, "Invalid option %c", opt); help(); panic(0, "Stopping."); } } if (optind == argc) { help(); exit(0); } filename = octstr_create(argv[argc-1]); mime = octstr_read_file(octstr_get_cstr(filename)); for (j = 1; j <= num; j++) { info(0,"MIME Octstr from file `%s':", octstr_get_cstr(filename)); octstr_dump(mime, 0); m = mime_octstr_to_entity(mime); mime_entity_dump(m); mime2 = mime_entity_to_octstr(m); info(0, "MIME Octstr after reconstruction:"); octstr_dump(mime2, 0); if (octstr_compare(mime, mime2) != 0) { error(0, "MIME content from file `%s' and reconstruction differs!", octstr_get_cstr(filename)); } else { info(0, "MIME Octstr compare result has been successfull."); } octstr_destroy(mime2); mime_entity_destroy(m); } /* num times */ octstr_destroy(filename); gwlib_shutdown(); return 0; }
int main(int argc, char **argv) { Octstr *mime_content, *pap_content, *push_data, *rdf_content, *boundary, *push_content_file = NULL, *this_header, *pap_osname, *data_osname; List *content_headers, *source_parts; char *pap_content_file, *push_data_file, *rdf_content_file; int ret, std_out, opt, d_file, c_file; FILE *fp1, *fp2, *fp3; gwlib_init(); std_out = 0; d_file = 0; c_file = 0; data_osname = NULL; pap_osname = NULL; while ((opt = getopt(argc, argv, "hd:sc:")) != EOF) { switch(opt) { case 'h': help(); exit(1); break; case 'd': d_file = 1; data_osname = octstr_create(optarg); break; case 'c': c_file = 1; pap_osname = octstr_create(optarg); break; case 's': std_out = 1; break; case '?': default: error(0, "Invalid option %c", opt); help(); panic(0, "Stopping"); break; } } if (optind >= argc) { help(); panic(0, "missing arguments, stopping"); } if (!c_file) pap_content_file = "test/pap.txt"; else pap_content_file = octstr_get_cstr(pap_osname); if (!d_file) push_data_file = "test/data.txt"; else push_data_file = octstr_get_cstr(data_osname); rdf_content_file = "test/rdf.txt"; mime_content = octstr_read_file(argv[optind]); if (mime_content == NULL) { octstr_destroy(mime_content); error(0, "No MIME source"); panic(0, "Stopping"); } source_parts = octstr_split(mime_content, octstr_imm("content=")); if (gwlist_len(source_parts) == 1) { /* a hack to circumvent a bug */ error(0, "Badly formatted source:"); octstr_destroy(mime_content); gwlist_destroy(source_parts, octstr_destroy_item); panic(0, "Stopping"); } boundary = gwlist_extract_first(source_parts); octstr_delete(boundary, 0, octstr_len(octstr_imm("boundary="))); if (skip_tail(&boundary, ';') == 0) { error(0, "Cannot determine boundary, no delimiter; possible"); octstr_dump(boundary, 0); goto no_parse; } octstr_destroy(mime_content); mime_content = gwlist_extract_first(source_parts); if (skip_tail(&mime_content, ';') == 0){ error(0, "Cannot determine mime content, no delimiter"); octstr_dump(mime_content, 0); goto no_parse; } prepend_crlf(&mime_content); add_crs(mime_content); append_crlf(mime_content); ret = mime_parse(boundary, mime_content, &pap_content, &push_data, &content_headers, &rdf_content); if (ret == 0) { error(0, "Mime_parse returned 0, cannot continue"); goto error; } remove_crs(pap_content); if (!std_out) { fp1 = fopen(pap_content_file, "a"); if (fp1 == NULL) { error(0, "Cannot open the file for pap control message"); goto error; } octstr_print(fp1, pap_content); debug("test.mime", 0, "pap control message appended to the file"); fclose(fp1); } else { debug("test.mime", 0, "pap control message was"); octstr_dump(pap_content, 0); } remove_crs(push_data); if (!std_out) { fp2 = fopen(push_data_file, "a"); if (fp2 == NULL) { error(0, "Cannot open the push data file"); goto error; } push_content_file = octstr_create(""); octstr_append(push_content_file, octstr_imm("headers=")); while (gwlist_len(content_headers) > 0) { octstr_append(push_content_file, this_header = gwlist_extract_first(content_headers)); octstr_format_append(push_content_file, "%c", ' '); octstr_destroy(this_header); } octstr_append(push_content_file, octstr_imm(";\n")); octstr_append(push_content_file, octstr_imm("content=")); octstr_append(push_content_file, push_data); octstr_append(push_content_file, octstr_imm(";\n")); octstr_print(fp2, push_content_file); debug("test.mime", 0, "push content appended to the file"); fclose(fp2); } else { debug("test.mime", 0, "Content headers were"); http_header_dump(content_headers); debug("test.mime", 0, "And push content itself"); octstr_dump(push_data, 0); } if (rdf_content != NULL) remove_crs(rdf_content); if (!std_out && rdf_content != NULL) { fp3 = NULL; if (rdf_content != NULL) { fp3 = fopen(rdf_content_file, "a"); if (fp3 == NULL) { error(0, "Cannot open the rdf file"); goto cerror; } octstr_print(fp3, rdf_content); debug("test.mime", 0, "push caps message appended to the file"); fclose(fp3); } } else { if (rdf_content != NULL) { debug("test.mime", 0, "push caps message was"); octstr_dump(rdf_content, 0); } } octstr_destroy(boundary); octstr_destroy(mime_content); octstr_destroy(pap_content); octstr_destroy(push_data); octstr_destroy(rdf_content); octstr_destroy(pap_osname); octstr_destroy(data_osname); http_destroy_headers(content_headers); gwlist_destroy(source_parts, octstr_destroy_item); octstr_destroy(push_content_file); gwlib_shutdown(); info(0, "MIME data parsed successfully"); return 0; no_parse: octstr_destroy(mime_content); octstr_destroy(pap_osname); octstr_destroy(data_osname); gwlist_destroy(source_parts, octstr_destroy_item); octstr_destroy(boundary); gwlib_shutdown(); panic(0, "Stopping"); error: octstr_destroy(mime_content); gwlist_destroy(source_parts, octstr_destroy_item); octstr_destroy(boundary); octstr_destroy(pap_content); octstr_destroy(push_data); octstr_destroy(pap_osname); octstr_destroy(data_osname); http_destroy_headers(content_headers); octstr_destroy(rdf_content); gwlib_shutdown(); panic(0, "Stopping"); cerror: octstr_destroy(mime_content); gwlist_destroy(source_parts, octstr_destroy_item); octstr_destroy(boundary); octstr_destroy(pap_content); octstr_destroy(push_data); octstr_destroy(push_content_file); octstr_destroy(pap_osname); octstr_destroy(data_osname); http_destroy_headers(content_headers); octstr_destroy(rdf_content); gwlib_shutdown(); panic(0, "Stopping"); /* return after panic always required by gcc */ return 1; }
int main(int argc, char **argv) { int i, opt, use_threads; struct sigaction act; char *filename; Octstr *log_filename; Octstr *file_contents; #ifdef HAVE_LIBSSL Octstr *ssl_server_cert_file = NULL; Octstr *ssl_server_key_file = NULL; #endif char *whitelist_name; char *blacklist_name; int white_asked, black_asked; long threads[MAX_THREADS]; FILE *fp; gwlib_init(); act.sa_handler = sigterm; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGTERM, &act, NULL); sigaction(SIGINT, &act, NULL); port = 8080; use_threads = 1; verbose = 1; run = 1; filename = NULL; log_filename = NULL; blacklist_name = NULL; whitelist_name = NULL; white_asked = 0; black_asked = 0; reply_text = octstr_create("Sent."); while ((opt = getopt(argc, argv, "hqv:p:t:f:l:sc:k:b:w:r:H:")) != EOF) { switch (opt) { case 'v': log_set_output_level(atoi(optarg)); break; case 'q': verbose = 0; break; case 'h': help(); exit(0); case 'p': port = atoi(optarg); break; case 't': use_threads = atoi(optarg); if (use_threads > MAX_THREADS) use_threads = MAX_THREADS; break; case 'c': #ifdef HAVE_LIBSSL octstr_destroy(ssl_server_cert_file); ssl_server_cert_file = octstr_create(optarg); #endif break; case 'k': #ifdef HAVE_LIBSSL octstr_destroy(ssl_server_key_file); ssl_server_key_file = octstr_create(optarg); #endif break; case 's': #ifdef HAVE_LIBSSL ssl = 1; #endif break; case 'f': filename = optarg; break; case 'l': octstr_destroy(log_filename); log_filename = octstr_create(optarg); break; case 'w': whitelist_name = optarg; if (whitelist_name == NULL) whitelist_name = ""; white_asked = 1; break; case 'b': blacklist_name = optarg; if (blacklist_name == NULL) blacklist_name = ""; black_asked = 1; break; case 'r': octstr_destroy(reply_text); reply_text = octstr_create(optarg); break; case 'H': { Octstr *cont; fp = fopen(optarg, "a"); if (fp == NULL) panic(0, "Cannot open header text file %s", optarg); cont = octstr_read_file(optarg); if (cont == NULL) panic(0, "Cannot read header text"); debug("", 0, "headers are"); octstr_dump(cont, 0); split_headers(cont, &extra_headers); fclose(fp); octstr_destroy(cont); break; } case '?': default: error(0, "Invalid option %c", opt); help(); panic(0, "Stopping."); } } if (log_filename != NULL) { log_open(octstr_get_cstr(log_filename), GW_DEBUG, GW_NON_EXCL); octstr_destroy(log_filename); } if (filename == NULL) file_contents = NULL; else file_contents = octstr_read_file(filename); if (white_asked) { whitelist = octstr_read_file(whitelist_name); if (whitelist == NULL) panic(0, "Cannot read the whitelist"); } if (black_asked) { blacklist = octstr_read_file(blacklist_name); if (blacklist == NULL) panic(0, "Cannot read the blacklist"); } #ifdef HAVE_LIBSSL /* * check if we are doing a SSL-enabled server version here * load the required cert and key file */ if (ssl) { if (ssl_server_cert_file != NULL && ssl_server_key_file != NULL) { use_global_server_certkey_file(ssl_server_cert_file, ssl_server_key_file); octstr_destroy(ssl_server_cert_file); octstr_destroy(ssl_server_key_file); } else { panic(0, "certificate and public key need to be given!"); } } #endif if (http_open_port(port, ssl) == -1) panic(0, "http_open_server failed"); /* * Do the real work in a separate thread so that the main * thread can catch signals safely. */ for (i = 0; i < use_threads; ++i) threads[i] = gwthread_create(client_thread, file_contents); /* wait for all working threads */ for (i = 0; i < use_threads; ++i) gwthread_join(threads[i]); octstr_destroy(reply_text); gwlist_destroy(extra_headers, octstr_destroy_item); debug("test.http", 0, "Program exiting normally."); gwlib_shutdown(); return 0; }
static void client_thread(void *arg) { HTTPClient *client; Octstr *body, *url, *ip; List *headers, *resph, *cgivars; HTTPCGIVar *v; Octstr *reply_body, *reply_type; unsigned long n = 0; int status, i; while (run) { client = http_accept_request(port, &ip, &url, &headers, &body, &cgivars); n++; if (client == NULL) break; info(0, "Request for <%s> from <%s>", octstr_get_cstr(url), octstr_get_cstr(ip)); if (verbose) debug("test.http", 0, "CGI vars were"); /* * Don't use gwlist_extract() here, otherwise we don't have a chance * to re-use the cgivars later on. */ for (i = 0; i < gwlist_len(cgivars); i++) { if ((v = gwlist_get(cgivars, i)) != NULL && verbose) { octstr_dump(v->name, 0); octstr_dump(v->value, 0); } } if (arg == NULL) { reply_body = octstr_duplicate(reply_text); reply_type = octstr_create("Content-Type: text/plain; " "charset=\"UTF-8\""); } else { reply_body = octstr_duplicate(arg); reply_type = octstr_create("Content-Type: text/vnd.wap.wml"); } resph = gwlist_create(); gwlist_append(resph, reply_type); status = HTTP_OK; /* check for special URIs and handle those */ if (octstr_compare(url, octstr_imm("/quit")) == 0) { run = 0; } else if (octstr_compare(url, octstr_imm("/whitelist")) == 0) { octstr_destroy(reply_body); if (whitelist != NULL) { if (verbose) { debug("test.http.server", 0, "we send a white list"); octstr_dump(whitelist, 0); } reply_body = octstr_duplicate(whitelist); } else { reply_body = octstr_imm(""); } } else if (octstr_compare(url, octstr_imm("/blacklist")) == 0) { octstr_destroy(reply_body); if (blacklist != NULL) { if (verbose) { debug("test.http.server", 0, "we send a blacklist"); octstr_dump(blacklist, 0); } reply_body = octstr_duplicate(blacklist); } else { reply_body = octstr_imm(""); } } else if (octstr_compare(url, octstr_imm("/save")) == 0) { /* safe the body into a temporary file */ pid_t pid = getpid(); FILE *f = fopen(octstr_get_cstr(octstr_format("/tmp/body.%ld.%ld", pid, n)), "w"); octstr_print(f, body); fclose(f); } else if (octstr_compare(url, octstr_imm("/redirect/")) == 0) { /* provide us with a HTTP 302 redirection response * will return /redirect/<pid> for the location header * and will return /redirect/ if cgivar loop is set to allow looping */ Octstr *redirect_header, *scheme, *uri, *l; pid_t pid = getpid(); uri = ((l = http_cgi_variable(cgivars, "loop")) != NULL) ? octstr_format("%s?loop=%s", octstr_get_cstr(url), octstr_get_cstr(l)) : octstr_format("%s%ld", octstr_get_cstr(url), pid); octstr_destroy(reply_body); reply_body = octstr_imm("Here you got a redirection URL that you should follow."); scheme = ssl ? octstr_imm("https://") : octstr_imm("http://"); redirect_header = octstr_format("Location: %s%s%s", octstr_get_cstr(scheme), octstr_get_cstr(http_header_value(headers, octstr_imm("Host"))), octstr_get_cstr(uri)); gwlist_append(resph, redirect_header); status = HTTP_FOUND; /* will provide 302 */ octstr_destroy(uri); } else if (octstr_compare(url, octstr_imm("/mmsc")) == 0) { /* fake a M-Send.conf PDU which is using MMSEncapsulation as body */ pid_t pid = getpid(); FILE *f; gwlist_destroy(resph, octstr_destroy_item); octstr_destroy(reply_body); reply_type = octstr_create("Content-Type: application/vnd.wap.mms-message"); reply_body = octstr_create(""); octstr_append_from_hex(reply_body, "8c81" /* X-Mms-Message-Type: m-send-conf */ "98632d3862343300" /* X-Mms-Transaction-ID: c-8b43 */ "8d90" /* X-Mms-MMS-Version: 1.0 */ "9280" /* Response-status: Ok */ "8b313331373939353434393639383434313731323400" ); /* Message-Id: 13179954496984417124 */ resph = gwlist_create(); gwlist_append(resph, reply_type); /* safe the M-Send.req body into a temporary file */ f = fopen(octstr_get_cstr(octstr_format("/tmp/mms-body.%ld.%ld", pid, n)), "w"); octstr_print(f, body); fclose(f); } if (verbose) { debug("test.http", 0, "request headers were"); http_header_dump(headers); if (body != NULL) { debug("test.http", 0, "request body was"); octstr_dump(body, 0); } } if (extra_headers != NULL) http_header_combine(resph, extra_headers); /* return response to client */ http_send_reply(client, status, resph, reply_body); octstr_destroy(ip); octstr_destroy(url); octstr_destroy(body); octstr_destroy(reply_body); http_destroy_cgiargs(cgivars); gwlist_destroy(headers, octstr_destroy_item); gwlist_destroy(resph, octstr_destroy_item); } octstr_destroy(whitelist); octstr_destroy(blacklist); debug("test.http", 0, "Working thread 'client_thread' terminates"); http_close_all_ports(); }
void wtls_pdu_dump(wtls_PDU *pdu, int level) { unsigned char *dbg = "wap.wtls"; /* the message type */ debug(dbg, 0, "%*sPDU type: %p", level, "", pdu->type); /* the reserved bit */ debug(dbg, 0, "%*sReserved bit: %p", level, "", pdu->reserved); /* cipher usage flag */ debug(dbg, 0, "%*sCipher in use: %p", level, "", pdu->cipher); /* the sequence number flag */ debug(dbg, 0, "%*sSequence number in use: %p", level, "", pdu->seqnum); /* the record field length flag */ debug(dbg, 0, "%*sRecord field length present: %p", level, "", pdu->rlen); switch (pdu->type) { case ChangeCipher_PDU: debug(dbg, 0, "%*sChangeCipher:", level, ""); debug(dbg, 0, "%*sChange: %d", level+1, "", pdu->u.cc.change); break; case Alert_PDU: debug(dbg, 0, "%*sAlert:", level, ""); debug(dbg, 0, "%*sLevel: %p", level+1, "", pdu->u.alert.level); debug(dbg, 0, "%*sDescription: %d", level+1, "", pdu->u.alert.desc); debug(dbg, 0, "%*sChecksum: %p", level+1, "", pdu->u.alert.chksum); break; case Handshake_PDU: debug(dbg, 0, "%*sHandshake:", level, ""); debug(dbg, 0, "%*sMessage Type: %d", level+1, "", pdu->u.handshake.msg_type); debug(dbg, 0, "%*sLength: %d", level+1, "", pdu->u.handshake.length); switch (pdu->u.handshake.msg_type) { case hello_request: debug(dbg, 0, "%*sHelloRequest.", level, ""); break; case client_hello: debug(dbg, 0, "%*sClientHello :", level, ""); debug(dbg, 0, "%*sClient version: %d", level+1, "", pdu->u.handshake.client_hello->clientversion); debug(dbg, 0, "%*sRandom:", level+1, ""); dump_random(dbg, level+2, pdu->u.handshake.client_hello->random); debug(dbg, 0, "%*sSessionId: ", level, ""); octstr_dump(pdu->u.handshake.client_hello->session_id, level + 2); /* pack the list of keys */ debug(dbg, 0, "%*sClient Key IDs: ", level+1, ""); dump_key_list(dbg, level+2, pdu->u.handshake.client_hello->client_key_ids); debug(dbg, 0, "%*sTrusted Key IDs: ", level+1, ""); dump_key_list(dbg, level+2, pdu->u.handshake.client_hello->trusted_key_ids); /* pack the list of CipherSuites */ debug(dbg, 0, "%*sCipherSuite List: ", level+1, ""); dump_ciphersuite_list(dbg, level+2, pdu->u.handshake.client_hello->ciphersuites); /* CompressionMethods */ debug(dbg, 0, "%*sCompression Method List: ", level+1, ""); dump_compression_method_list(dbg, level+2, pdu->u.handshake.client_hello->comp_methods); debug(dbg, 0, "%*sSeq Number Mode: %d", level+1, "", pdu->u.handshake.client_hello->snmode); debug(dbg, 0, "%*sKey Refresh: %p", level+1, "", pdu->u.handshake.client_hello->krefresh); break; case server_hello: debug(dbg, 0, "%*sServerHello :", level, ""); debug(dbg, 0, "%*sServer version: %d", level+1, "", pdu->u.handshake.server_hello->serverversion); debug(dbg, 0, "%*sRandom:", level+1, ""); dump_random(dbg, level+2, pdu->u.handshake.server_hello->random); debug(dbg, 0, "%*sSession ID: %d", level+1, "", pdu->u.handshake.server_hello->session_id); debug(dbg, 0, "%*sClient Key ID: %p", level+1, "", pdu->u.handshake.server_hello->client_key_id); /* CypherSuite */ debug(dbg, 0, "%*sBulk Cipher Algo: %p", level+1, "", pdu->u.handshake.server_hello->ciphersuite->bulk_cipher_algo); debug(dbg, 0, "%*sMAC Algo: %p", level+1, "", pdu->u.handshake.server_hello->ciphersuite->mac_algo); /* CompressionMethod */ debug(dbg, 0, "%*sCompression Method: %p", level+1, "", pdu->u.handshake.server_hello->comp_method); debug(dbg, 0, "%*sSeq Number Mode: %p", level+1, "", pdu->u.handshake.server_hello->snmode); debug(dbg, 0, "%*sKey Refresh: %p", level+1, "", pdu->u.handshake.server_hello->krefresh); break; case certificate: debug(dbg, 0, "%*sCertificate :", level, ""); debug(dbg, 0, "%*sCertificate Format: %p", level+1, "", pdu->u.handshake.certificate->certificateformat); switch (pdu->u.handshake.certificate->certificateformat) { case WTLSCert: debug(dbg, 0, "%*sWTLS Certificate: %p", level+1, ""); dump_wtls_certificate(dbg, level+2, pdu->u.handshake.certificate->wtls_certificate); break; case X509Cert: debug(dbg, 0, "%*sX509 Certificate: %p", level+1, ""); octstr_dump(pdu->u.handshake.certificate->x509_certificate, level+2); break; case X968Cert: debug(dbg, 0, "%*sX968 Certificate: %p", level+1, ""); octstr_dump(pdu->u.handshake.certificate->x968_certificate, level+2); break; } break; case server_key_exchange: debug(dbg, 0, "%*sServerKeyExchange :", level, ""); /* ParameterSpecifier */ debug(dbg, 0, "%*sParameter Index: %p", level+1, "", pdu->u.handshake.server_key_exchange->param_spec->param_index); if(pdu->u.handshake.server_key_exchange->param_spec->param_index == 255) { /* ParameterSet */ debug(dbg, 0, "%*sParameter Set: %p", level+1, "", pdu->u.handshake.server_key_exchange->param_spec->param_set); } switch (client_key_exchange_algo) { case rsa_anon: dump_rsa_pubkey(dbg, level+1, pdu->u.handshake.server_key_exchange->rsa_params); break; case dh_anon: dump_dh_pubkey(dbg, level+1, pdu->u.handshake.server_key_exchange->dh_params); break; case ecdh_anon: dump_ec_pubkey(dbg, level+1, pdu->u.handshake.server_key_exchange->ecdh_params); break; } break; case client_key_exchange: debug(dbg, 0, "%*sClientKeyExchange :", level, ""); switch (client_key_exchange_algo) { case rsa: case rsa_anon: dump_rsa_encrypted_secret(dbg, level+1, pdu->u.handshake.client_key_exchange->rsa_params); break; case dh_anon: dump_dh_pubkey(dbg, level+1, pdu->u.handshake.client_key_exchange->dh_anon_params); break; case ecdh_anon: case ecdh_ecdsa: dump_ec_pubkey(dbg, level+1, pdu->u.handshake.client_key_exchange->ecdh_params); break; } break; case server_hello_done: debug(dbg, 0, "%*sClientHelloDone.", level, ""); /* empty */ break; case finished: debug(dbg, 0, "%*sFinished :", level, ""); debug(dbg, 0, "%*sverify_data :", level+1, ""); octstr_dump(pdu->u.handshake.finished->verify_data, level+2); break; } break; case Application_PDU: debug(dbg, 0, "%*sApplication :", level, ""); /* application message */ octstr_dump(pdu->u.application.data, level+1); break; default: debug(dbg, 0, "%*sWTLS PDU at %p:", level, "", (void *)pdu); debug(dbg, 0, "%*s unknown type %u", level, "", pdu->type); } }
static void proxy_thread(void *arg) { int ss, cs; /* server and client sockets */ int fl; /* socket flags */ Octstr *addr = NULL; int forward; Octstr *tmp; run_thread = 1; ss = cs = -1; /* create client binding, only if we have a remote server * and make the client socet non-blocking */ if (remote_host != NULL) { cs = udp_client_socket(); fl = fcntl(cs, F_GETFL); fcntl(cs, F_SETFL, fl | O_NONBLOCK); addr = udp_create_address(remote_host, remote_port); } /* create server binding */ ss = udp_bind(our_port, octstr_get_cstr(our_host)); /* make the server socket non-blocking */ fl = fcntl(ss, F_GETFL); fcntl(ss, F_SETFL, fl | O_NONBLOCK); if (ss == -1) panic(0, "RADIUS: Couldn't set up server socket for port %ld.", our_port); while (run_thread) { RADIUS_PDU *pdu, *r; Octstr *data, *rdata; Octstr *from_nas, *from_radius; pdu = r = NULL; data = rdata = from_nas = from_radius = NULL; if (read_available(ss, 100000) < 1) continue; /* get request from NAS */ if (udp_recvfrom(ss, &data, &from_nas) == -1) { if (errno == EAGAIN) /* No datagram available, don't block. */ continue; error(0, "RADIUS: Couldn't receive request data from NAS"); continue; } tmp = udp_get_ip(from_nas); info(0, "RADIUS: Got data from NAS <%s:%d>", octstr_get_cstr(tmp), udp_get_port(from_nas)); octstr_destroy(tmp); octstr_dump(data, 0); /* unpacking the RADIUS PDU */ if ((pdu = radius_pdu_unpack(data)) == NULL) { warning(0, "RADIUS: Couldn't unpack PDU from NAS, ignoring."); goto error; } info(0, "RADIUS: from NAS: PDU type: %s", pdu->type_name); /* authenticate the Accounting-Request packet */ if (radius_authenticate_pdu(pdu, &data, secret_nas) == 0) { warning(0, "RADIUS: Authentication failed for PDU from NAS, ignoring."); goto error; } /* store to hash table if not present yet */ mutex_lock(radius_mutex); forward = update_tables(pdu); mutex_unlock(radius_mutex); /* create response PDU for NAS */ r = radius_pdu_create(0x05, pdu); /* * create response authenticator * code+identifier(req)+length+authenticator(req)+(attributes)+secret */ r->u.Accounting_Response.identifier = pdu->u.Accounting_Request.identifier; r->u.Accounting_Response.authenticator = octstr_duplicate(pdu->u.Accounting_Request.authenticator); /* pack response for NAS */ rdata = radius_pdu_pack(r); /* creates response autenticator in encoded PDU */ radius_authenticate_pdu(r, &rdata, secret_nas); /* * forward request to remote RADIUS server only if updated * and if we have a configured remote RADIUS server */ if ((remote_host != NULL) && forward) { if (udp_sendto(cs, data, addr) == -1) { error(0, "RADIUS: Couldn't send to remote RADIUS <%s:%ld>.", octstr_get_cstr(remote_host), remote_port); } else if (read_available(cs, remote_timeout) < 1) { error(0, "RADIUS: Timeout for response from remote RADIUS <%s:%ld>.", octstr_get_cstr(remote_host), remote_port); } else if (udp_recvfrom(cs, &data, &from_radius) == -1) { error(0, "RADIUS: Couldn't receive from remote RADIUS <%s:%ld>.", octstr_get_cstr(remote_host), remote_port); } else { info(0, "RADIUS: Got data from remote RADIUS <%s:%d>.", octstr_get_cstr(udp_get_ip(from_radius)), udp_get_port(from_radius)); octstr_dump(data, 0); /* XXX unpack the response PDU and check if the response * authenticator is valid */ } } /* send response to NAS */ if (udp_sendto(ss, rdata, from_nas) == -1) error(0, "RADIUS: Couldn't send response data to NAS <%s:%d>.", octstr_get_cstr(udp_get_ip(from_nas)), udp_get_port(from_nas)); error: radius_pdu_destroy(pdu); radius_pdu_destroy(r); octstr_destroy(rdata); octstr_destroy(data); octstr_destroy(from_nas); debug("radius.proxy", 0, "RADIUS: Mapping table contains %ld elements", dict_key_count(radius_table)); debug("radius.proxy", 0, "RADIUS: Session table contains %ld elements", dict_key_count(session_table)); debug("radius.proxy", 0, "RADIUS: Client table contains %ld elements", dict_key_count(client_table)); } octstr_destroy(addr); }
static void start_push(Octstr *rcpt_to, int isphonenum, MmsEnvelope *e, MmsMsg *msg) { List *pheaders; static unsigned char ct; /* Transaction counter -- do we need it? */ Octstr *to = NULL; Octstr *pduhdr = octstr_create(""); Octstr *s = NULL; info(0, "mms2mobile.startpush: notification to %s\n", octstr_get_cstr(rcpt_to)); if (!rcpt_to) { error(0, "mobilesender: Queue entry %s has no recipient address!", e->xqfname); goto done; } else to = octstr_duplicate(rcpt_to); ct++; octstr_append_char(pduhdr, ct); /* Pushd id */ octstr_append_char(pduhdr, 0x06); /* PUSH */ #if 1 octstr_append_char(pduhdr, 1 + 1 + 1); octstr_append_char(pduhdr, 0xbe); /* content type. */ #else octstr_append_char(pduhdr, 1 + 1 + sizeof "application/vnd.wap.mms-message"); /*header length. */ octstr_append_cstr(pduhdr, "application/vnd.wap.mms-message"); octstr_append_char(pduhdr, 0x0); /* string terminator. */ #endif octstr_append_char(pduhdr, 0xaf); /* push application ID header and value follows. */ octstr_append_char(pduhdr, 0x84); /* ... */ s = mms_tobinary(msg); if (isphonenum) { Octstr *url; octstr_url_encode(to); octstr_url_encode(s); #if 0 octstr_dump(pduhdr, 0); #endif octstr_url_encode(pduhdr); url = octstr_format("%S&text=%S%S&to=%S&udh=%%06%%05%%04%%0B%%84%%23%%F0", settings->sendsms_url, pduhdr, s, to); pheaders = http_create_empty_headers(); http_header_add(pheaders, "Connection", "close"); http_header_add(pheaders, "User-Agent", MM_NAME "/" MMSC_VERSION); http_start_request(httpcaller, HTTP_METHOD_GET, url, pheaders, NULL, 0, e, NULL); http_destroy_headers(pheaders); octstr_destroy(url); } else { /* An IP Address: Send packet, forget. */ Octstr *addr = udp_create_address(to, WAPPUSH_PORT); int sock = udp_client_socket(); if (sock > 0) { MmsEnvelopeTo *xto = gwlist_get(e->to,0); octstr_append(pduhdr, s); #if 0 octstr_dump(pduhdr, 0); #endif udp_sendto(sock, pduhdr, addr); close(sock); /* ?? */ mms_log2("Notify", octstr_imm("system"), to, -1, e ? e->msgId : NULL, NULL, NULL, "MM1", NULL,NULL); e = update_env_success(e, xto); } else { e = update_env_failed(e); error(0, "push to %s:%d failed: %s", octstr_get_cstr(to), WAPPUSH_PORT, strerror(errno)); } octstr_destroy(addr); if (e) settings->qfs->mms_queue_free_env(e); } done: octstr_destroy(to); octstr_destroy(pduhdr); octstr_destroy(s); }
SMPP_PDU *smpp_pdu_unpack(Octstr *data_without_len) { SMPP_PDU *pdu; unsigned long type; long len, pos; len = octstr_len(data_without_len); if (len < 4) { error(0, "SMPP: PDU was too short (%ld bytes).", octstr_len(data_without_len)); return NULL; } /* get the PDU type */ if ((type = decode_integer(data_without_len, 0, 4)) == -1) return NULL; /* create a coresponding representation structure */ pdu = smpp_pdu_create(type, 0); if (pdu == NULL) return NULL; pos = 0; switch (type) { #define OPTIONAL_BEGIN \ { /* Read optional parameters */ \ while (pos + 4 <= len) { \ unsigned long opt_tag, opt_len; \ opt_tag = decode_integer(data_without_len, pos, 2); pos += 2; \ debug("sms.smpp", 0, "Optional parameter tag (0x%04lx)", opt_tag); \ opt_len = decode_integer(data_without_len, pos, 2); pos += 2; \ debug("sms.smpp", 0, "Optional parameter length read as %ld", opt_len); #define TLV_INTEGER(name, octets) \ if (SMPP_##name == opt_tag) { \ /* check length */ \ if (opt_len > octets) { \ error(0, "SMPP: Optional field (%s) with invalid length (%ld) dropped.", #name, opt_len); \ pos += opt_len; \ continue; \ } \ INTEGER(name, opt_len); \ } else #define TLV_NULTERMINATED(name, max_len) \ if (SMPP_##name == opt_tag) { \ /* check length */ \ if (opt_len > max_len || pos+opt_len > len) { \ error(0, "SMPP: Optional field (%s) with invalid length (%ld) dropped.", #name, opt_len); \ pos += opt_len; \ continue; \ } \ copy_until_nul(#name, data_without_len, &pos, opt_len, &p->name); \ } else #define TLV_OCTETS(name, min_len, max_len) \ if (SMPP_##name == opt_tag) { \ /* check length */ \ if (opt_len < min_len || opt_len > max_len || pos + opt_len > len) { \ error(0, "SMPP: Optional field (%s) with invalid length (%ld) (should be %d - %d) dropped.", \ #name, opt_len, min_len, max_len); \ pos += opt_len; \ continue; \ } \ p->name = octstr_copy(data_without_len, pos, opt_len); \ pos += opt_len; \ } else #define OPTIONAL_END \ { \ Octstr *val = octstr_copy(data_without_len, pos, opt_len); \ if (val) octstr_binary_to_hex(val, 0); \ else val = octstr_create(""); \ warning(0, "SMPP: Unknown TLV(0x%04lx,0x%04lx,%s) for PDU type (%s) received!", \ opt_tag, opt_len, octstr_get_cstr(val), pdu->type_name); \ pos += opt_len; \ octstr_destroy(val); \ } \ } \ } #define INTEGER(name, octets) \ if ((p->name = decode_integer(data_without_len, pos, octets)) == -1) \ goto err; \ pos += octets; #define NULTERMINATED(name, max_octets) \ /* just warn about errors but not fail */ \ copy_until_nul(#name, data_without_len, &pos, max_octets, &p->name); #define OCTETS(name, field_giving_octets) \ p->name = octstr_copy(data_without_len, pos, \ p->field_giving_octets); \ if (p->field_giving_octets != (unsigned long) octstr_len(p->name)) { \ error(0, "smpp_pdu: error while unpacking '" #name "', " \ "len is %ld but should have been %ld, dropping.", \ octstr_len(p->name), p->field_giving_octets); \ goto err; \ } else { \ pos += p->field_giving_octets; \ } #define PDU(name, id, fields) \ case id: { struct name *p = &pdu->u.name; fields } break; #include "smpp_pdu.def" default: error(0, "Unknown SMPP_PDU type, internal error while unpacking."); } return pdu; err: smpp_pdu_destroy(pdu); octstr_dump(data_without_len, 0); return NULL; }
wtls_Payload *wtls_pdu_pack(wtls_PDU *pdu, WTLSMachine* wtls_machine) { Octstr *data, *buffer, *encryptedbuffer; wtls_Payload *payload; long bitpos, charpos; long messageSizePos, sizepos; /* Used for length calculations */ int size, recordType; /* create the wtls_PDU */ payload = (wtls_Payload *)gw_malloc(sizeof(wtls_Payload)); payload->type = pdu->type; payload->reserved = pdu->reserved; payload->cipher = pdu->cipher; payload->seqnum = pdu->seqnum; /* We rely on octstr_set_bits to lengthen our octstr as needed. */ data = octstr_create(""); buffer = octstr_create(""); bitpos = 0; charpos = 0; sizepos = 0; switch (pdu->type) { case ChangeCipher_PDU: octstr_append_char(buffer, pdu->u.cc.change); charpos += 1; break; case Alert_PDU: octstr_append_char(buffer, pdu->u.alert.level); charpos += 1; octstr_append_char(buffer, pdu->u.alert.desc); charpos += 1; charpos = pack_octstr_fixed(buffer, charpos, pdu->u.alert.chksum); charpos += 1; break; case Handshake_PDU: octstr_append_char(buffer, pdu->u.handshake.msg_type); charpos += 1; /* Save the location of the message size */ messageSizePos = charpos; charpos = pack_int16 (buffer, charpos, pdu->u.handshake.length); switch (pdu->u.handshake.msg_type) { case hello_request: break; case client_hello: octstr_append_char(buffer, pdu->u.handshake.client_hello->clientversion); charpos += 1; charpos = pack_random(buffer, charpos, pdu->u.handshake.client_hello->random); octstr_append_char(buffer, octstr_len( pdu->u.handshake.client_hello->session_id)); charpos += 1; charpos = pack_octstr(buffer, charpos, pdu->u.handshake.client_hello->session_id); /* pack the list of keys */ charpos = pack_key_list(buffer, charpos, pdu->u.handshake.client_hello->client_key_ids); charpos = pack_key_list(buffer, charpos, pdu->u.handshake.client_hello->trusted_key_ids); /* pack the list of CipherSuites */ charpos = pack_ciphersuite_list(buffer, charpos, pdu->u.handshake.client_hello->ciphersuites); /* CompressionMethods */ charpos = pack_compression_method_list(buffer, charpos, pdu->u.handshake.client_hello->comp_methods); octstr_append_char(buffer, pdu->u.handshake.client_hello->snmode); charpos += 1; octstr_append_char(buffer, pdu->u.handshake.client_hello->krefresh); charpos += 1; break; case server_hello: octstr_append_char(buffer, pdu->u.handshake.server_hello->serverversion); charpos += 1; charpos = pack_random(buffer, charpos, pdu->u.handshake.server_hello->random); charpos = pack_octstr(buffer, charpos, pdu->u.handshake.server_hello->session_id); charpos += 1; octstr_append_char(buffer, pdu->u.handshake.server_hello-> client_key_id); charpos += 1; /* CypherSuite */ octstr_append_char(buffer, pdu->u.handshake.server_hello-> ciphersuite->bulk_cipher_algo); charpos += 1; octstr_append_char(buffer, pdu->u.handshake.server_hello-> ciphersuite->mac_algo); charpos += 1; /* CompressionMethod */ octstr_append_char(buffer, pdu->u.handshake.server_hello->comp_method); charpos += 1; octstr_append_char(buffer, pdu->u.handshake.server_hello->snmode); charpos += 1; octstr_append_char(buffer, pdu->u.handshake.server_hello->krefresh); charpos += 1; break; case certificate: octstr_append_char(buffer, pdu->u.handshake.certificate->certificateformat); charpos += 1; switch (pdu->u.handshake.certificate->certificateformat) { case WTLSCert: charpos = pack_wtls_certificate(buffer, charpos, pdu->u.handshake.certificate->wtls_certificate); break; case X509Cert: charpos = pack_octstr16(buffer, charpos, pdu->u.handshake.certificate->x509_certificate); break; case X968Cert: charpos = pack_octstr16(buffer, charpos, pdu->u.handshake.certificate->x968_certificate); break; } break; case server_key_exchange: debug("wtls: ", 0,"Packing ServerKeyExchange"); /* pack the ParameterSpecifier */ charpos = pack_param_spec(buffer, charpos, pdu->u.handshake.server_key_exchange->param_spec); switch (client_key_exchange_algo) { case rsa_anon: charpos = pack_rsa_pubkey(buffer, charpos, pdu->u.handshake.server_key_exchange->rsa_params); break; case dh_anon: charpos = pack_dh_pubkey(buffer, charpos, pdu->u.handshake.server_key_exchange->dh_params); break; case ecdh_anon: charpos = pack_ec_pubkey(buffer, charpos, pdu->u.handshake.server_key_exchange->ecdh_params); break; } break; case client_key_exchange: switch (client_key_exchange_algo) { case rsa: case rsa_anon: charpos = pack_rsa_encrypted_secret(buffer, charpos, pdu->u.handshake.client_key_exchange->rsa_params); break; case dh_anon: charpos = pack_dh_pubkey(buffer, charpos, pdu->u.handshake.client_key_exchange->dh_anon_params); break; case ecdh_anon: case ecdh_ecdsa: charpos = pack_ec_pubkey(buffer, charpos, pdu->u.handshake.client_key_exchange->ecdh_params); break; } break; case server_hello_done: /* empty */ break; case finished: charpos = pack_octstr_fixed(buffer, charpos, pdu->u.handshake.finished->verify_data); debug("wtls", 0, "verify_data (in pack)"); octstr_dump(pdu->u.handshake.finished->verify_data,0 ); break; } /* Change the length */ size = octstr_len(buffer) - messageSizePos - 2; debug("wtls_msg.c:length",0,"Setting msg size to : %d",size); octstr_set_char(buffer, messageSizePos, (size & 0xFF00) >> 8); messageSizePos += 1; octstr_set_char(buffer, messageSizePos, (size & 0x00FF)); /* we keep the handshake data to create the Finished PDU */ octstr_append(wtls_machine->handshake_data, buffer); break; case Application_PDU: /* application message */ charpos += pack_octstr(data, charpos, pdu->u.application.data); break; default: panic(0, "Packing unknown WTLS PDU type %ld", (long) pdu->type); } /* encrypt the buffer if needed */ if(pdu->cipher) { /* the MAC is calculated with the record type so we need it now */ recordType = 1 << 7; /* length, always present */ recordType |= pdu->seqnum << 6; recordType |= pdu->cipher << 5; recordType |= pdu->reserved << 4; recordType |= pdu->type; encryptedbuffer = wtls_encrypt(buffer, wtls_machine, recordType); payload->data = encryptedbuffer; } else { payload->data = buffer; } payload->rlen = octstr_len(payload->data); debug("wtls", 0, "Packed PDU Length: %d", payload->rlen); return payload; }