⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsc_http.c

📁 WAP gateway, Linux source
💻 C
📖 第 1 页 / 共 5 页
字号:
static void wapme_smsproxy_send_sms(SMSCConn *conn, Msg *sms){    ConnData *conndata = conn->data;    Octstr *url;    List *headers;    url = octstr_format("%S?command=forward&smsText=%E&phoneNumber=%E"                        "&serviceNumber=%E&smsc=%E",                        conndata->send_url,                        sms->sms.msgdata, sms->sms.sender, sms->sms.receiver,                        sms->sms.smsc_id);    headers = gwlist_create();    debug("smsc.http.wapme", 0, "HTTP[%s]: Start request",          octstr_get_cstr(conn->id));    http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers,                        NULL, 0, sms, NULL);    octstr_destroy(url);    http_destroy_headers(headers);}static void wapme_smsproxy_parse_reply(SMSCConn *conn, Msg *msg, int status,			       List *headers, Octstr *body){    if (status == HTTP_OK || status == HTTP_ACCEPTED) {        bb_smscconn_sent(conn, msg, NULL);    } else {        bb_smscconn_send_failed(conn, msg,	            SMSCCONN_FAILED_MALFORMED, octstr_duplicate(body));    }}/* * static void wapme_smsproxy_receive_sms(SMSCConn *conn, HTTPClient *client, *                                List *headers, Octstr *body, List *cgivars) * * The HTTP server for MO messages will act with the same interface as smsbox's  * sendsms interface, so that the logical difference is hidden and SMS Proxy  * can act transparently. So there is no need for an explicite implementation * here. *//*---------------------------------------------------------------- * (Semi-)generic HTTP interface * * This 'generic' type will handle the 'send-url' directive in the  * group the same way the 'sms-service' for smsbox does, via  * URLTranslation. Response interpretation is based on the three * regex value that match against the reponse body. The HTTP reponse * code is not obeyed. *  * It handles mainly MT messages, due to the fact that MO traffic * can't be abstracted in a universal way. Therefor we use the * Kannel sendsms interface layout as generic fallback. So if your * SMSC provider needs to send MO messages, he needs to implement * the Kannel sendsms HTTP interface variables. *  * Example config group: *  *  group = smsc *  smsc = http *  system-type = generic *  send-url = "http://<foobar>/<uri>?from=%P&to=%p&text=%b" *  status-success-regex = "ok" *  status-permfail-regex = "failure" *  status-tempfail-regex = "retry later" *  * Note that neither 'smsc-username' nor 'smsc-password' is required, * since they are coded into the the 'send-url' value directly.  *  * Stipe Tolj <st@tolj.org> */static void generic_send_sms(SMSCConn *conn, Msg *sms){    ConnData *conndata = conn->data;    Octstr *url;    List *headers;    /* We use the escape code population function from our     * URLTranslation module to fill in the appropriate values     * into the URL scheme. */    url = urltrans_fill_escape_codes(conndata->send_url, sms);    headers = gwlist_create();    debug("smsc.http.generic", 0, "HTTP[%s]: Sending request <%s>",          octstr_get_cstr(conn->id), octstr_get_cstr(url));    http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers,                        NULL, 0, sms, NULL);    octstr_destroy(url);    http_destroy_headers(headers);}static void generic_parse_reply(SMSCConn *conn, Msg *msg, int status,                                List *headers, Octstr *body){    ConnData *conndata = conn->data;    size_t n_match = 1;    regmatch_t p_match[10];        /*      * Our generic type checks only content on the HTTP reponse body.     * We use the pre-compiled regex to match against the states.     * This is the most generic criteria (at the moment).      */    if ((conndata->success_regex != NULL) &&         (gw_regex_exec(conndata->success_regex, body, n_match, p_match, 0) == 0)) {        bb_smscconn_sent(conn, msg, NULL);    }     else if ((conndata->permfail_regex != NULL) &&                (gw_regex_exec(conndata->permfail_regex, body, n_match, p_match, 0) == 0)) {        error(0, "HTTP[%s]: Message not accepted.", octstr_get_cstr(conn->id));        bb_smscconn_send_failed(conn, msg,            SMSCCONN_FAILED_MALFORMED, octstr_duplicate(body));    }    else if ((conndata->tempfail_regex != NULL) &&                (gw_regex_exec(conndata->tempfail_regex, body, n_match, p_match, 0) == 0)) {        warning(0, "HTTP[%s]: Message temporary not accepted, will retry.",                 octstr_get_cstr(conn->id));        bb_smscconn_send_failed(conn, msg,            SMSCCONN_FAILED_TEMPORARILY, octstr_duplicate(body));    }    else {        error(0, "HTTP[%s]: Message was rejected. SMSC reponse was:",              octstr_get_cstr(conn->id));        octstr_dump(body, 0);        bb_smscconn_send_failed(conn, msg,            SMSCCONN_FAILED_REJECTED, octstr_create("REJECTED"));    }}/*----------------------------------------------------------------- * functions to implement various smscconn operations */static int httpsmsc_send(SMSCConn *conn, Msg *msg){    ConnData *conndata = conn->data;    Msg *sms = msg_duplicate(msg);    double delay = 0;    if (conn->throughput > 0) {        delay = 1.0 / conn->throughput;    }    conndata->open_sends++;    conndata->send_sms(conn, sms);    /* obey throughput speed limit, if any */    if (conn->throughput > 0)        gwthread_sleep(delay);    return 0;}static long httpsmsc_queued(SMSCConn *conn){    ConnData *conndata = conn->data;    return (conndata ? (conn->status != SMSCCONN_DEAD ?             conndata->open_sends : 0) : 0);}static int httpsmsc_shutdown(SMSCConn *conn, int finish_sending){    ConnData *conndata = conn->data;    debug("httpsmsc_shutdown", 0, "HTTP[%s]: Shutting down",          octstr_get_cstr(conn->id));    conn->why_killed = SMSCCONN_KILLED_SHUTDOWN;    conndata->shutdown = 1;    http_close_port(conndata->port);    return 0;}int smsc_http_create(SMSCConn *conn, CfgGroup *cfg){    ConnData *conndata = NULL;    Octstr *type;    long portno;   /* has to be long because of cfg_get_integer */    int ssl = 0;   /* indicate if SSL-enabled server should be used */    Octstr *os;    if (cfg_get_integer(&portno, cfg, octstr_imm("port")) == -1) {        error(0, "HTTP[%s]: 'port' invalid in smsc 'http' record.",              octstr_get_cstr(conn->id));        return -1;    }    if ((type = cfg_get(cfg, octstr_imm("system-type")))==NULL) {        error(0, "HTTP[%s]: 'type' missing in smsc 'http' record.",              octstr_get_cstr(conn->id));        octstr_destroy(type);        return -1;    }    conndata = gw_malloc(sizeof(ConnData));    conndata->http_ref = NULL;    conndata->success_regex =         conndata->permfail_regex = conndata->tempfail_regex = NULL;    conndata->allow_ip = cfg_get(cfg, octstr_imm("connect-allow-ip"));    conndata->send_url = cfg_get(cfg, octstr_imm("send-url"));    conndata->dlr_url = cfg_get(cfg, octstr_imm("dlr-url"));    conndata->username = cfg_get(cfg, octstr_imm("smsc-username"));    conndata->password = cfg_get(cfg, octstr_imm("smsc-password"));    conndata->system_id = cfg_get(cfg, octstr_imm("system-id"));    cfg_get_bool(&conndata->no_sender, cfg, octstr_imm("no-sender"));    cfg_get_bool(&conndata->no_coding, cfg, octstr_imm("no-coding"));    cfg_get_bool(&conndata->no_sep, cfg, octstr_imm("no-sep"));    conndata->proxy = cfg_get(cfg, octstr_imm("system-id"));    if (conndata->send_url == NULL)        panic(0, "HTTP[%s]: Sending not allowed. No 'send-url' specified.",              octstr_get_cstr(conn->id));    if (octstr_case_compare(type, octstr_imm("kannel")) == 0) {        if (conndata->username == NULL || conndata->password == NULL) {            error(0, "HTTP[%s]: 'username' and 'password' required for Kannel http smsc",                  octstr_get_cstr(conn->id));            goto error;        }        conndata->receive_sms = kannel_receive_sms;        conndata->send_sms = kannel_send_sms;        conndata->parse_reply = kannel_parse_reply;    }    else if (octstr_case_compare(type, octstr_imm("brunet")) == 0) {        if (conndata->username == NULL) {            error(0, "HTTP[%s]: 'username' (=CustomerId) required for bruNET http smsc",                  octstr_get_cstr(conn->id));            goto error;        }        conndata->receive_sms = brunet_receive_sms;        conndata->send_sms = brunet_send_sms;        conndata->parse_reply = brunet_parse_reply;    }    else if (octstr_case_compare(type, octstr_imm("xidris")) == 0) {        if (conndata->username == NULL || conndata->password == NULL) {            error(0, "HTTP[%s]: 'username' and 'password' required for Xidris http smsc",                  octstr_get_cstr(conn->id));            goto error;        }        conndata->receive_sms = xidris_receive_sms;        conndata->send_sms = xidris_send_sms;        conndata->parse_reply = xidris_parse_reply;    }    else if (octstr_case_compare(type, octstr_imm("wapme")) == 0) {        if (conndata->username == NULL || conndata->password == NULL) {            error(0, "HTTP[%s]: 'username' and 'password' required for Wapme http smsc",                  octstr_get_cstr(conn->id));            goto error;        }        conndata->receive_sms = kannel_receive_sms; /* emulate sendsms interface */        conndata->send_sms = wapme_smsproxy_send_sms;        conndata->parse_reply = wapme_smsproxy_parse_reply;    }    else if (octstr_case_compare(type, octstr_imm("clickatell")) == 0) {        /* no required data checks here? */        conndata->receive_sms = clickatell_receive_sms;        conndata->send_sms = clickatell_send_sms;        conndata->parse_reply = clickatell_parse_reply;    }    else if (octstr_case_compare(type, octstr_imm("generic")) == 0) {        /* we need at least the criteria for a successfull sent */        if ((os = cfg_get(cfg, octstr_imm("status-success-regex"))) == NULL) {            error(0, "HTTP[%s]: 'status-success-regex' required for generic http smsc",                  octstr_get_cstr(conn->id));            goto error;        }        conndata->receive_sms = kannel_receive_sms; /* emulate sendsms interface */        conndata->send_sms = generic_send_sms;        conndata->parse_reply = generic_parse_reply;        /* pre-compile regex expressions */        if (os != NULL) {   /* this is implicite due to the above if check */            if ((conndata->success_regex = gw_regex_comp(os, REG_EXTENDED)) == NULL)                panic(0, "Could not compile regex pattern '%s'", octstr_get_cstr(os));            octstr_destroy(os);        }        if ((os = cfg_get(cfg, octstr_imm("status-permfail-regex"))) != NULL) {            if ((conndata->permfail_regex = gw_regex_comp(os, REG_EXTENDED)) == NULL)                panic(0, "Could not compile regex pattern '%s'", octstr_get_cstr(os));            octstr_destroy(os);        }        if ((os = cfg_get(cfg, octstr_imm("status-tempfail-regex"))) != NULL) {            if ((conndata->tempfail_regex = gw_regex_comp(os, REG_EXTENDED)) == NULL)                panic(0, "Could not compile regex pattern '%s'", octstr_get_cstr(os));            octstr_destroy(os);        }    }    /*     * ADD NEW HTTP SMSC TYPES HERE     */    else {	error(0, "HTTP[%s]: system-type '%s' unknown smsc 'http' record.",          octstr_get_cstr(conn->id), octstr_get_cstr(type));	goto error;    }	    conndata->open_sends = 0;    conndata->http_ref = http_caller_create();        conn->data = conndata;    conn->name = octstr_format("HTTP:%S", type);    conn->status = SMSCCONN_ACTIVE;    conn->connect_time = time(NULL);    conn->shutdown = httpsmsc_shutdown;    conn->queued = httpsmsc_queued;    conn->send_msg = httpsmsc_send;    if (http_open_port_if(portno, ssl, conn->our_host)==-1)	goto error;    conndata->port = portno;    conndata->shutdown = 0;        if ((conndata->receive_thread =	 gwthread_create(httpsmsc_receiver, conn)) == -1)	goto error;    if ((conndata->send_cb_thread =	 gwthread_create(httpsmsc_send_cb, conn)) == -1)	goto error;    info(0, "HTTP[%s]: Initiated and ready", octstr_get_cstr(conn->id));        octstr_destroy(type);    return 0;error:    error(0, "HTTP[%s]: Failed to create http smsc connection",          octstr_get_cstr(conn->id));    conn->data = NULL;    conndata_destroy(conndata);    conn->why_killed = SMSCCONN_KILLED_CANNOT_CONNECT;    conn->status = SMSCCONN_DEAD;    octstr_destroy(type);    return -1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -