1 /* -------------------------------------------------------------------- */
2 /* SMS Client, send messages to mobile phones and pagers		*/
3 /*									*/
4 /* ucp_tcp.c								*/
5 /*									*/
6 /*  Copyright (C) 1997,1998,1999 Angelo Masci				*/
7 /*									*/
8 /*  This library is free software; you can redistribute it and/or	*/
9 /*  modify it under the terms of the GNU Library General Public		*/
10 /*  License as published by the Free Software Foundation; either	*/
11 /*  version 2 of the License, or (at your option) any later version.	*/
12 /*									*/
13 /*  This library is distributed in the hope that it will be useful,	*/
14 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of	*/
15 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU	*/
16 /*  Library General Public License for more details.			*/
17 /*									*/
18 /*  You should have received a copy of the GNU Library General Public	*/
19 /*  License along with this library; if not, write to the Free		*/
20 /*  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.	*/
21 /*									*/
22 /*  You can contact the author at this e-mail address:			*/
23 /*									*/
24 /*  angelo@styx.demon.co.uk						*/
25 /*									*/
26 /* -------------------------------------------------------------------- */
27 /* $Id$
28    -------------------------------------------------------------------- */
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 
36 #include "common/common.h"
37 #include "logfile/logfile.h"
38 #include "driver.h"
39 #include "error.h"
40 #include "ascii.h"
41 #include "ia5table.h"
42 #include "comms/comms.h"
43 #include "resource/resource.h"
44 
45 /* -------------------------------------------------------------------- */
46 
47 static struct ucp_tcp_env
48 {
49 	DRIVER_DEFAULT_ENV def;
50 
51 	/* Place any extended driver	*/
52 	/* variables here 		*/
53 
54 	char 	*ipaddress,
55 		*password;
56 
57 } driver_env;
58 
59 /* -------------------------------------------------------------------- */
60 
61 static 	RESOURCE resource_list[] =
62 	{
63 		{ RESOURCE_STRING,  "SMS_comms_params", 	0, 0, NULL, 0,  NULL,        0,  &(driver_env.def.comms_params)  	},
64 		{ RESOURCE_STRING,  "SMS_centre_number", 	0, 0, NULL, 0,  NULL,        0,  &(driver_env.def.centre_number)  	},
65 		{ RESOURCE_NUMERIC, "SMS_baud", 		0, 0, NULL, 0,  NULL,        0,  &(driver_env.def.baud)  		},
66 		{ RESOURCE_NUMERIC, "SMS_deliver_timeout", 	0, 0, NULL, 0,  NULL,        30, &(driver_env.def.deliver_timeout)  },
67 		{ RESOURCE_NUMERIC, "SMS_timeout", 		0, 0, NULL, 0,  NULL,        10, &(driver_env.def.timeout)  	},
68 		{ RESOURCE_NUMERIC, "SMS_write_timeout", 	0, 0, NULL, 0,  NULL,        10, &(driver_env.def.write_timeout)  	},
69 
70 		{ RESOURCE_STRING,  "SMS_ipaddress", 		0, 1, NULL, 0,  "", 	    0,    &(driver_env.ipaddress)		},
71 		{ RESOURCE_STRING,  "SMS_password", 		0, 1, NULL, 0,  "", 	    0,    &(driver_env.password)		},
72 
73 		{ RESOURCE_STRING,  "SMS_server_name", 		0, 1, NULL, 0,  "localhost", 0,  &(driver_env.def.server_name)		},
74 		{ RESOURCE_NUMERIC, "SMS_server_port", 		0, 1, NULL, 0,  NULL, 	     500, &(driver_env.def.server_port)		},
75 		{ RESOURCE_NULL,     NULL, 			0, 1, NULL, 0,  NULL,        0,  NULL  				}
76 	};
77 
78 /* -------------------------------------------------------------------- */
79 
80 #define DELIVERTIMEOUT 		(driver_env.def.deliver_timeout)
81 #define TIMEOUT 		(driver_env.def.timeout)
82 #define WRITETIMEOUT 		(driver_env.def.write_timeout)
83 
84 /* -------------------------------------------------------------------- */
85 
86 #define FD			(driver_env.def.fd)
87 
88 /* -------------------------------------------------------------------- */
89 /* -------------------------------------------------------------------- */
90 
91 
92 
93 
94 #include "libucp.c"
95 
96 
97 
98 
99 /* -------------------------------------------------------------------- */
100 /* -------------------------------------------------------------------- */
101 
102 static int UCP_TCP_init(char *mservice, DEVICE_ENTRY *device);
103 static int UCP_TCP_login(void);
104 static int UCP_TCP_sendmessage(char *msisdn, char *message);
105 static void UCP_TCP_hangup(void);
106 
107 
108 /* -------------------------------------------------------------------- */
109 /* -------------------------------------------------------------------- */
UCP_TCP_init(char * mservice,DEVICE_ENTRY * device)110 static int UCP_TCP_init(char *mservice, DEVICE_ENTRY *device)
111 {
112 	char	*ptr;
113 	int 	addr[4];
114 
115 
116 	if ( default_init(mservice, device) == -1 )
117 	{	return -1;
118 	}
119 
120 
121         if ( sscanf(driver_env.ipaddress, "%3d.%3d.%3d.%3d", &addr[0], &addr[1], &addr[2], &addr[3]) == 4 )
122 	{
123 		sprintf(driver_env.ipaddress, "%3.3d%3.3d%3.3d%3.3d", addr[0], addr[1], addr[2], addr[3]);
124 	}
125 	else
126 	{	lprintf(LOG_ERROR, "IP Address in incorrect format\n");
127 		return -1;
128 	}
129 
130     /* ------------------------------------ */
131     /* Convert password to ia5 format       */
132     /* ------------------------------------ */
133 
134     ptr = driver_env.password;
135     while (*ptr != '\0')
136     {
137         *ptr = toia5(*ptr);
138         ptr++;
139     }
140 
141 	return 0;
142 }
143 
144 
145 /* -------------------------------------------------------------------- */
146 /* -------------------------------------------------------------------- */
UCP_TCP_login(void)147 static int UCP_TCP_login(void)
148 {
149     char   buf[MAX_RESPONSE_BUFSIZE],
150            *ucpMessage,
151            *ptr;
152 
153     int    result;
154 
155 
156     /* ------------------------------------ */
157     /* Convert password to ia5 format       */
158     /* ------------------------------------ */
159 
160     ptr = driver_env.password;
161     while (*ptr != '\0')
162     {
163         *ptr = toia5(*ptr);
164         ptr++;
165     }
166 
167     /* ------------------------------------ */
168 
169 
170     ucpMessage = UCP_CallInputOp60( driver_env.ipaddress, driver_env.password );
171 
172     twrite(FD, ucpMessage, strlen(ucpMessage), WRITETIMEOUT);
173 
174     if( expstr(FD, buf, "\03", MAX_RESPONSE_BUFSIZE, DELIVERTIMEOUT) == 0)
175     {
176         lprintf(LOG_STANDARD, "SMSC Login Respsonse: %s\n", buf);
177 
178         result = UCP_parse_response(buf);
179         if     (result == 0)
180         {
181             lprintf(LOG_STANDARD, "Login accepted\n");
182         }
183         else if(result == 1)
184         {
185             lprintf(LOG_ERROR, "Login rejected\n");
186             UCP_TCP_hangup();
187             return EUCP_ACKFAILED;
188         }
189         else
190         {
191             lprintf(LOG_ERROR, "Bad Login acknowledgement\n");
192             UCP_TCP_hangup();
193             return EUCP_BADACK;
194         }
195      }
196      else
197      {
198         lprintf(LOG_ERROR, "No Login Response\n");
199         UCP_TCP_hangup();
200         return EUCP_NORESPONSE;
201      }
202 
203      return 0;
204 }
205 
206 
207 /* -------------------------------------------------------------------- */
208 /* -------------------------------------------------------------------- */
UCP_TCP_sendmessage(char * msisdn,char * message)209 static int UCP_TCP_sendmessage(char *msisdn, char *message)
210 {
211     char   buf[MAX_RESPONSE_BUFSIZE],
212            *ucpMessage,
213            *msg;
214 
215 
216     int    result;
217 
218     /* ------------------------------------ */
219     /* Convert message to ia5 format        */
220     /* This should be done prior to         */
221     /* entering this function.              */
222     /* ------------------------------------ */
223 
224     msg = message;
225     while (*msg != '\0')
226     {
227         *msg = toia5(*msg);
228         msg++;
229     }
230 
231     /* ------------------------------------ */
232 
233 #if 0
234     ucpMessage = UCP_CallInputOp01(msisdn, message);
235 #else
236     ucpMessage = UCP_CallInputOp51(msisdn, message, driver_env.ipaddress);
237 #endif
238 
239     twrite(FD, ucpMessage, strlen(ucpMessage), WRITETIMEOUT);
240 
241     if( expstr(FD, buf, "\03", MAX_RESPONSE_BUFSIZE, DELIVERTIMEOUT) == 0)
242     {
243         lprintf(LOG_STANDARD, "SMSC Respsonse: %s\n", buf);
244 
245         result = UCP_parse_response(buf);
246         if     (result == 0)
247         {
248             lprintf(LOG_STANDARD, "Message accepted\n");
249         }
250         else if(result == 1)
251         {
252             lprintf(LOG_ERROR, "Message rejected\n");
253             UCP_TCP_hangup();
254             return EUCP_ACKFAILED;
255         }
256         else
257         {
258             lprintf(LOG_ERROR, "Bad message acknowledgement\n");
259             UCP_TCP_hangup();
260             return EUCP_BADACK;
261         }
262      }
263      else
264      {
265         lprintf(LOG_ERROR, "No Message Response\n");
266         UCP_TCP_hangup();
267         return EUCP_NORESPONSE;
268      }
269 
270      return 0;
271 }
272 
273 /* -------------------------------------------------------------------- */
274 /* -------------------------------------------------------------------- */
UCP_TCP_hangup(void)275 static void UCP_TCP_hangup(void)
276 {	TCPIP_disconnect(FD);
277 }
278 
279 /* -------------------------------------------------------------------- */
280 /* -------------------------------------------------------------------- */
281 DEVICE_ENTRY ucp_tcp_device = {
282 
283 	"UCP_TCP",
284 	"1.0",
285 	resource_list,
286 	(DRIVER_DEFAULT_ENV *)(&driver_env),
287 
288 	UCP_TCP_init,
289 	default_main,
290 	default_validate_always_true,
291 	default_tcpip_connect,
292 	default_tcpip_disconnect,
293         default_send_disconnect,
294 	default_single_deliver,
295 	UCP_TCP_sendmessage,
296 	UCP_TCP_login
297 };
298 
299 
300 
301 
302