?? smsc_fake.c
字號:
/* ==================================================================== * The Kannel Software License, Version 1.0 * * Copyright (c) 2001-2005 Kannel Group * Copyright (c) 1998-2001 WapIT Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Kannel Group (http://www.kannel.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Kannel" and "Kannel Group" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please * contact org@kannel.org. * * 5. Products derived from this software may not be called "Kannel", * nor may "Kannel" appear in their name, without prior written * permission of the Kannel Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Kannel Group. For more information on * the Kannel Group, please see <http://www.kannel.org/>. * * Portions of this software are based upon software originally written at * WapIT Ltd., Helsinki, Finland for the Kannel project. */ /* * smsc_fake.c - interface to fakesmsc.c * * Uoti Urpala 2001 *//* Doesn't support multi-send * Doesn't warn about unrecognized configuration variables */#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#include <errno.h>#include <time.h>#include <limits.h>#include "gwlib/gwlib.h"#include "smscconn.h"#include "smscconn_p.h"#include "bb_smscconn_cb.h"#include "msg.h"#include "sms.h"#include "dlr.h"typedef struct privdata { List *outgoing_queue; long connection_thread; int shutdown; /* Signal to the connection thread to shut down */ int listening_socket; /* File descriptor */ int port; /* Port number to listen */ Octstr *allow_ip, *deny_ip;} PrivData;static int fake_open_connection(SMSCConn *conn, PrivData *privdata){ int s; if ((s = make_server_socket(privdata->port, (conn->our_host ? octstr_get_cstr(conn->our_host) : NULL))) == -1) { error(0, "smsc_fake: could not create listening socket in port %d", privdata->port); return -1; } if (socket_set_blocking(s, 0) == -1) { error(0, "smsc_fake: couldn't make listening socket port %d non-blocking", privdata->port); return -1; } privdata->listening_socket = s; return 0;}static int sms_to_client(Connection *client, Msg *msg){ Octstr *line; Octstr *msgdata = NULL; /* NULL to allow octstr_destroy */ char *contents; int len; debug("bb.sms", 0, "smsc_fake: sending message to client");// msg_dump(msg, 0); line = octstr_duplicate(msg->sms.sender); octstr_append_char(line, ' '); octstr_append(line, msg->sms.receiver); if (octstr_len(msg->sms.udhdata)) { octstr_append(line, octstr_imm(" udh ")); msgdata = octstr_duplicate(msg->sms.udhdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); octstr_destroy(msgdata); octstr_append_char(line, ' '); msgdata = octstr_duplicate(msg->sms.msgdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); } else { contents = octstr_get_cstr(msg->sms.msgdata); len = octstr_len(msg->sms.msgdata); while (len > 0) { len--; if (contents[len] < 32 || contents[len] > 126) { octstr_append(line, octstr_imm(" data ")); msgdata = octstr_duplicate(msg->sms.msgdata); octstr_url_encode(msgdata); octstr_append(line, msgdata); goto notelse; /* C lacks "else" clause for while loops */ } } octstr_append(line, octstr_imm(" text ")); octstr_append(line, msg->sms.msgdata); }notelse: octstr_append_char(line, 10); if (conn_write(client, line) == -1) { octstr_destroy(msgdata); octstr_destroy(line); return -1; } octstr_destroy(msgdata); octstr_destroy(line); return 1;}static void msg_to_bb(SMSCConn *conn, Octstr *line){ long p, p2; Msg *msg; Octstr *type = NULL; /* might be destroyed after error before created */ msg = msg_create(sms); p = octstr_search_char(line, ' ', 0); if (p == -1) goto error; msg->sms.sender = octstr_copy(line, 0, p); p2 = octstr_search_char(line, ' ', p + 1); if (p2 == -1) goto error; msg->sms.receiver = octstr_copy(line, p + 1, p2 - p - 1); p = octstr_search_char(line, ' ', p2 + 1); if (p == -1) goto error; type = octstr_copy(line, p2 + 1, p - p2 - 1); if (!octstr_compare(type, octstr_imm("text"))) msg->sms.msgdata = octstr_copy(line, p + 1, LONG_MAX); else if (!octstr_compare(type, octstr_imm("data"))) { msg->sms.msgdata = octstr_copy(line, p + 1, LONG_MAX); if (octstr_url_decode(msg->sms.msgdata) == -1) warning(0, "smsc_fake: urlcoded data from client looks malformed"); } else if (!octstr_compare(type, octstr_imm("route"))) { p2 = octstr_search_char(line, ' ', p + 1); if (p2 == -1) goto error; msg->sms.boxc_id = octstr_copy(line, p + 1, p2 - p - 1); msg->sms.msgdata = octstr_copy(line, p2 + 1, LONG_MAX); } else if (!octstr_compare(type, octstr_imm("udh"))) { p2 = octstr_search_char(line, ' ', p + 1); if (p2 == -1) goto error; msg->sms.udhdata = octstr_copy(line, p + 1, p2 - p - 1); msg->sms.msgdata = octstr_copy(line, p2 + 1, LONG_MAX); if (msg->sms.coding == DC_UNDEF) msg->sms.coding = DC_8BIT;; if (octstr_url_decode(msg->sms.msgdata) == -1 || octstr_url_decode(msg->sms.udhdata) == -1) warning(0, "smsc_fake: urlcoded data from client looks malformed"); } else if (!octstr_compare(type, octstr_imm("dlr-mask"))) { Octstr *tmp; p2 = octstr_search_char(line, ' ', p + 1); if (p2 == -1) goto error; tmp = octstr_copy(line, p + 1, p2 - p - 1); msg->sms.dlr_mask = atoi(octstr_get_cstr(tmp)); octstr_destroy(tmp); msg->sms.msgdata = octstr_copy(line, p2 + 1, LONG_MAX); } else goto error; octstr_destroy(line); octstr_destroy(type); time(&msg->sms.time); msg->sms.smsc_id = octstr_duplicate(conn->id); debug("bb.sms", 0, "smsc_fake: new message received");// msg_dump(msg, 0); bb_smscconn_receive(conn, msg); return;error: warning(0, "smsc_fake: invalid message syntax from client, ignored"); msg_destroy(msg); octstr_destroy(line); octstr_destroy(type); return;}static void main_connection_loop(SMSCConn *conn, Connection *client){ PrivData *privdata = conn->data; Octstr *line; Msg *msg; double delay = 0; if (conn->throughput > 0) { delay = 1.0 / conn->throughput; } while (1) { while (!conn->is_stopped && !privdata->shutdown && (line = conn_read_line(client))) msg_to_bb(conn, line); if (conn_error(client)) goto error; if (conn_eof(client)) goto eof; while ((msg = gwlist_extract_first(privdata->outgoing_queue)) != NULL) { if (sms_to_client(client, msg) == 1) { /* * Now look for the DLR entry and pass it to the upper layer. * There is no *real* DLR awaited from the fakesmsc. */ if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask)) { Msg *dlrmsg; Octstr *tmp; int dlrstat = DLR_SUCCESS; char id[UUID_STR_LEN + 1]; uuid_unparse(msg->sms.id, id); tmp = octstr_create(id); dlrmsg = dlr_find(conn->id, tmp, /* smsc message id */ msg->sms.receiver, /* destination */ dlrstat); if (dlrmsg != NULL) { bb_smscconn_receive(conn, dlrmsg); } else { error(0,"smsc_fale: got DLR but could not find message or was not interested in it"); } octstr_destroy(tmp); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -