static void init_batch(Octstr *cfilename, Octstr *rfilename) { Octstr *receivers; long lineno = 0; content = octstr_read_file(octstr_get_cstr(cfilename)); octstr_strip_crlfs(content); if (content == NULL) panic(0,"Can not read content file `%s'.", octstr_get_cstr(cfilename)); info(0,"SMS-Text: <%s>", octstr_get_cstr(content)); info(0,"Loading receiver list. This may take a while..."); receivers = octstr_read_file(octstr_get_cstr(rfilename)); if (receivers == NULL) panic(0,"Can not read receivers file `%s'.", octstr_get_cstr(rfilename)); lines = octstr_split(receivers, octstr_imm("\n")); lineno = gwlist_len(lines); if (lineno <= 0) panic(0,"Receiver file seems empty!"); info(0,"Receivers file `%s' contains %ld destination numbers.", octstr_get_cstr(rfilename), lineno); counter = counter_create(); }
static MmsMsg *mms_queue_getdata(MmsEnvelope *e) { Octstr *fname; Octstr *ms; MmsMsg *m; struct qfile_t *qfs; if (!e) return NULL; qfs = e->qfs_data; fname = octstr_format("%s/%s%c%s", qfs->dir, qfs->subdir, MDF, qfs->name + 1); ms = octstr_read_file(octstr_get_cstr(fname)); if (!ms) { error(0, "mms_queue_getdata: Failed to load data file for queue entry %s in %s", qfs->name, qfs->dir); octstr_destroy(fname); return NULL; } m = mms_frombinary(ms, octstr_imm("")); if (!m) { error(0, "mms_queue_getdata: Failed to decode data file for queue entry %s in %s", qfs->name, qfs->dir); octstr_destroy(fname); return NULL; } octstr_destroy(ms); octstr_destroy(fname); return m; }
static int verified_file(const char *filename, const struct stat *sb, int tflag, struct FTW *ftwbuf) { Octstr *os; Msg *msg; /* we need to check here if we have a regular file. */ if (tflag != FTW_F) return 0; #else static int verified_file(const char *filename, const struct stat *sb, int tflag, void *ftwbuf) { Octstr *os; Msg *msg; #endif if ((os = octstr_read_file(filename)) == NULL) { return -1; } if ((msg = store_msg_unpack(os)) == NULL) { error(0, "Could not unpack DLR message `%s'", filename); octstr_destroy(os); return -1; } /* we could load and unpack, so this is verified */ counter_increase(counter); octstr_destroy(os); msg_destroy(msg); return 0; }
static Octstr *get_msg_surrogate(const Octstr *dir_s, const Octstr *hash, const Octstr *dst, Octstr **filename) { /* get our msg filename */ if ((*filename = get_msg_filename(dir_s, hash, dst)) == NULL) return NULL; return octstr_read_file(octstr_get_cstr(*filename)); }
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 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; }
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; }
static void status_cb(const Octstr *filename, void *d) { struct status *data = d; Octstr *msg_s; Msg *msg; msg_s = octstr_read_file(octstr_get_cstr(filename)); msg = store_msg_unpack(msg_s); octstr_destroy(msg_s); if (msg == NULL) return; data->callback_fn(msg, data->data); msg_destroy(msg); }
static void status_cb(const Octstr *filename, void *d) { struct status *data = d; struct tm tm; char id[UUID_STR_LEN + 1]; Octstr *msg_s; Msg *msg; msg_s = octstr_read_file(octstr_get_cstr(filename)); msg = store_msg_unpack(msg_s); octstr_destroy(msg_s); if (msg == NULL) return; /* transform the time value */ #if LOG_TIMESTAMP_LOCALTIME tm = gw_localtime(msg->sms.time); #else tm = gw_gmtime(msg->sms.time); #endif if (msg->sms.udhdata) octstr_binary_to_hex(msg->sms.udhdata, 1); if (msg->sms.msgdata && (msg->sms.coding == DC_8BIT || msg->sms.coding == DC_UCS2 || (msg->sms.coding == DC_UNDEF && msg->sms.udhdata))) octstr_binary_to_hex(msg->sms.msgdata, 1); uuid_unparse(msg->sms.id, id); octstr_format_append(data->status, data->format, id, (msg->sms.sms_type == mo ? "MO" : msg->sms.sms_type == mt_push ? "MT-PUSH" : msg->sms.sms_type == mt_reply ? "MT-REPLY" : msg->sms.sms_type == report_mo ? "DLR-MO" : msg->sms.sms_type == report_mt ? "DLR-MT" : ""), tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, (msg->sms.sender ? octstr_get_cstr(msg->sms.sender) : ""), (msg->sms.receiver ? octstr_get_cstr(msg->sms.receiver) : ""), (msg->sms.smsc_id ? octstr_get_cstr(msg->sms.smsc_id) : ""), (msg->sms.boxc_id ? octstr_get_cstr(msg->sms.boxc_id) : ""), (msg->sms.udhdata ? octstr_get_cstr(msg->sms.udhdata) : ""), (msg->sms.msgdata ? octstr_get_cstr(msg->sms.msgdata) : "")); msg_destroy(msg); }
static void check_reversible(void) { Octstr *dates; long pos, endpos, tabpos; Octstr *date, *canondate; long timeval; dates = octstr_read_file("checks/test_dates"); if (dates == NULL) return; for (pos = 0; ; pos = endpos + 1) { endpos = octstr_search_char(dates, '\n', pos); if (endpos < 0) break; tabpos = octstr_search_char(dates, '\t', pos); if (tabpos >= 0 && tabpos < endpos) { date = octstr_copy(dates, pos, tabpos - pos); canondate = octstr_copy(dates, tabpos + 1, endpos - tabpos - 1); } else { date = octstr_copy(dates, pos, endpos - pos); canondate = octstr_duplicate(date); } timeval = date_parse_http(date); if (timeval == -1) warning(0, "Could not parse date \"%s\"", octstr_get_cstr(date)); else { Octstr *newdate; newdate = date_format_http((unsigned long) timeval); if (octstr_compare(newdate, canondate) != 0) { warning(0, "Date not reversible: \"%s\" becomes \"%s\"", octstr_get_cstr(date), octstr_get_cstr(newdate)); } octstr_destroy(newdate); } octstr_destroy(date); octstr_destroy(canondate); } octstr_destroy(dates); }
MmsMsg *mms_mmbox_get(char *mmbox_root, char *user, Octstr *msgref, unsigned long *msize) { Octstr *sdf = octstr_duplicate(msgref); Octstr *fname = NULL, *home = user_mmbox_dir(mmbox_root,user); Octstr *s = NULL; int ifd = -1; MmsMsg *m = NULL; octstr_replace(sdf, octstr_imm("-"), octstr_imm("/")); if (!home) goto done; ifd = open_mmbox_index(octstr_get_cstr(home),1); /* Grab a lock on the index file. */ if (ifd < 0) goto done; fname = octstr_format("%S/%S", home, sdf); s = octstr_read_file(octstr_get_cstr(fname)); if (s) { if (msize) *msize = octstr_len(s); m = mms_frombinary(s, octstr_imm("anon@anon")); } else if (msize) *msize = 0; done: if (ifd > 0) unlock_and_close(ifd); if (fname) octstr_destroy(fname); if (s) octstr_destroy(s); if (home) octstr_destroy(home); return m; }
static void dispatch(const Octstr *filename, void *data) { Octstr *msg_s; Msg *msg; void(*receive_msg)(Msg*) = data; /* debug("", 0, "dispatch(%s,...) called", octstr_get_cstr(filename)); */ msg_s = octstr_read_file(octstr_get_cstr(filename)); if (msg_s == NULL) return; msg = store_msg_unpack(msg_s); octstr_destroy(msg_s); if (msg != NULL) { receive_msg(msg); counter_increase(counter); } else { error(0, "Could not unpack message `%s'", octstr_get_cstr(filename)); } }
static void send_file(int udpsock, char *filename, Octstr *address) { Octstr *contents; contents = octstr_read_file(filename); if (contents == NULL) { info(0, "Skipping \"%s\".", filename); return; } info(0, "Sending \"%s\", %ld octets.", filename, octstr_len(contents)); if (octstr_len(contents) > maxsize) { octstr_truncate(contents, maxsize); warning(0, "Truncating to %ld octets.", maxsize); } udp_sendto(udpsock, contents, address); octstr_destroy(contents); }
static int find_own(int i, int argc, char *argv[]) { if (argv[i][1] == 'f') if (i + 1 < argc) { from = octstr_create(argv[i+1]); return 1; } else return -1; else if (argv[i][1] == 'b') { savetommbox = 1; return 0; } else if (argv[i][1] == 't') if (i + 1 < argc) { Octstr *x = octstr_create(argv[i+1]); to = octstr_split(x, octstr_imm(":")); octstr_destroy(x); return 1; } else return -1; else if (argv[i][1] == 'm') if (i + 1 < argc) { data = octstr_read_file(argv[i+1]); return 1; } else return -1; else if (argv[i][1] == 'r') { dlr = 1; return 0; } else if (argv[i][1] == 'B') if (i + 1 < argc) { binfo = octstr_create(argv[i+1]); return 1; } else return -1; else return -1; }
int main(int argc, char **argv) { int opt, ret; Octstr *pap_doc, *log_file; WAPEvent *e; log_file = NULL; gwlib_init(); while ((opt = getopt(argc, argv, "h:v:l:")) != EOF) { switch (opt) { case 'h': help(); exit(1); break; case 'v': log_set_output_level(atoi(optarg)); break; case 'l': octstr_destroy(log_file); log_file = octstr_create(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"); } if (log_file != NULL) { log_open(octstr_get_cstr(log_file), GW_DEBUG, GW_NON_EXCL); octstr_destroy(log_file); } pap_doc = octstr_read_file(argv[optind]); if (pap_doc == NULL) panic(0, "Cannot read the pap document"); e = NULL; ret = pap_compile(pap_doc, &e); if (ret < 0) { debug("test.pap", 0, "Unable to compile the pap document, rc %d", ret); return 1; } debug("test.pap", 0, "Compiling successfull, wap event being:\n"); wap_event_dump(e); wap_event_destroy(e); octstr_destroy(pap_doc); gwlib_shutdown(); return 0; }
static List *expand_file(Octstr *file, int forward) { Octstr *os; Octstr *line; List *lines; List *expand; long lineno; CfgLoc *loc = NULL; os = octstr_read_file(octstr_get_cstr(file)); if (os == NULL) return NULL; lines = octstr_split(os, octstr_imm("\n")); lineno = 0; expand = gwlist_create(); while ((line = gwlist_extract_first(lines)) != NULL) { if (loc == NULL) { ++lineno; loc = cfgloc_create(file); loc->line_no = lineno; loc->line = octstr_create(""); if (forward) gwlist_append(expand, loc); else gwlist_insert(expand, 0, loc); } /* check for escape and then add to existing loc */ if (octstr_get_char(line, octstr_len(line) - 1) == '\\') { octstr_delete(line, octstr_len(line) - 1, 1); octstr_append(loc->line, line); /* check for second escape */ if (octstr_get_char(line, octstr_len(line) - 1) == '\\') loc = NULL; } else { octstr_append(loc->line, line); loc = NULL; } octstr_destroy(line); } /* * add newline at each end of included files to avoid * concatenating different groups by mistake */ if (lineno > 0) { loc = cfgloc_create(file); loc->line_no = lineno; loc->line = octstr_create("\n"); if (forward) gwlist_append(expand, loc); else gwlist_insert(expand, 0, loc); } gwlist_destroy(lines, octstr_destroy_item); octstr_destroy(os); return expand; }
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) { 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) { int i; Octstr *packet = NULL; Octstr *newpacket = NULL; WTP_PDU *pdu = NULL; Octstr *wsp_data = NULL; WSP_PDU *wsp = NULL; gwlib_init(); for (i = 1; i < argc; i++) { octstr_destroy(packet); packet = NULL; octstr_destroy(newpacket); newpacket = NULL; octstr_destroy(wsp_data); wsp_data = NULL; wtp_pdu_destroy(pdu); pdu = NULL; wsp_pdu_destroy(wsp); wsp = NULL; packet = octstr_read_file(argv[i]); pdu = wtp_pdu_unpack(packet); if (!pdu) { warning(0, "Unpacking PDU %s failed", argv[i]); continue; } debug("test", 0, "PDU %s:", argv[i]); wtp_pdu_dump(pdu, 0); newpacket = wtp_pdu_pack(pdu); if (!newpacket) { warning(0, "Repacking PDU %s failed", argv[i]); continue; } if (octstr_compare(packet, newpacket) != 0) { error(0, "Repacking PDU %s changed it", argv[i]); debug("test", 0, "Original:"); octstr_dump(packet, 1); debug("test", 0, "New:"); octstr_dump(newpacket, 1); continue; } if (pdu->type == Invoke) { wsp_data = pdu->u.Invoke.user_data; } else if (pdu->type == Result) { wsp_data = pdu->u.Result.user_data; } else { continue; } wsp_data = octstr_duplicate(wsp_data); wsp = wsp_pdu_unpack(wsp_data); if (!wsp) { warning(0, "Unpacking WSP data in %s failed", argv[i]); continue; } wsp_pdu_dump(wsp, 0); octstr_destroy(newpacket); newpacket = wsp_pdu_pack(wsp); if (!newpacket) { warning(0, "Repacking WSP data in %s failed", argv[i]); continue; } if (octstr_compare(wsp_data, newpacket) != 0) { error(0, "Repacking WSP data in %s changed it", argv[i]); debug("test", 0, "Original:"); octstr_dump(wsp_data, 1); debug("test", 0, "New:"); octstr_dump(newpacket, 1); continue; } } octstr_destroy(packet); octstr_destroy(newpacket); wtp_pdu_destroy(pdu); gwlib_shutdown(); return 0; }
mCfg *mms_cfg_read(Octstr *file) { Octstr *sf; List *lines; int i, n; mCfg *cfg; mCfgGrp *grp = NULL; int skip = 0; gw_assert(file); if ((sf = octstr_read_file(octstr_get_cstr(file))) == NULL) { mms_error(errno, "mms_cfg", NULL, "failed to read config from `%s'", octstr_get_cstr(file)); return NULL; } cfg = gw_malloc(sizeof *cfg); cfg->file = octstr_duplicate(file); cfg->grps = dict_create(7, NULL); cfg->xcfg = NULL; cfg->cfg_funcs = NULL; lines = octstr_split(sf, octstr_imm("\n")); for (i = 0, n = gwlist_len(lines); i < n; i++) { Octstr *current = gwlist_get(lines,i); int pos; octstr_strip_blanks(current); if (octstr_len(current) == 0) { /* end of group. */ grp = NULL; skip = 0; continue; } else if (skip || octstr_get_char(current, 0) == '#') continue; if ((pos = octstr_search_char(current, '=',0)) > 0) { /* a field name. first see if start of grp */ Octstr *field = octstr_copy(current,0,pos); Octstr *value = octstr_copy(current,pos+1,octstr_len(current)); octstr_strip_blanks(field); fixup_value(value, i+1); #if 0 mms_info(0, "mms_cfg", NULL, "field/value: [%s - %s]", octstr_get_cstr(field), octstr_get_cstr(value)); #endif if (octstr_str_case_compare(field, "group") == 0) if (grp == NULL) { /* grp name. */ int ismulti = is_multigroup(value); if (ismulti < 0) { mms_info(0, "mms_cfg", NULL, "Skipping unknown group `%s' at line %d of conf file", octstr_get_cstr(value), i+1); skip = 1; } else { grp = gw_malloc(sizeof *grp); grp->name = octstr_duplicate(value); grp->fields = dict_create(23, (void (*)(void *))octstr_destroy); if (ismulti) { List *l = dict_get(cfg->grps, value); if (l == NULL) { l = gwlist_create(); dict_put(cfg->grps, value, l); } gwlist_append(l, grp); } else if (dict_put_once(cfg->grps, value, grp) == 0) panic(0, "Group `%s' [at line %d] cannot appear more " "than once in config!", octstr_get_cstr(value), i+1); } } else panic(0,"`group' is an illegal field name " "within a group at line %d in config file!", i+1); else if (grp) /* an ordinary field name. */ check_and_add_field(grp, field, value,i+1); else panic(0, "A group must begin with a `group = group_name' " "clause [at line %d in config file]", i+1); octstr_destroy(field); octstr_destroy(value); } else panic(0, "mal-formed entry in conf file at line %d!", i+1); } gwlist_destroy(lines, (gwlist_item_destructor_t *)octstr_destroy); octstr_destroy(sf); /* Now check if config-source is set, use that. */ if ((grp = mms_cfg_get_single(cfg, octstr_imm("config-source"))) != NULL) { Octstr *init = mms_cfg_get(cfg, grp, octstr_imm("config-library-init-param")); cfg->cfg_funcs = _mms_load_module(cfg, grp, "config-library", "cfg_funcs", NULL); if (cfg->cfg_funcs == NULL || cfg->cfg_funcs->read == NULL || (cfg->xcfg = cfg->cfg_funcs->read(init)) == NULL) { mms_error(0, "mms_cfg", NULL, "Failed to load cfg reader library from conf!"); mms_cfg_destroy(cfg); cfg = NULL; } octstr_destroy(init); } return cfg; }
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) { 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 int store_file_load(void(*receive_msg)(Msg*)) { List *keys; Octstr *store_file, *key; Msg *msg; int retval, msgs; long end, pos; if (filename == NULL) return 0; mutex_lock(file_mutex); if (file != NULL) { fclose(file); file = NULL; } store_file = octstr_read_file(octstr_get_cstr(filename)); if (store_file != NULL) info(0, "Loading store file `%s'", octstr_get_cstr(filename)); else { store_file = octstr_read_file(octstr_get_cstr(newfile)); if (store_file != NULL) info(0, "Loading store file `%s'", octstr_get_cstr(newfile)); else { store_file = octstr_read_file(octstr_get_cstr(bakfile)); if (store_file != NULL) info(0, "Loading store file `%s'", octstr_get_cstr(bakfile)); else { info(0, "Cannot open any store file, starting a new one"); retval = open_file(filename); goto end; } } } info(0, "Store-file size %ld, starting to unpack%s", octstr_len(store_file), octstr_len(store_file) > 10000 ? " (may take awhile)" : ""); pos = 0; msgs = 0; end = octstr_len(store_file); while (pos < end) { if (read_msg(&msg, store_file, &pos) == -1) { error(0, "Garbage at store-file, skipped."); continue; } if (msg_type(msg) == sms) { store_to_dict(msg); msgs++; } else if (msg_type(msg) == ack) { store_to_dict(msg); } else { warning(0, "Strange message in store-file, discarded, " "dump follows:"); msg_dump(msg, 0); } msg_destroy(msg); } octstr_destroy(store_file); info(0, "Retrieved %d messages, non-acknowledged messages: %ld", msgs, dict_key_count(sms_dict)); /* now create a new sms_store out of messages left */ keys = dict_keys(sms_dict); while ((key = gwlist_extract_first(keys)) != NULL) { msg = dict_remove(sms_dict, key); if (store_to_dict(msg) != -1) { receive_msg(msg); } else { error(0, "Found unknown message type in store file."); msg_dump(msg, 0); msg_destroy(msg); } octstr_destroy(key); } gwlist_destroy(keys, octstr_destroy_item); /* Finally, generate new store file out of left messages */ retval = do_dump(); end: mutex_unlock(file_mutex); /* allow using of store */ gwlist_remove_producer(loaded); /* start dumper thread */ if ((cleanup_thread = gwthread_create(store_dumper, NULL))==-1) panic(0, "Failed to create a cleanup thread!"); return retval; }
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; }
static Octstr *push_content_create(void) { Octstr *push_content, *wap_content; Octstr *wap_file_content, *pap_content, *pap_file_content, *bpos, *bcos; wap_content = NULL; push_content = NULL; if (use_hardcoded) { push_content = octstr_create("\r\n\r\n" "--asdlfkjiurwgasf\r\n" "Content-Type: application/xml\r\n\r\n" "<?xml version=\"1.0\"?>" "<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP//EN\"" " \"http://www.wapforum.org/DTD/pap_1.0.dtd\">" "<pap>" "<push-message push-id=\"[email protected]\"" " deliver-before-timestamp=\"2002-11-01T06:45:00Z\"" " deliver-after-timestamp=\"2000-02-27T06:45:00Z\"" " progress-notes-requested=\"false\">" "<address address-value=\"WAPPUSH=+358408676001/" "[email protected]\">" "</address>" "<quality-of-service" " priority=\"low\"" " delivery-method=\"unconfirmed\"" " network-required=\"true\"" " network=\"GSM\"" " bearer-required=\"true\"" " bearer=\"SMS\">" "</quality-of-service>" "</push-message>" "</pap>\r\n\r\n" "--asdlfkjiurwgasf\r\n" "Content-Type: text/vnd.wap.si\r\n\r\n" "<?xml version=\"1.0\"?>" "<!DOCTYPE si PUBLIC \"-//WAPFORUM//DTD SI 1.0//EN\" " " \"http://www.wapforum.org/DTD/si.dtd\">" "<si>" "<indication href=\"http://wap.iobox.fi\"" " si-id=\"[email protected]\"" " action=\"signal-high\"" " created=\"1999-06-25T15:23:15Z\"" " si-expires=\"2002-12-30T00:00:00Z\">" "Want to test a fetch?" "</indication>" "</si>\r\n\r\n" "--asdlfkjiurwgasf--\r\n\r\n" ""); } else { add_content_type(content_flag, &wap_content); add_content_transfer_encoding_type(content_transfer_encoding, wap_content); add_part_header(content_header, &wap_content); /* Read the content file. (To be pushed)*/ if ((wap_file_content = octstr_read_file(octstr_get_cstr(content_file))) == NULL) panic(0, "Stopping"); if (accept_binary) { octstr_delete_matching(wap_file_content, octstr_imm(" ")); octstr_delete_matching(wap_file_content, octstr_imm("\n")); octstr_delete_matching(wap_file_content, octstr_imm("\r")); if (!octstr_is_all_hex(wap_file_content)) panic(0, "non-hex chars in the content file, cannot continue"); octstr_hex_to_binary(wap_file_content); } transfer_encode(content_transfer_encoding, wap_file_content); octstr_append(wap_content, wap_file_content); octstr_destroy(wap_file_content); /* Read the control file. (To control pushing)*/ pap_content = octstr_format("%s", "Content-Type: application/xml"); add_delimiter(&pap_content); add_delimiter(&pap_content); if ((pap_file_content = octstr_read_file(octstr_get_cstr(pap_file))) == NULL) panic(0, "Stopping"); octstr_append(pap_content, pap_file_content); octstr_destroy(pap_file_content); if (wap_content == NULL || pap_content == NULL) panic(0, "Cannot open the push content files"); push_content = octstr_create(""); if (add_preamble) octstr_append(push_content, octstr_imm("the parser should discard this")); octstr_append(push_content, bpos = make_part_delimiter(octstr_imm(boundary))); /*octstr_append(push_content, octstr_imm("\r\n"));*/ /* Do we accept an additional * clrf ? */ octstr_append(push_content, pap_content); octstr_append(push_content, bpos); octstr_destroy(bpos); octstr_append(push_content, wap_content); octstr_append(push_content, bcos = make_close_delimiter(octstr_imm(boundary))); if (add_epilogue) { octstr_append(push_content, octstr_imm("\r\n")); octstr_append(push_content, octstr_imm("the parser should discard this")); } octstr_destroy(bcos); octstr_destroy(pap_content); octstr_destroy(wap_content); } return push_content; }
int mms_mmbox_modmsg(char *mmbox_root, char *user, Octstr *msgref, Octstr *state, List *flag_cmds) { Octstr *sdf = octstr_duplicate(msgref); Octstr *fname = NULL, *ftmp = NULL; Octstr *home = user_mmbox_dir(mmbox_root,user); Octstr *s = NULL; List *flags = NULL; Octstr *nstate = NULL; int ifd = -1, nifd, tmpfd = -1; MmsMsg *m = NULL; int res = -1; int msize; octstr_replace(sdf, octstr_imm("-"), octstr_imm("/")); if (!home) goto done; ifd = open_mmbox_index(octstr_get_cstr(home),1); if (ifd < 0) goto done; fname = octstr_format("%S/%S", home, sdf); s = octstr_read_file(octstr_get_cstr(fname)); if ( s == NULL || octstr_len(s) == 0) { error(0, "mmbox.mod: failed to read data file [%s] - %s!", octstr_get_cstr(fname), strerror(errno)); goto done; } m = mms_frombinary(s, octstr_imm("anon@anon")); if (!m) { error(0, "mmbox.mod: failed to read data file [%s]!", octstr_get_cstr(fname)); goto done; } if (state == NULL) nstate = mms_get_header_value(m, octstr_imm("X-Mms-MM-State")); else { nstate = octstr_duplicate(state); mms_replace_header_value(m, "X-Mms-MM-State", octstr_get_cstr(nstate)); } flags = mms_get_header_values(m, octstr_imm("X-Mms-MM-Flags")); flags = make_mm_flags(flags, flag_cmds); mms_replace_header_values(m, "X-Mms-MM-Flags", flags); ftmp = octstr_format("%S.%ld.%d", fname, time(NULL), getpid()); tmpfd = open(octstr_get_cstr(ftmp), O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); if (tmpfd < 0) goto done; s = mms_tobinary(m); msize = octstr_len(s); octstr_write_to_socket(tmpfd, s); rename(octstr_get_cstr(ftmp), octstr_get_cstr(fname)); unlock_and_close(tmpfd); if ((nifd = update_mmbox_index(ifd, octstr_get_cstr(home), ITEM_MOD, sdf, nstate, flags, msize)) < 0) { /* Not good, we wrote but could not update the index file. scream. */ error(0, "mmbox.mod: failed to update index file, home is %s!", octstr_get_cstr(home)); goto done; } ifd = nifd; res = 0; done: if (ifd > 0) unlock_and_close(ifd); if (fname) octstr_destroy(fname); if (ftmp) octstr_destroy(ftmp); if (sdf) octstr_destroy(sdf); if (s) octstr_destroy(s); if (home) octstr_destroy(home); if (nstate) octstr_destroy(nstate); if (flags) gwlist_destroy(flags, (gwlist_item_destructor_t *)octstr_destroy); if (m) mms_destroy(m); return res; }
/* * Attempt to read an envelope from queue file: * - opens and locks the file. * - if the lock succeeds, check that file hasn't changed since opening. If it has * return NULL (i.e. file is being processed elsewhere -- race condition), otherwise read it. * - If should block is 1, then does a potentially blocking attempt to lock the file. */ static MmsEnvelope *mms_queue_readenvelope(char *qf, char *mms_queuedir, int shouldblock) { Octstr *fname; int fd; Octstr *qdata, *s; ParseContext *p; MmsEnvelope *e; int okfile = 0; char subdir[64]; char realqf[QFNAMEMAX]; char xqf[QFNAMEMAX+64]; struct qfile_t *qfs; get_subdir(qf, subdir, realqf); /* break it down... */ fname = octstr_format( "%.128s/%s%s", mms_queuedir, subdir, realqf); strncpy(xqf, octstr_get_cstr(fname), sizeof xqf); #ifdef SunOS if ((fd = open(octstr_get_cstr(fname), O_RDWR)) < 0) { #else if ((fd = open(octstr_get_cstr(fname), O_RDONLY)) < 0) { #endif debug("",0,"mms_queue_readenvelope: could not open file %s", octstr_get_cstr(fname)); octstr_destroy(fname); return NULL; } else if (mm_lockfile(fd, octstr_get_cstr(fname), shouldblock) != 0) { debug("",0,"mms_queue_readenvelope: could not lock file %s", octstr_get_cstr(fname)); unlock_and_close(fd); octstr_destroy(fname); return NULL; } debug("",0,"locked and opened file: %s", octstr_get_cstr(fname)); e = mms_queue_create_envelope(NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, qf, NULL, sizeof (struct qfile_t), NULL); qfs = e->qfs_data; qfs->fd = fd; strncpy(qfs->name, realqf, sizeof qfs->name); strncpy(qfs->subdir, subdir, sizeof qfs->subdir); strncpy(qfs->dir, mms_queuedir, sizeof qfs->dir); qdata = octstr_read_file(octstr_get_cstr(fname)); octstr_destroy(fname); if (qdata == NULL) qdata = octstr_imm(""); p = parse_context_create(qdata); for (s = parse_get_line(p); s; s = parse_get_line(p)) { char *line = octstr_get_cstr(s); int ch = line[0]; char *res = line + 1; char *ptmp; switch (ch) { Octstr *t; MmsEnvelopeTo *to; case 'T': t = octstr_create(res); e->msgtype = mms_string_to_message_type(t); octstr_destroy(t); if (e->msgtype < 0) { e->msgtype = 0; error(0, "mms_queueread: Unknown MMS message type (%s) in file %s, skipped!\n", res, xqf); } break; case 'I': e->msgId = octstr_create(res); break; case 'i': strncpy(e->src_interface, res, sizeof e->src_interface); break; case 'F': e->from = octstr_create(res); if (mms_validate_address(e->from) != 0) { warning(0, "mms_queueread: Mal-formed address [%s] in file %s! " "Attempting fixup.", res, xqf); _mms_fixup_address(&e->from, NULL, NULL, 1); } break; case 'R': t = octstr_create(res); if (mms_validate_address(t) != 0) { warning(0, "mms_queueread: Mal-formed address [%s] in file %s! " "Attempting fixup.", res, xqf); _mms_fixup_address(&t, NULL, NULL, 1); } to = gw_malloc(sizeof *to); to->rcpt = t; to->process = 1; gwlist_append(e->to, to); break; case 'C': e->created = atol(res); break; case 'L': e->lasttry = atol(res); break; case 'D': e->sendt = atol(res); break; case 'X': e->expiryt = atol(res); break; case 'N': e->attempts = atol(res); break; case 'P': e->fromproxy = octstr_create(res); break; case 'M': e->mdata = octstr_create(res); break; case 'p': e->viaproxy = octstr_create(res); break; case 'S': e->msize = atol(res); break; case 's': e->subject = octstr_create(res); break; case 't': e->token = octstr_create(res); break; case 'f': e->lastaccess = atol(res); break; case 'b': e->bill.billed = 1; e->bill.amt = atof(res); break; case 'r': e->dlr = 1; break; case 'V': e->vaspid = octstr_create(res); break; case 'v': e->vasid = octstr_create(res); break; case 'U': e->url1 = octstr_create(res); break; case 'u': e->url2 = octstr_create(res); break; case 'H': if (e->hdrs == NULL) e->hdrs = http_create_empty_headers(); if ((ptmp = index(res, ':')) == NULL) error(0, "Incorrectly formatted line %s in queue file %s!", line, xqf); else { char *value = ptmp + 1; char hname[512]; int xlen = (ptmp - res < sizeof hname) ? ptmp - res : -1 + sizeof hname; strncpy(hname, res, xlen); hname[xlen] = 0; /* terminate it. */ http_header_add(e->hdrs, hname, value); } break; case '.': okfile = 1; break; default: error(0, "Unknown QF header %c in file %s!", ch, xqf); break; } octstr_destroy(s); if (okfile) break; /* We are done. */ } parse_context_destroy(p); octstr_destroy(qdata); /* We should properly validate the queue file here. */ if (!okfile) { free_envelope(e,0); e = NULL; error(0, "Corrupt queue control file: %s", xqf); } return e; } /* Updates envelope to queue file: * - opens temp file * - writes output to temp file, if not new else writes directly. * - renames temp file to queue file (if not new) * This function doesn't check that this envelope is useless (i.e. no recipients) * - If function returns -1, caller should check errno for error. */ static int writeenvelope(MmsEnvelope *e, int newenv) { Octstr *tfname = NULL; char *s; char buf[512]; int fd; int i, n; int res = 0; struct qfile_t *qfs = e ? e->qfs_data : NULL; gw_assert(e); if (newenv) fd = qfs->fd; else { tfname = octstr_format( "%s/%s%c%s.%d", qfs->dir, qfs->subdir, MTF, qfs->name + 1, random()); fd = open(octstr_get_cstr(tfname), O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); if (fd < 0 ) { error(0, "mms_queueadd: Failed to open temp file %s: error = %s\n", octstr_get_cstr(tfname), strerror(errno)); res = -1; goto done; } else if (mm_lockfile(fd, octstr_get_cstr(tfname), 0) != 0) { /* Lock it. */ error(0, "mms_queueadd: Failed lock temp file %s: error = %s\n", octstr_get_cstr(tfname), strerror(errno)); res = -1; goto done; } } /* Write out. */ s = (char *)mms_message_type_to_cstr(e->msgtype); if (!s) { error(0, "mms_queuewrite: Unknown MMS message type %d! Skipped\n", e->msgtype); s = ""; } _putline(fd, "T", s); if (e->msgId) _putline(fd, "I", octstr_get_cstr(e->msgId)); if (e->src_interface[0]) _putline(fd, "i", e->src_interface); if (e->from) _putline(fd, "F", octstr_get_cstr(e->from)); if (e->to) n = gwlist_len(e->to); else n = 0; for (i = 0; i < n; i++) { MmsEnvelopeTo *to = gwlist_get(e->to, i); if (to->process) _putline(fd, "R", octstr_get_cstr(to->rcpt)); } /* Output headers if any. */ n = (e->hdrs) ? gwlist_len(e->hdrs) : 0; for (i = 0; i < n; i++) { Octstr *h = NULL, *v = NULL; http_header_get(e->hdrs, i, &h, &v); if (h && v) { Octstr *x = octstr_format("%s:%s", octstr_get_cstr(h), octstr_get_cstr(v)); _putline(fd, "H", octstr_get_cstr(x)); octstr_destroy(x); } if (h) octstr_destroy(h); if (v) octstr_destroy(v); } sprintf(buf, "%ld", e->created); _putline(fd, "C", buf); if (e->lasttry) { sprintf(buf, "%ld", e->lasttry); _putline(fd, "L", buf); } if (e->sendt) { sprintf(buf, "%ld", e->sendt); _putline(fd, "D", buf); } if (e->expiryt) { sprintf(buf, "%ld", e->expiryt); _putline(fd, "X", buf); } if (e->attempts) { sprintf(buf, "%ld", e->attempts); _putline(fd, "N", buf); } if (e->lastaccess) { sprintf(buf, "%ld", e->lastaccess); _putline(fd, "f", buf); } sprintf(buf, "%ld", e->msize); _putline(fd, "S", buf); if (e->fromproxy) _putline(fd, "P", octstr_get_cstr(e->fromproxy)); if (e->mdata) _putline(fd, "M", octstr_get_cstr(e->mdata)); if (e->subject) _putline(fd, "s", octstr_get_cstr(e->subject)); if (e->viaproxy) _putline(fd, "p", octstr_get_cstr(e->viaproxy)); if (e->token) _putline(fd, "t", octstr_get_cstr(e->token)); if (e->vaspid) _putline(fd, "V", octstr_get_cstr(e->vaspid)); if (e->vasid) _putline(fd, "v", octstr_get_cstr(e->vasid)); if (e->url1) _putline(fd, "U", octstr_get_cstr(e->url1)); if (e->url2) _putline(fd, "u", octstr_get_cstr(e->url2)); if (e->dlr) _putline(fd, "r", "Yes"); if (e->bill.billed) { sprintf(buf, "%.3f", e->bill.amt); _putline(fd,"b", buf); } _putline(fd, "", "."); fsync(fd); /* Sync data. */ if (!newenv) { /* An update */ Octstr *qfname; qfname = octstr_format("%s/%s%s", qfs->dir, qfs->subdir, qfs->name); if (rename(octstr_get_cstr(tfname), octstr_get_cstr(qfname)) < 0) { error(0, "mms_queuewrite: Failed to rename %s to %s: error = %s\n", octstr_get_cstr(qfname), octstr_get_cstr(tfname), strerror(errno)); unlock_and_close(fd); /* Close new one, keep old one. */ res = -1; } else { /* On success, new descriptor replaces old one and we close old one. */ unlock_and_close(qfs->fd); qfs->fd = fd; } octstr_destroy(qfname); } done: octstr_destroy(tfname); return res; }
int main(int argc, char **argv) { int i, opt, num_threads; Octstr *proxy; List *exceptions; long proxy_port; int proxy_ssl = 0; Octstr *proxy_username; Octstr *proxy_password; Octstr *exceptions_regex; char *p; long threads[MAX_THREADS]; time_t start, end; double run_time; FILE *fp; int ssl = 0; gwlib_init(); proxy = NULL; proxy_port = -1; exceptions = gwlist_create(); proxy_username = NULL; proxy_password = NULL; exceptions_regex = NULL; num_threads = 1; file = 0; fp = NULL; while ((opt = getopt(argc, argv, "hv:qr:p:P:Se:t:i:a:u:sc:H:B:m:f")) != EOF) { switch (opt) { case 'v': log_set_output_level(atoi(optarg)); break; case 'q': verbose = 0; break; case 'r': max_requests = atoi(optarg); break; case 't': num_threads = atoi(optarg); if (num_threads > MAX_THREADS) num_threads = MAX_THREADS; break; case 'i': interval = atof(optarg); break; case 'u': file = 1; fp = fopen(optarg, "a"); if (fp == NULL) panic(0, "Cannot open message text file %s", optarg); msg_text = octstr_read_file(optarg); if (msg_text == NULL) panic(0, "Cannot read message text"); debug("", 0, "message text is"); octstr_dump(msg_text, 0); octstr_url_encode(msg_text); fclose(fp); break; case 'h': help(); exit(0); case 'p': proxy = octstr_create(optarg); break; case 'P': proxy_port = atoi(optarg); break; case 'S': proxy_ssl = 1; break; case 'e': p = strtok(optarg, ":"); while (p != NULL) { gwlist_append(exceptions, octstr_create(p)); p = strtok(NULL, ":"); } break; case 'E': exceptions_regex = octstr_create(optarg); break; case 'a': p = strtok(optarg, ":"); if (p != NULL) { auth_username = octstr_create(p); p = strtok(NULL, ""); if (p != NULL) auth_password = octstr_create(p); } break; case 's': ssl = 1; break; case 'c': octstr_destroy(ssl_client_certkey_file); ssl_client_certkey_file = octstr_create(optarg); break; case 'H': fp = fopen(optarg, "a"); if (fp == NULL) panic(0, "Cannot open header text file %s", optarg); extra_headers = octstr_read_file(optarg); if (extra_headers == NULL) panic(0, "Cannot read header text"); debug("", 0, "headers are"); octstr_dump(extra_headers, 0); split_headers(extra_headers, &split); fclose(fp); break; case 'B': content_file = octstr_create(optarg); break; case 'm': method_name = octstr_create(optarg); break; case 'f': follow_redirect = 0; break; case '?': default: error(0, "Invalid option %c", opt); help(); panic(0, "Stopping."); } } if (optind == argc) { help(); exit(0); } #ifdef HAVE_LIBSSL /* * check if we are doing a SSL-enabled client version here * load the required cert and key file */ if (ssl || proxy_ssl) { if (ssl_client_certkey_file != NULL) { use_global_client_certkey_file(ssl_client_certkey_file); } else { panic(0, "client certkey file need to be given!"); } } #endif if (method_name != NULL) { method = http_name2method(method_name); } if (proxy != NULL && proxy_port > 0) { http_use_proxy(proxy, proxy_port, proxy_ssl, exceptions, proxy_username, proxy_password, exceptions_regex); } octstr_destroy(proxy); octstr_destroy(proxy_username); octstr_destroy(proxy_password); octstr_destroy(exceptions_regex); gwlist_destroy(exceptions, octstr_destroy_item); urls = argv + optind; num_urls = argc - optind; time(&start); if (num_threads == 1) client_thread(http_caller_create()); else { for (i = 0; i < num_threads; ++i) threads[i] = gwthread_create(client_thread, http_caller_create()); for (i = 0; i < num_threads; ++i) gwthread_join(threads[i]); } time(&end); run_time = difftime(end, start); info(0, "%ld requests in %f seconds, %f requests/s.", (max_requests * num_threads), run_time, (max_requests * num_threads) / run_time); octstr_destroy(ssl_client_certkey_file); octstr_destroy(auth_username); octstr_destroy(auth_password); octstr_destroy(extra_headers); octstr_destroy(content_file); gwlist_destroy(split, octstr_destroy_item); gwlib_shutdown(); return 0; }