blob: 493d07973def1fbd45541dcf609b5b753f070ea7 [file] [log] [blame]
Harald Weltef1033cc2012-11-08 16:14:37 +01001/* SMPP 3.4 interface, SMSC-side implementation */
2/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20#include <stdio.h>
21#include <unistd.h>
22#include <string.h>
23#include <stdint.h>
24#include <errno.h>
25
26#include <sys/socket.h>
27#include <netinet/in.h>
28
29#include <smpp34.h>
30#include <smpp34_structs.h>
31#include <smpp34_params.h>
32
33#include <osmocom/core/utils.h>
34#include <osmocom/core/socket.h>
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/logging.h>
37#include <osmocom/core/write_queue.h>
38#include <osmocom/core/talloc.h>
39
40#include "smpp_smsc.h"
41
42#include <openbsc/debug.h>
43
Harald Welte4dbcdad2012-11-08 22:12:45 +010044/*! \brief Ugly wrapper. libsmpp34 should do this itself! */
45#define SMPP34_UNPACK(rc, type, str, data, len) \
46 memset(str, 0, sizeof(*str)); \
47 rc = smpp34_unpack(type, str, data, len)
48
Harald Weltef1033cc2012-11-08 16:14:37 +010049enum emse_bind {
50 ESME_BIND_RX = 0x01,
51 ESME_BIND_TX = 0x02,
52};
53
Harald Welte338e3b32012-11-20 22:22:04 +010054struct osmo_smpp_acl *smpp_acl_by_system_id(struct smsc *smsc,
55 const char *sys_id)
56{
57 struct osmo_smpp_acl *acl;
58
59 llist_for_each_entry(acl, &smsc->acl_list, list) {
60 if (!strcmp(acl->system_id, sys_id))
61 return acl;
62 }
63
64 return NULL;
65}
66
67struct osmo_smpp_acl *smpp_acl_alloc(struct smsc *smsc, const char *sys_id)
68{
69 struct osmo_smpp_acl *acl;
70
71 if (strlen(sys_id) > SMPP_SYS_ID_LEN)
72 return NULL;
73
74 if (smpp_acl_by_system_id(smsc, sys_id))
75 return NULL;
76
77 acl = talloc_zero(smsc, struct osmo_smpp_acl);
78 if (!acl)
79 return NULL;
80
81 acl->smsc = smsc;
82 strcpy(acl->system_id, sys_id);
83
84 llist_add(&acl->list, &smsc->acl_list);
85
86 return acl;
87}
88
89void smpp_acl_delete(struct osmo_smpp_acl *acl)
90{
91 struct osmo_esme *esme, *e2;
92 struct smsc *smsc = acl->smsc;
93
94 llist_del(&acl->list);
95
96 llist_for_each_entry_safe(esme, e2, &smsc->esme_list, list) {
97 if (!strcmp(acl->system_id, esme->system_id)) {
98 /* FIXME: drop connection */
99 }
100 }
101
102 talloc_free(acl);
103}
104
105
Harald Weltee94db492012-11-08 20:11:05 +0100106/*! \brief increaes the use/reference count */
107void smpp_esme_get(struct osmo_esme *esme)
108{
109 esme->use++;
110}
111
112static void esme_destroy(struct osmo_esme *esme)
113{
114 osmo_wqueue_clear(&esme->wqueue);
115 if (esme->wqueue.bfd.fd >= 0) {
116 osmo_fd_unregister(&esme->wqueue.bfd);
117 close(esme->wqueue.bfd.fd);
118 }
119 llist_del(&esme->list);
120 talloc_free(esme);
121}
122
123/*! \brief decrease the use/reference count, free if it is 0 */
124void smpp_esme_put(struct osmo_esme *esme)
125{
126 esme->use--;
127 if (esme->use <= 0)
128 esme_destroy(esme);
129}
130
Harald Welted4bdee72012-11-08 19:44:08 +0100131static struct osmo_esme *
132esme_by_system_id(const struct smsc *smsc, char *system_id)
133{
134 struct osmo_esme *e;
135
136 llist_for_each_entry(e, &smsc->esme_list, list) {
137 if (!strcmp(e->system_id, system_id))
138 return e;
139 }
140 return NULL;
141}
Harald Weltef1033cc2012-11-08 16:14:37 +0100142
143
Harald Weltee94db492012-11-08 20:11:05 +0100144/*! \brief initialize the libsmpp34 data structure for a response */
Harald Weltef1033cc2012-11-08 16:14:37 +0100145#define INIT_RESP(type, resp, req) { \
146 memset((resp), 0, sizeof(*(resp))); \
147 (resp)->command_length = 0; \
148 (resp)->command_id = type; \
149 (resp)->command_status = ESME_ROK; \
150 (resp)->sequence_number = (req)->sequence_number; \
151}
152
Harald Weltee94db492012-11-08 20:11:05 +0100153/*! \brief pack a libsmpp34 data strcutrure and send it to the ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100154#define PACK_AND_SEND(esme, ptr) pack_and_send(esme, (ptr)->command_id, ptr)
155static int pack_and_send(struct osmo_esme *esme, uint32_t type, void *ptr)
156{
157 struct msgb *msg = msgb_alloc(4096, "SMPP_Tx");
158 int rc, rlen;
159 if (!msg)
160 return -ENOMEM;
161
162 rc = smpp34_pack(type, msg->tail, msgb_tailroom(msg), &rlen, ptr);
163 if (rc != 0) {
Harald Welte874f9f12012-11-09 13:02:49 +0100164 LOGP(DSMPP, LOGL_ERROR, "[%s] Error during smpp34_pack(): %s\n",
165 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100166 msgb_free(msg);
167 return -EINVAL;
168 }
169 msgb_put(msg, rlen);
170
171 return osmo_wqueue_enqueue(&esme->wqueue, msg);
172}
173
Harald Weltee94db492012-11-08 20:11:05 +0100174/*! \brief transmit a generic NACK to a remote ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100175static int smpp_tx_gen_nack(struct osmo_esme *esme, uint32_t seq, uint32_t status)
176{
177 struct generic_nack_t nack;
Harald Welte874f9f12012-11-09 13:02:49 +0100178 char buf[SMALL_BUFF];
Harald Weltef1033cc2012-11-08 16:14:37 +0100179
180 nack.command_length = 0;
181 nack.command_id = GENERIC_NACK;
182 nack.sequence_number = seq;
183 nack.command_status = status;
184
Harald Welte874f9f12012-11-09 13:02:49 +0100185 LOGP(DSMPP, LOGL_ERROR, "[%s] Tx GENERIC NACK: %s\n",
186 esme->system_id, str_command_status(status, buf));
187
Harald Weltef1033cc2012-11-08 16:14:37 +0100188 return PACK_AND_SEND(esme, &nack);
189}
190
Harald Weltee94db492012-11-08 20:11:05 +0100191/*! \brief retrieve SMPP command ID from a msgb */
Harald Weltef1033cc2012-11-08 16:14:37 +0100192static inline uint32_t smpp_msgb_cmdid(struct msgb *msg)
193{
194 uint8_t *tmp = msgb_data(msg) + 4;
195 return ntohl(*(uint32_t *)tmp);
196}
197
Harald Weltee94db492012-11-08 20:11:05 +0100198/*! \brief retrieve SMPP sequence number from a msgb */
Harald Weltef1033cc2012-11-08 16:14:37 +0100199static inline uint32_t smpp_msgb_seq(struct msgb *msg)
200{
201 uint8_t *tmp = msgb_data(msg);
202 return ntohl(*(uint32_t *)tmp);
203}
204
Harald Weltee94db492012-11-08 20:11:05 +0100205/*! \brief handle an incoming SMPP generic NACK */
Harald Weltef1033cc2012-11-08 16:14:37 +0100206static int smpp_handle_gen_nack(struct osmo_esme *esme, struct msgb *msg)
207{
208 struct generic_nack_t nack;
209 char buf[SMALL_BUFF];
210 int rc;
211
Harald Welte4dbcdad2012-11-08 22:12:45 +0100212 SMPP34_UNPACK(rc, GENERIC_NACK, &nack, msgb_data(msg),
213 msgb_length(msg));
Harald Welte874f9f12012-11-09 13:02:49 +0100214 if (rc < 0) {
215 LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n",
216 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100217 return rc;
Harald Welte874f9f12012-11-09 13:02:49 +0100218 }
Harald Weltef1033cc2012-11-08 16:14:37 +0100219
Harald Welte874f9f12012-11-09 13:02:49 +0100220 LOGP(DSMPP, LOGL_ERROR, "[%s] Rx GENERIC NACK: %s\n",
221 esme->system_id, str_command_status(nack.command_status, buf));
Harald Weltef1033cc2012-11-08 16:14:37 +0100222
223 return 0;
224}
225
Harald Weltee94db492012-11-08 20:11:05 +0100226/*! \brief handle an incoming SMPP BIND RECEIVER */
Harald Weltef1033cc2012-11-08 16:14:37 +0100227static int smpp_handle_bind_rx(struct osmo_esme *esme, struct msgb *msg)
228{
229 struct bind_receiver_t bind;
230 struct bind_receiver_resp_t bind_r;
Harald Welte338e3b32012-11-20 22:22:04 +0100231 struct osmo_smpp_acl *acl;
Harald Weltef1033cc2012-11-08 16:14:37 +0100232 int rc;
233
Harald Welte4dbcdad2012-11-08 22:12:45 +0100234 SMPP34_UNPACK(rc, BIND_RECEIVER, &bind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100235 msgb_length(msg));
Harald Welte874f9f12012-11-09 13:02:49 +0100236 if (rc < 0) {
237 LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n",
238 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100239 return rc;
Harald Welte874f9f12012-11-09 13:02:49 +0100240 }
Harald Weltef1033cc2012-11-08 16:14:37 +0100241
242 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
243
Harald Welte874f9f12012-11-09 13:02:49 +0100244 LOGP(DSMPP, LOGL_INFO, "[%s] Rx BIND Rx from (Version %02x)\n",
Harald Weltef1033cc2012-11-08 16:14:37 +0100245 bind.system_id, bind.interface_version);
246
247 if (bind.interface_version != SMPP_VERSION) {
248 bind_r.command_status = ESME_RSYSERR;
249 goto err;
250 }
251
252 if (esme->bind_flags) {
253 bind_r.command_status = ESME_RALYBND;
254 goto err;
255 }
256
257 esme->smpp_version = bind.interface_version;
258 snprintf(esme->system_id, sizeof(esme->system_id), "%s",
259 bind.system_id);
Harald Weltef1033cc2012-11-08 16:14:37 +0100260
Harald Welte338e3b32012-11-20 22:22:04 +0100261 acl = smpp_acl_by_system_id(esme->smsc, esme->system_id);
262 if (!esme->smsc->accept_all) {
263 if (!acl) {
264 /* This system is unknown */
265 bind_r.command_status = ESME_RINVSYSID;
266 goto err;
267 } else {
268 if (strlen(acl->passwd) &&
269 strcmp(acl->passwd, (char *)bind.password)) {
270 bind_r.command_status = ESME_RINVPASWD;
271 goto err;
272 }
273 esme->acl = acl;
274 if (acl->default_route)
275 esme->smsc->def_route = esme;
276 }
277 }
278
279 esme->bind_flags = ESME_BIND_RX;
Harald Weltef1033cc2012-11-08 16:14:37 +0100280err:
281 return 0;
282}
283
Harald Weltee94db492012-11-08 20:11:05 +0100284/*! \brief handle an incoming SMPP BIND TRANSMITTER */
Harald Weltef1033cc2012-11-08 16:14:37 +0100285static int smpp_handle_bind_tx(struct osmo_esme *esme, struct msgb *msg)
286{
287 struct bind_transmitter_t bind;
288 struct bind_transmitter_resp_t bind_r;
Harald Welte338e3b32012-11-20 22:22:04 +0100289 struct osmo_smpp_acl *acl;
Harald Weltef1033cc2012-11-08 16:14:37 +0100290 struct tlv_t tlv;
291 int rc;
292
Harald Welte4dbcdad2012-11-08 22:12:45 +0100293 SMPP34_UNPACK(rc, BIND_TRANSMITTER, &bind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100294 msgb_length(msg));
295 if (rc < 0) {
Harald Welte874f9f12012-11-09 13:02:49 +0100296 LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n",
297 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100298 return rc;
299 }
300
301 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
302
Harald Welte874f9f12012-11-09 13:02:49 +0100303 LOGP(DSMPP, LOGL_INFO, "[%s] Rx BIND Tx (Version %02x)\n",
Harald Weltef1033cc2012-11-08 16:14:37 +0100304 bind.system_id, bind.interface_version);
305
306 if (bind.interface_version != SMPP_VERSION) {
307 bind_r.command_status = ESME_RSYSERR;
308 goto err;
309 }
310
311 if (esme->bind_flags) {
312 bind_r.command_status = ESME_RALYBND;
313 goto err;
314 }
315
316 esme->smpp_version = bind.interface_version;
317 snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id);
Harald Welte338e3b32012-11-20 22:22:04 +0100318
319 acl = smpp_acl_by_system_id(esme->smsc, esme->system_id);
320 if (!esme->smsc->accept_all) {
321 if (!acl) {
322 /* This system is unknown */
323 bind_r.command_status = ESME_RINVSYSID;
324 goto err;
325 } else {
326 if (strlen(acl->passwd) &&
327 strcmp(acl->passwd, (char *)bind.password)) {
328 bind_r.command_status = ESME_RINVPASWD;
329 goto err;
330 }
331 esme->acl = acl;
332 }
333 }
334
Harald Weltef1033cc2012-11-08 16:14:37 +0100335 esme->bind_flags = ESME_BIND_TX;
336
337 /* build response */
338 snprintf((char *)bind_r.system_id, sizeof(bind_r.system_id), "%s",
339 esme->smsc->system_id);
340
341 /* add interface version TLV */
342 tlv.tag = TLVID_sc_interface_version;
343 tlv.length = sizeof(uint8_t);
344 tlv.value.val16 = esme->smpp_version;
345 build_tlv(&bind_r.tlv, &tlv);
346
347err:
348 return PACK_AND_SEND(esme, &bind_r);
349}
350
Harald Weltee94db492012-11-08 20:11:05 +0100351/*! \brief handle an incoming SMPP BIND TRANSCEIVER */
Harald Weltef1033cc2012-11-08 16:14:37 +0100352static int smpp_handle_bind_trx(struct osmo_esme *esme, struct msgb *msg)
353{
354 struct bind_transceiver_t bind;
355 struct bind_transceiver_resp_t bind_r;
Harald Welte338e3b32012-11-20 22:22:04 +0100356 struct osmo_smpp_acl *acl;
Harald Weltef1033cc2012-11-08 16:14:37 +0100357 int rc;
358
Harald Welte4dbcdad2012-11-08 22:12:45 +0100359 SMPP34_UNPACK(rc, BIND_TRANSCEIVER, &bind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100360 msgb_length(msg));
Harald Welte874f9f12012-11-09 13:02:49 +0100361 if (rc < 0) {
362 LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n",
363 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100364 return rc;
Harald Welte874f9f12012-11-09 13:02:49 +0100365 }
Harald Weltef1033cc2012-11-08 16:14:37 +0100366
367 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
368
Harald Welte874f9f12012-11-09 13:02:49 +0100369 LOGP(DSMPP, LOGL_INFO, "[%s] Rx BIND Trx (Version %02x)\n",
Harald Weltef1033cc2012-11-08 16:14:37 +0100370 bind.system_id, bind.interface_version);
371
372 if (bind.interface_version != SMPP_VERSION) {
373 bind_r.command_status = ESME_RSYSERR;
374 goto err;
375 }
376
377 if (esme->bind_flags) {
378 bind_r.command_status = ESME_RALYBND;
379 goto err;
380 }
381
382 esme->smpp_version = bind.interface_version;
383 snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id);
Harald Welte338e3b32012-11-20 22:22:04 +0100384
385 acl = smpp_acl_by_system_id(esme->smsc, esme->system_id);
386 if (!esme->smsc->accept_all) {
387 if (!acl) {
388 /* This system is unknown */
389 bind_r.command_status = ESME_RINVSYSID;
390 goto err;
391 } else {
392 if (strlen(acl->passwd) &&
393 strcmp(acl->passwd, (char *)bind.password)) {
394 bind_r.command_status = ESME_RINVPASWD;
395 goto err;
396 }
397 esme->acl = acl;
398 if (acl->default_route)
399 esme->smsc->def_route = esme;
400 }
401 }
402
Harald Weltef1033cc2012-11-08 16:14:37 +0100403 esme->bind_flags |= ESME_BIND_TX | ESME_BIND_RX;
404
Harald Weltef1033cc2012-11-08 16:14:37 +0100405err:
406 return 0;
407}
408
Harald Weltee94db492012-11-08 20:11:05 +0100409/*! \brief handle an incoming SMPP UNBIND */
Harald Weltef1033cc2012-11-08 16:14:37 +0100410static int smpp_handle_unbind(struct osmo_esme *esme, struct msgb *msg)
411{
412 struct unbind_t unbind;
413 struct unbind_resp_t unbind_r;
414 int rc;
415
Harald Welte4dbcdad2012-11-08 22:12:45 +0100416 SMPP34_UNPACK(rc, UNBIND, &unbind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100417 msgb_length(msg));
Harald Welte874f9f12012-11-09 13:02:49 +0100418 if (rc < 0) {
419 LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n",
420 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100421 return rc;
Harald Welte874f9f12012-11-09 13:02:49 +0100422 }
Harald Weltef1033cc2012-11-08 16:14:37 +0100423
424 INIT_RESP(UNBIND_RESP, &unbind_r, &unbind);
425
Harald Welte874f9f12012-11-09 13:02:49 +0100426 LOGP(DSMPP, LOGL_INFO, "[%s] Rx UNBIND\n", esme->system_id);
Harald Weltef1033cc2012-11-08 16:14:37 +0100427
428 if (esme->bind_flags == 0) {
429 unbind_r.command_status = ESME_RINVBNDSTS;
430 goto err;
431 }
432
433 esme->bind_flags = 0;
Harald Welte338e3b32012-11-20 22:22:04 +0100434 if (esme->smsc->def_route == esme)
435 esme->smsc->def_route = NULL;
Harald Weltef1033cc2012-11-08 16:14:37 +0100436err:
437 return PACK_AND_SEND(esme, &unbind_r);
438}
439
Harald Weltee94db492012-11-08 20:11:05 +0100440/*! \brief handle an incoming SMPP ENQUIRE LINK */
Harald Weltef1033cc2012-11-08 16:14:37 +0100441static int smpp_handle_enq_link(struct osmo_esme *esme, struct msgb *msg)
442{
443 struct enquire_link_t enq;
444 struct enquire_link_resp_t enq_r;
445 int rc;
446
Harald Welte4dbcdad2012-11-08 22:12:45 +0100447 SMPP34_UNPACK(rc, ENQUIRE_LINK, &enq, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100448 msgb_length(msg));
Harald Welte874f9f12012-11-09 13:02:49 +0100449 if (rc < 0) {
450 LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n",
451 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100452 return rc;
Harald Welte874f9f12012-11-09 13:02:49 +0100453 }
Harald Weltef1033cc2012-11-08 16:14:37 +0100454
Harald Welte874f9f12012-11-09 13:02:49 +0100455 LOGP(DSMPP, LOGL_DEBUG, "[%s] Rx Enquire Link\n", esme->system_id);
Harald Weltef1033cc2012-11-08 16:14:37 +0100456
457 INIT_RESP(ENQUIRE_LINK_RESP, &enq_r, &enq);
458
Harald Welte874f9f12012-11-09 13:02:49 +0100459 LOGP(DSMPP, LOGL_DEBUG, "[%s] Tx Enquire Link Response\n", esme->system_id);
460
Harald Weltef1033cc2012-11-08 16:14:37 +0100461 return PACK_AND_SEND(esme, &enq_r);
462}
463
Harald Weltee94db492012-11-08 20:11:05 +0100464/*! \brief send a SUBMIT-SM RESPONSE to a remote ESME */
Harald Welted4bdee72012-11-08 19:44:08 +0100465int smpp_tx_submit_r(struct osmo_esme *esme, uint32_t sequence_nr,
466 uint32_t command_status, char *msg_id)
467{
468 struct submit_sm_resp_t submit_r;
469
470 memset(&submit_r, 0, sizeof(submit_r));
471 submit_r.command_length = 0;
472 submit_r.command_id = SUBMIT_SM_RESP;
473 submit_r.command_status = command_status;
474 submit_r.sequence_number= sequence_nr;
Harald Welte8a1b0562012-11-09 12:51:44 +0100475 snprintf((char *) submit_r.message_id, sizeof(submit_r.message_id), "%s", msg_id);
Harald Welted4bdee72012-11-08 19:44:08 +0100476
477 return PACK_AND_SEND(esme, &submit_r);
478}
479
Harald Welte8a1b0562012-11-09 12:51:44 +0100480static const struct value_string smpp_avail_strs[] = {
481 { 0, "Available" },
482 { 1, "Denied" },
483 { 2, "Unavailable" },
484 { 0, NULL }
485};
486
487/*! \brief send an ALERT_NOTIFICATION to a remote ESME */
488int smpp_tx_alert(struct osmo_esme *esme, uint8_t ton, uint8_t npi,
489 const char *addr, uint8_t avail_status)
490{
491 struct alert_notification_t alert;
492 struct tlv_t tlv;
493
494 memset(&alert, 0, sizeof(alert));
495 alert.command_length = 0;
496 alert.command_id = ALERT_NOTIFICATION;
497 alert.command_status = ESME_ROK;
498 alert.sequence_number = esme->own_seq_nr++;
499 alert.source_addr_ton = ton;
500 alert.source_addr_npi = npi;
Harald Welte874f9f12012-11-09 13:02:49 +0100501 snprintf((char *)alert.source_addr, sizeof(alert.source_addr), "%s", addr);
Harald Welte8a1b0562012-11-09 12:51:44 +0100502
503 tlv.tag = TLVID_ms_availability_status;
504 tlv.length = sizeof(uint8_t);
505 tlv.value.val08 = avail_status;
506 build_tlv(&alert.tlv, &tlv);
507
508 LOGP(DSMPP, LOGL_DEBUG, "[%s] Tx ALERT_NOTIFICATION (%s/%u/%u): %s\n",
509 esme->system_id, alert.source_addr, alert.source_addr_ton,
510 alert.source_addr_npi,
511 get_value_string(smpp_avail_strs, avail_status));
512
513 return PACK_AND_SEND(esme, &alert);
514}
515
Harald Weltee94db492012-11-08 20:11:05 +0100516/*! \brief handle an incoming SMPP SUBMIT-SM */
Harald Weltef1033cc2012-11-08 16:14:37 +0100517static int smpp_handle_submit(struct osmo_esme *esme, struct msgb *msg)
518{
519 struct submit_sm_t submit;
520 struct submit_sm_resp_t submit_r;
521 int rc;
522
Harald Welte4dbcdad2012-11-08 22:12:45 +0100523 memset(&submit, 0, sizeof(submit));
524 SMPP34_UNPACK(rc, SUBMIT_SM, &submit, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100525 msgb_length(msg));
Harald Welte874f9f12012-11-09 13:02:49 +0100526 if (rc < 0) {
527 LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n",
528 esme->system_id, smpp34_strerror);
Harald Weltef1033cc2012-11-08 16:14:37 +0100529 return rc;
Harald Welte874f9f12012-11-09 13:02:49 +0100530 }
Harald Weltef1033cc2012-11-08 16:14:37 +0100531
532 INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit);
533
534 if (!(esme->bind_flags & ESME_BIND_TX)) {
535 submit_r.command_status = ESME_RINVBNDSTS;
536 return PACK_AND_SEND(esme, &submit_r);
537 }
538
Harald Welte874f9f12012-11-09 13:02:49 +0100539 LOGP(DSMPP, LOGL_INFO, "[%s] Rx SUBMIT-SM (%s/%u/%u)\n",
540 esme->system_id, submit.destination_addr,
541 submit.dest_addr_ton, submit.dest_addr_npi);
Harald Weltef1033cc2012-11-08 16:14:37 +0100542
543 INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit);
544
545 rc = handle_smpp_submit(esme, &submit, &submit_r);
546 if (rc == 0)
547 return PACK_AND_SEND(esme, &submit_r);
548
549 return rc;
550}
551
Harald Weltee94db492012-11-08 20:11:05 +0100552/*! \brief one complete SMPP PDU from the ESME has been received */
Harald Weltef1033cc2012-11-08 16:14:37 +0100553static int smpp_pdu_rx(struct osmo_esme *esme, struct msgb *msg)
554{
555 uint32_t cmd_id = smpp_msgb_cmdid(msg);
556 int rc = 0;
557
Harald Welte874f9f12012-11-09 13:02:49 +0100558 LOGP(DSMPP, LOGL_DEBUG, "[%s] smpp_pdu_rx(%s)\n", esme->system_id,
Harald Weltef1033cc2012-11-08 16:14:37 +0100559 osmo_hexdump(msgb_data(msg), msgb_length(msg)));
560
561 switch (cmd_id) {
562 case GENERIC_NACK:
563 rc = smpp_handle_gen_nack(esme, msg);
564 break;
565 case BIND_RECEIVER:
566 rc = smpp_handle_bind_rx(esme, msg);
567 break;
568 case BIND_TRANSMITTER:
569 rc = smpp_handle_bind_tx(esme, msg);
570 break;
571 case BIND_TRANSCEIVER:
572 rc = smpp_handle_bind_trx(esme, msg);
573 break;
574 case UNBIND:
575 rc = smpp_handle_unbind(esme, msg);
576 break;
577 case ENQUIRE_LINK:
578 rc = smpp_handle_enq_link(esme, msg);
579 break;
580 case SUBMIT_SM:
581 rc = smpp_handle_submit(esme, msg);
582 break;
583 case DELIVER_SM:
584 break;
585 case DATA_SM:
586 break;
587 case CANCEL_SM:
588 case QUERY_SM:
589 case REPLACE_SM:
590 case SUBMIT_MULTI:
Harald Welte874f9f12012-11-09 13:02:49 +0100591 LOGP(DSMPP, LOGL_NOTICE, "[%s] Unimplemented PDU Commmand "
Harald Weltef1033cc2012-11-08 16:14:37 +0100592 "0x%08x\n", esme->system_id, cmd_id);
593 break;
594 default:
Harald Welte874f9f12012-11-09 13:02:49 +0100595 LOGP(DSMPP, LOGL_ERROR, "[%s] Unknown PDU Command 0x%08x\n",
Harald Weltef1033cc2012-11-08 16:14:37 +0100596 esme->system_id, cmd_id);
597 rc = smpp_tx_gen_nack(esme, smpp_msgb_seq(msg), ESME_RINVCMDID);
598 break;
599 }
600
601 return rc;
602}
603
Harald Weltee94db492012-11-08 20:11:05 +0100604/* !\brief call-back when per-ESME TCP socket has some data to be read */
Harald Weltef1033cc2012-11-08 16:14:37 +0100605static int esme_link_read_cb(struct osmo_fd *ofd)
606{
607 struct osmo_esme *esme = ofd->data;
608 uint32_t len;
609 uint8_t *lenptr = (uint8_t *) &len;
610 uint8_t *cur;
611 struct msgb *msg;
612 int rdlen;
613 int rc;
614
615 switch (esme->read_state) {
616 case READ_ST_IN_LEN:
617 rdlen = sizeof(uint32_t) - esme->read_idx;
618 rc = read(ofd->fd, lenptr + esme->read_idx, rdlen);
619 if (rc < 0) {
Harald Welte874f9f12012-11-09 13:02:49 +0100620 LOGP(DSMPP, LOGL_ERROR, "[%s] read returned %d\n",
621 esme->system_id, rc);
Harald Weltef1033cc2012-11-08 16:14:37 +0100622 } else if (rc == 0) {
623 goto dead_socket;
624 } else
625 esme->read_idx += rc;
626 if (esme->read_idx >= sizeof(uint32_t)) {
627 esme->read_len = ntohl(len);
628 msg = msgb_alloc(esme->read_len, "SMPP Rx");
629 if (!msg)
630 return -ENOMEM;
631 esme->read_msg = msg;
632 cur = msgb_put(msg, sizeof(uint32_t));
633 memcpy(cur, lenptr, sizeof(uint32_t));
634 esme->read_state = READ_ST_IN_MSG;
635 esme->read_idx = sizeof(uint32_t);
636 }
637 break;
638 case READ_ST_IN_MSG:
639 msg = esme->read_msg;
640 rdlen = esme->read_len - esme->read_idx;
641 rc = read(ofd->fd, msg->tail, OSMO_MIN(rdlen, msgb_tailroom(msg)));
642 if (rc < 0) {
Harald Welte874f9f12012-11-09 13:02:49 +0100643 LOGP(DSMPP, LOGL_ERROR, "[%s] read returned %d\n",
644 esme->system_id, rc);
Harald Weltef1033cc2012-11-08 16:14:37 +0100645 } else if (rc == 0) {
646 goto dead_socket;
647 } else {
648 esme->read_idx += rc;
649 msgb_put(msg, rc);
650 }
651
652 if (esme->read_idx >= esme->read_len) {
653 rc = smpp_pdu_rx(esme, esme->read_msg);
654 esme->read_msg = NULL;
655 esme->read_idx = 0;
656 esme->read_len = 0;
657 esme->read_state = READ_ST_IN_LEN;
658 }
659 break;
660 }
661
662 return 0;
663dead_socket:
664 msgb_free(esme->read_msg);
Harald Weltee94db492012-11-08 20:11:05 +0100665 osmo_fd_unregister(&esme->wqueue.bfd);
666 close(esme->wqueue.bfd.fd);
667 esme->wqueue.bfd.fd = -1;
668 smpp_esme_put(esme);
Harald Weltef1033cc2012-11-08 16:14:37 +0100669
670 return 0;
671}
672
673/* call-back of write queue once it wishes to write a message to the socket */
674static void esme_link_write_cb(struct osmo_fd *ofd, struct msgb *msg)
675{
676 struct osmo_esme *esme = ofd->data;
677 int rc;
678
679 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
680 if (rc == 0) {
Harald Weltee94db492012-11-08 20:11:05 +0100681 osmo_fd_unregister(&esme->wqueue.bfd);
682 close(esme->wqueue.bfd.fd);
683 esme->wqueue.bfd.fd = -1;
684 smpp_esme_put(esme);
Harald Weltef1033cc2012-11-08 16:14:37 +0100685 } else if (rc < msgb_length(msg)) {
Harald Welte874f9f12012-11-09 13:02:49 +0100686 LOGP(DSMPP, LOGL_ERROR, "[%s] Short write\n", esme->system_id);
Harald Weltef1033cc2012-11-08 16:14:37 +0100687 return;
688 }
689}
690
691/* callback for already-accepted new TCP socket */
692static int link_accept_cb(struct smsc *smsc, int fd,
693 struct sockaddr_storage *s, socklen_t s_len)
694{
695 struct osmo_esme *esme = talloc_zero(smsc, struct osmo_esme);
696 if (!esme)
697 return -ENOMEM;
698
Harald Weltee94db492012-11-08 20:11:05 +0100699 smpp_esme_get(esme);
Harald Welte8a1b0562012-11-09 12:51:44 +0100700 esme->own_seq_nr = rand();
Harald Weltef1033cc2012-11-08 16:14:37 +0100701 esme->smsc = smsc;
702 osmo_wqueue_init(&esme->wqueue, 10);
703 esme->wqueue.bfd.fd = fd;
704 esme->wqueue.bfd.data = esme;
705 esme->wqueue.bfd.when = BSC_FD_READ;
706 osmo_fd_register(&esme->wqueue.bfd);
707
708 esme->wqueue.read_cb = esme_link_read_cb;
709 esme->wqueue.write_cb = esme_link_write_cb;
710
711 esme->sa_len = OSMO_MIN(sizeof(esme->sa), s_len);
712 memcpy(&esme->sa, s, esme->sa_len);
713
714 llist_add_tail(&esme->list, &smsc->esme_list);
715
716 return 0;
717}
718
719/* callback of listening TCP socket */
720static int smsc_fd_cb(struct osmo_fd *ofd, unsigned int what)
721{
722 int rc;
723 struct sockaddr_storage sa;
724 socklen_t sa_len = sizeof(sa);
725
726 rc = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
727 if (rc < 0) {
728 LOGP(DSMPP, LOGL_ERROR, "Accept returns %d (%s)\n",
729 rc, strerror(errno));
730 return rc;
731 }
732 return link_accept_cb(ofd->data, rc, &sa, sa_len);
733}
734
Harald Weltee94db492012-11-08 20:11:05 +0100735/*! \brief Initialize the SMSC-side SMPP implementation */
Harald Weltef1033cc2012-11-08 16:14:37 +0100736int smpp_smsc_init(struct smsc *smsc, uint16_t port)
737{
738 int rc;
739
Harald Welte338e3b32012-11-20 22:22:04 +0100740 /* default port for SMPP */
741 if (port == 0)
742 port = 2775;
743
744 /* This will not work if we were to actually ever use FD 0
745 * (stdin) for this ... */
746 if (smsc->listen_ofd.fd <= 0) {
747 INIT_LLIST_HEAD(&smsc->esme_list);
748 INIT_LLIST_HEAD(&smsc->acl_list);
749 smsc->listen_ofd.data = smsc;
750 smsc->listen_ofd.cb = smsc_fd_cb;
751 } else {
752 close(smsc->listen_ofd.fd);
753 osmo_fd_unregister(&smsc->listen_ofd);
754 }
755
Harald Weltef1033cc2012-11-08 16:14:37 +0100756 rc = osmo_sock_init_ofd(&smsc->listen_ofd, AF_UNSPEC, SOCK_STREAM,
Harald Welte338e3b32012-11-20 22:22:04 +0100757 IPPROTO_TCP, NULL, port,
758 OSMO_SOCK_F_BIND);
759
760 /* if there is an error, try to re-bind to the old port */
761 if (rc < 0) {
762 rc = osmo_sock_init_ofd(&smsc->listen_ofd, AF_UNSPEC,
763 SOCK_STREAM, IPPROTO_TCP, NULL,
764 smsc->listen_port, OSMO_SOCK_F_BIND);
765 } else
766 smsc->listen_port = port;
Harald Weltef1033cc2012-11-08 16:14:37 +0100767
768 return rc;
769}