snpp.c
上传用户:mei_mei897
上传日期:2007-01-05
资源大小:82k
文件大小:8k
- /* -------------------------------------------------------------------- */
- /* SMS Client, send messages to mobile phones and pagers */
- /* */
- /* snpp.c */
- /* */
- /* Copyright (C) 1997,1998 Angelo Masci */
- /* */
- /* This library is free software; you can redistribute it and/or */
- /* modify it under the terms of the GNU Library General Public */
- /* License as published by the Free Software Foundation; either */
- /* version 2 of the License, or (at your option) any later version. */
- /* */
- /* This library is distributed in the hope that it will be useful, */
- /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
- /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
- /* Library General Public License for more details. */
- /* */
- /* You should have received a copy of the GNU Library General Public */
- /* License along with this library; if not, write to the Free */
- /* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
- /* */
- /* You can contact the author at this e-mail address: */
- /* */
- /* angelo@styx.demon.co.uk */
- /* */
- /* -------------------------------------------------------------------- */
- /* $Id$
- -------------------------------------------------------------------- */
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include "common.h"
- #include "logfile.h"
- #include "driver.h"
- #include "expect.h"
- #include "sms_error.h"
- #include "sms_resource.h"
- #include "sms_tcpip.h"
- /* -------------------------------------------------------------------- */
- static struct snpp_env
- {
- DRIVER_DEFAULT_ENV def;
- /* Place any extended driver */
- /* variables here */
- char *server_name;
- long server_port;
- } driver_env;
- /* -------------------------------------------------------------------- */
- static RESOURCE resource_list[] =
- {
- { RESOURCE_STRING, "SMS_comms_params", 0, 0, NULL, 0, NULL, 0, &(driver_env.def.comms_params) },
- { RESOURCE_STRING, "SMS_centre_number", 0, 0, NULL, 0, NULL, 0, &(driver_env.def.centre_number) },
- { RESOURCE_NUMERIC, "SMS_baud", 0, 0, NULL, 0, NULL, 0, &(driver_env.def.baud) },
- { RESOURCE_NUMERIC, "SMS_deliver_timeout", 0, 0, NULL, 0, NULL, 30, &(driver_env.def.deliver_timeout) },
- { RESOURCE_NUMERIC, "SMS_timeout", 0, 0, NULL, 0, NULL, 10, &(driver_env.def.timeout) },
- { RESOURCE_NUMERIC, "SMS_write_timeout", 0, 0, NULL, 0, NULL, 10, &(driver_env.def.write_timeout) },
- { RESOURCE_STRING, "SMS_server_name", 0, 1, NULL, 0, "localhost", 0, &(driver_env.server_name) },
- { RESOURCE_NUMERIC, "SMS_server_port", 0, 1, NULL, 0, NULL, 444, &(driver_env.server_port) },
- { RESOURCE_NULL, NULL, 0, 1, NULL, 0, NULL, 0, NULL }
- };
- /* -------------------------------------------------------------------- */
- #define DELIVERTIMEOUT (driver_env.def.deliver_timeout)
- #define TIMEOUT (driver_env.def.timeout)
- #define WRITETIMEOUT (driver_env.def.write_timeout)
- /* -------------------------------------------------------------------- */
- #define FD (driver_env.def.fd)
- /* -------------------------------------------------------------------- */
- static int SNPP_login(void);
- static int SNPP_sendmessage(char *msisdn, char *message);
- static int SNPP_send_disconnect(void);
- static void SNPP_hangup(void);
- static int SNPP_dial(DRIVER_DEFAULT_ENV *env);
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static int SNPP_dial(DRIVER_DEFAULT_ENV *env)
- {
- lprintf(LOG_STANDARD, "Connecting to SNPP Server %s:%ld...n",
- ((struct snpp_env *)env)->server_name,
- ((struct snpp_env *)env)->server_port);
-
- env->fd = TCPIP_connect(((struct snpp_env *)env)->server_name,
- ((struct snpp_env *)env)->server_port);
- if (env->fd < 0)
- { lprintf(LOG_WARNING, "Failed to Connect.n");
- return EDIAL;
- }
- lprintf(LOG_STANDARD, "Connection Established.n");
- return 0;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static int SNPP_login(void)
- {
- char buf[MAX_RESPONSE_BUFSIZE];
- if (expstr(FD, buf, "n", MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
- {
- if (strncmp(buf, "220", 3) == 0)
- {
- lprintf(LOG_STANDARD, "SNPP Service Loginn");
- lprintf(LOG_VERBOSE, "SNPP Response: %s", buf);
- }
- else
- { lprintf(LOG_STANDARD, "SNPP Service Login Failed - Bad responsen");
- SNPP_hangup();
- return ESNPP_NORESPONSE;
- }
- }
- else
- {
- lprintf(LOG_STANDARD, "SNPP Service Login Failedn");
-
- SNPP_hangup();
- return ESNPP_NORESPONSE;
- }
- return 0;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static int SNPP_sendmessage(char *msisdn, char *message)
- {
- char buf[MAX_RESPONSE_BUFSIZE];
- twrite(FD, "PAGE ", sms_strlen("PAGE "), WRITETIMEOUT);
- twrite(FD, msisdn, sms_strlen(msisdn), WRITETIMEOUT);
- twrite(FD, "n", sms_strlen("n"), WRITETIMEOUT);
- if (expstr(FD, buf, "n", MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
- {
- if (strncmp(buf, "250", 3) == 0)
- {
- lprintf(LOG_STANDARD, "Pager ID Acceptedn");
- lprintf(LOG_VERBOSE, "SNPP Response: %s", buf);
- }
- else
- { lprintf(LOG_STANDARD, "Pager ID NOT Acceptedn");
- SNPP_hangup();
- return ESNPP_NONUMBER;
- }
- }
- else
- { lprintf(LOG_STANDARD, "Pager ID NOT Acceptedn");
- SNPP_hangup();
- return ESNPP_NONUMBER;
- }
- twrite(FD, "MESS ", sms_strlen("MESS "), WRITETIMEOUT);
- twrite(FD, message, sms_strlen(message), WRITETIMEOUT);
- twrite(FD, "n", sms_strlen("n"), WRITETIMEOUT);
- if (expstr(FD, buf, "n", MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
- {
- if (strncmp(buf, "250", 3) == 0)
- {
- lprintf(LOG_STANDARD, "Message Acceptedn");
- lprintf(LOG_VERBOSE, "SNPP Response: %s", buf);
- }
- else
- { lprintf(LOG_STANDARD, "Message NOT Acceptedn");
- SNPP_hangup();
- return ESNPP_NOMESSAGE;
- }
- }
- else
- { lprintf(LOG_STANDARD, "Message NOT Acceptedn");
- SNPP_hangup();
- return ESNPP_NOMESSAGE;
- }
- twrite(FD, "SENDn", sms_strlen("SENDn"), WRITETIMEOUT);
- if (expstr(FD, buf, "n", MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
- {
- if (strncmp(buf, "250", 3) == 0)
- {
- lprintf(LOG_STANDARD, "Message Sentn");
- lprintf(LOG_VERBOSE, "SNPP Response: %s", buf);
- }
- else
- { lprintf(LOG_STANDARD, "Message NOT Sentn");
- SNPP_hangup();
- return ESNPP_NODELIVERY;
- }
- }
- else
- { lprintf(LOG_STANDARD, "Message NOT Sentn");
- SNPP_hangup();
- return ESNPP_NODELIVERY;
- }
- return 0;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static int SNPP_send_disconnect(void)
- {
- char buf[MAX_RESPONSE_BUFSIZE];
- twrite(FD, "QUITn", sms_strlen("QUITn"), WRITETIMEOUT);
- if (expstr(FD, buf, "n", MAX_RESPONSE_BUFSIZE, TIMEOUT) == 0)
- {
- if (strncmp(buf, "221", 3) == 0)
- {
- lprintf(LOG_STANDARD, "Sent Diconnectionn");
- lprintf(LOG_VERBOSE, "SNPP Response: %s", buf);
- }
- else
- { lprintf(LOG_STANDARD, "Failed to send Disconnectionn");
- SNPP_hangup();
- return ESNPP_NODISCONNECT;
- }
- }
- else
- { lprintf(LOG_STANDARD, "Failed to send Disconnectionn");
- SNPP_hangup();
- return ESNPP_NODISCONNECT;
- }
- return 0;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static void SNPP_hangup(void)
- { TCPIP_disconnect(FD);
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static void SNPP_wrapper_hangup(DRIVER_DEFAULT_ENV *env)
- { SNPP_hangup();
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- DEVICE_ENTRY snpp_device = {
- "SNPP",
- resource_list,
- (DRIVER_DEFAULT_ENV *)(&driver_env),
- default_init,
- default_main,
- default_validate_always_true,
- SNPP_dial,
- SNPP_wrapper_hangup,
- SNPP_send_disconnect,
- default_single_deliver,
- SNPP_sendmessage,
- SNPP_login
- };