blob: 6bfd0c9b76d2bc5fe019351067688417af3e5706 [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 Weltee94db492012-11-08 20:11:05 +010054/*! \brief increaes the use/reference count */
55void smpp_esme_get(struct osmo_esme *esme)
56{
57 esme->use++;
58}
59
60static void esme_destroy(struct osmo_esme *esme)
61{
62 osmo_wqueue_clear(&esme->wqueue);
63 if (esme->wqueue.bfd.fd >= 0) {
64 osmo_fd_unregister(&esme->wqueue.bfd);
65 close(esme->wqueue.bfd.fd);
66 }
67 llist_del(&esme->list);
68 talloc_free(esme);
69}
70
71/*! \brief decrease the use/reference count, free if it is 0 */
72void smpp_esme_put(struct osmo_esme *esme)
73{
74 esme->use--;
75 if (esme->use <= 0)
76 esme_destroy(esme);
77}
78
Harald Welted4bdee72012-11-08 19:44:08 +010079static struct osmo_esme *
80esme_by_system_id(const struct smsc *smsc, char *system_id)
81{
82 struct osmo_esme *e;
83
84 llist_for_each_entry(e, &smsc->esme_list, list) {
85 if (!strcmp(e->system_id, system_id))
86 return e;
87 }
88 return NULL;
89}
Harald Weltef1033cc2012-11-08 16:14:37 +010090
91
Harald Weltee94db492012-11-08 20:11:05 +010092/*! \brief initialize the libsmpp34 data structure for a response */
Harald Weltef1033cc2012-11-08 16:14:37 +010093#define INIT_RESP(type, resp, req) { \
94 memset((resp), 0, sizeof(*(resp))); \
95 (resp)->command_length = 0; \
96 (resp)->command_id = type; \
97 (resp)->command_status = ESME_ROK; \
98 (resp)->sequence_number = (req)->sequence_number; \
99}
100
Harald Weltee94db492012-11-08 20:11:05 +0100101/*! \brief pack a libsmpp34 data strcutrure and send it to the ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100102#define PACK_AND_SEND(esme, ptr) pack_and_send(esme, (ptr)->command_id, ptr)
103static int pack_and_send(struct osmo_esme *esme, uint32_t type, void *ptr)
104{
105 struct msgb *msg = msgb_alloc(4096, "SMPP_Tx");
106 int rc, rlen;
107 if (!msg)
108 return -ENOMEM;
109
110 rc = smpp34_pack(type, msg->tail, msgb_tailroom(msg), &rlen, ptr);
111 if (rc != 0) {
112 LOGP(DSMPP, LOGL_ERROR, "Error during smpp34_pack(): %s\n",
113 smpp34_strerror);
114 msgb_free(msg);
115 return -EINVAL;
116 }
117 msgb_put(msg, rlen);
118
119 return osmo_wqueue_enqueue(&esme->wqueue, msg);
120}
121
Harald Weltee94db492012-11-08 20:11:05 +0100122/*! \brief transmit a generic NACK to a remote ESME */
Harald Weltef1033cc2012-11-08 16:14:37 +0100123static int smpp_tx_gen_nack(struct osmo_esme *esme, uint32_t seq, uint32_t status)
124{
125 struct generic_nack_t nack;
126
127 nack.command_length = 0;
128 nack.command_id = GENERIC_NACK;
129 nack.sequence_number = seq;
130 nack.command_status = status;
131
132 return PACK_AND_SEND(esme, &nack);
133}
134
Harald Weltee94db492012-11-08 20:11:05 +0100135/*! \brief retrieve SMPP command ID from a msgb */
Harald Weltef1033cc2012-11-08 16:14:37 +0100136static inline uint32_t smpp_msgb_cmdid(struct msgb *msg)
137{
138 uint8_t *tmp = msgb_data(msg) + 4;
139 return ntohl(*(uint32_t *)tmp);
140}
141
Harald Weltee94db492012-11-08 20:11:05 +0100142/*! \brief retrieve SMPP sequence number from a msgb */
Harald Weltef1033cc2012-11-08 16:14:37 +0100143static inline uint32_t smpp_msgb_seq(struct msgb *msg)
144{
145 uint8_t *tmp = msgb_data(msg);
146 return ntohl(*(uint32_t *)tmp);
147}
148
Harald Weltee94db492012-11-08 20:11:05 +0100149/*! \brief handle an incoming SMPP generic NACK */
Harald Weltef1033cc2012-11-08 16:14:37 +0100150static int smpp_handle_gen_nack(struct osmo_esme *esme, struct msgb *msg)
151{
152 struct generic_nack_t nack;
153 char buf[SMALL_BUFF];
154 int rc;
155
Harald Welte4dbcdad2012-11-08 22:12:45 +0100156 SMPP34_UNPACK(rc, GENERIC_NACK, &nack, msgb_data(msg),
157 msgb_length(msg));
Harald Weltef1033cc2012-11-08 16:14:37 +0100158 if (rc < 0)
159 return rc;
160
161 LOGP(DSMPP, LOGL_ERROR, "%s: GENERIC NACK: %s\n", esme->system_id,
162 str_command_status(nack.command_status, buf));
163
164 return 0;
165}
166
Harald Weltee94db492012-11-08 20:11:05 +0100167/*! \brief handle an incoming SMPP BIND RECEIVER */
Harald Weltef1033cc2012-11-08 16:14:37 +0100168static int smpp_handle_bind_rx(struct osmo_esme *esme, struct msgb *msg)
169{
170 struct bind_receiver_t bind;
171 struct bind_receiver_resp_t bind_r;
172 int rc;
173
Harald Welte4dbcdad2012-11-08 22:12:45 +0100174 SMPP34_UNPACK(rc, BIND_RECEIVER, &bind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100175 msgb_length(msg));
176 if (rc < 0)
177 return rc;
178
179 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
180
181 LOGP(DSMPP, LOGL_INFO, "%s: BIND Rx from (Version %02x)\n",
182 bind.system_id, bind.interface_version);
183
184 if (bind.interface_version != SMPP_VERSION) {
185 bind_r.command_status = ESME_RSYSERR;
186 goto err;
187 }
188
189 if (esme->bind_flags) {
190 bind_r.command_status = ESME_RALYBND;
191 goto err;
192 }
193
194 esme->smpp_version = bind.interface_version;
195 snprintf(esme->system_id, sizeof(esme->system_id), "%s",
196 bind.system_id);
197 esme->bind_flags = ESME_BIND_RX;
198
199 /* FIXME */
200err:
201 return 0;
202}
203
Harald Weltee94db492012-11-08 20:11:05 +0100204/*! \brief handle an incoming SMPP BIND TRANSMITTER */
Harald Weltef1033cc2012-11-08 16:14:37 +0100205static int smpp_handle_bind_tx(struct osmo_esme *esme, struct msgb *msg)
206{
207 struct bind_transmitter_t bind;
208 struct bind_transmitter_resp_t bind_r;
209 struct tlv_t tlv;
210 int rc;
211
Harald Welte4dbcdad2012-11-08 22:12:45 +0100212 SMPP34_UNPACK(rc, BIND_TRANSMITTER, &bind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100213 msgb_length(msg));
214 if (rc < 0) {
215 printf("error during unpack: %s\n", smpp34_strerror);
216 return rc;
217 }
218
219 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
220
221 LOGP(DSMPP, LOGL_INFO, "%s: BIND Tx (Version %02x)\n",
222 bind.system_id, bind.interface_version);
223
224 if (bind.interface_version != SMPP_VERSION) {
225 bind_r.command_status = ESME_RSYSERR;
226 goto err;
227 }
228
229 if (esme->bind_flags) {
230 bind_r.command_status = ESME_RALYBND;
231 goto err;
232 }
233
234 esme->smpp_version = bind.interface_version;
235 snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id);
236 esme->bind_flags = ESME_BIND_TX;
237
238 /* build response */
239 snprintf((char *)bind_r.system_id, sizeof(bind_r.system_id), "%s",
240 esme->smsc->system_id);
241
242 /* add interface version TLV */
243 tlv.tag = TLVID_sc_interface_version;
244 tlv.length = sizeof(uint8_t);
245 tlv.value.val16 = esme->smpp_version;
246 build_tlv(&bind_r.tlv, &tlv);
247
248err:
249 return PACK_AND_SEND(esme, &bind_r);
250}
251
Harald Weltee94db492012-11-08 20:11:05 +0100252/*! \brief handle an incoming SMPP BIND TRANSCEIVER */
Harald Weltef1033cc2012-11-08 16:14:37 +0100253static int smpp_handle_bind_trx(struct osmo_esme *esme, struct msgb *msg)
254{
255 struct bind_transceiver_t bind;
256 struct bind_transceiver_resp_t bind_r;
257 int rc;
258
Harald Welte4dbcdad2012-11-08 22:12:45 +0100259 SMPP34_UNPACK(rc, BIND_TRANSCEIVER, &bind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100260 msgb_length(msg));
261 if (rc < 0)
262 return rc;
263
264 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
265
266 LOGP(DSMPP, LOGL_INFO, "%s: BIND Trx (Version %02x)\n",
267 bind.system_id, bind.interface_version);
268
269 if (bind.interface_version != SMPP_VERSION) {
270 bind_r.command_status = ESME_RSYSERR;
271 goto err;
272 }
273
274 if (esme->bind_flags) {
275 bind_r.command_status = ESME_RALYBND;
276 goto err;
277 }
278
279 esme->smpp_version = bind.interface_version;
280 snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id);
281 esme->bind_flags |= ESME_BIND_TX | ESME_BIND_RX;
282
283 /* FIXME */
284err:
285 return 0;
286}
287
Harald Weltee94db492012-11-08 20:11:05 +0100288/*! \brief handle an incoming SMPP UNBIND */
Harald Weltef1033cc2012-11-08 16:14:37 +0100289static int smpp_handle_unbind(struct osmo_esme *esme, struct msgb *msg)
290{
291 struct unbind_t unbind;
292 struct unbind_resp_t unbind_r;
293 int rc;
294
Harald Welte4dbcdad2012-11-08 22:12:45 +0100295 SMPP34_UNPACK(rc, UNBIND, &unbind, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100296 msgb_length(msg));
297 if (rc < 0)
298 return rc;
299
300 INIT_RESP(UNBIND_RESP, &unbind_r, &unbind);
301
302 LOGP(DSMPP, LOGL_INFO, "%s: UNBIND\n", esme->system_id);
303
304 if (esme->bind_flags == 0) {
305 unbind_r.command_status = ESME_RINVBNDSTS;
306 goto err;
307 }
308
309 esme->bind_flags = 0;
310err:
311 return PACK_AND_SEND(esme, &unbind_r);
312}
313
Harald Weltee94db492012-11-08 20:11:05 +0100314/*! \brief handle an incoming SMPP ENQUIRE LINK */
Harald Weltef1033cc2012-11-08 16:14:37 +0100315static int smpp_handle_enq_link(struct osmo_esme *esme, struct msgb *msg)
316{
317 struct enquire_link_t enq;
318 struct enquire_link_resp_t enq_r;
319 int rc;
320
Harald Welte4dbcdad2012-11-08 22:12:45 +0100321 SMPP34_UNPACK(rc, ENQUIRE_LINK, &enq, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100322 msgb_length(msg));
323 if (rc < 0)
324 return rc;
325
326 LOGP(DSMPP, LOGL_DEBUG, "%s: Enquire Link\n", esme->system_id);
327
328 INIT_RESP(ENQUIRE_LINK_RESP, &enq_r, &enq);
329
330 return PACK_AND_SEND(esme, &enq_r);
331}
332
Harald Weltee94db492012-11-08 20:11:05 +0100333/*! \brief send a SUBMIT-SM RESPONSE to a remote ESME */
Harald Welted4bdee72012-11-08 19:44:08 +0100334int smpp_tx_submit_r(struct osmo_esme *esme, uint32_t sequence_nr,
335 uint32_t command_status, char *msg_id)
336{
337 struct submit_sm_resp_t submit_r;
338
339 memset(&submit_r, 0, sizeof(submit_r));
340 submit_r.command_length = 0;
341 submit_r.command_id = SUBMIT_SM_RESP;
342 submit_r.command_status = command_status;
343 submit_r.sequence_number= sequence_nr;
Harald Welte8a1b0562012-11-09 12:51:44 +0100344 snprintf((char *) submit_r.message_id, sizeof(submit_r.message_id), "%s", msg_id);
Harald Welted4bdee72012-11-08 19:44:08 +0100345
346 return PACK_AND_SEND(esme, &submit_r);
347}
348
Harald Welte8a1b0562012-11-09 12:51:44 +0100349static const struct value_string smpp_avail_strs[] = {
350 { 0, "Available" },
351 { 1, "Denied" },
352 { 2, "Unavailable" },
353 { 0, NULL }
354};
355
356/*! \brief send an ALERT_NOTIFICATION to a remote ESME */
357int smpp_tx_alert(struct osmo_esme *esme, uint8_t ton, uint8_t npi,
358 const char *addr, uint8_t avail_status)
359{
360 struct alert_notification_t alert;
361 struct tlv_t tlv;
362
363 memset(&alert, 0, sizeof(alert));
364 alert.command_length = 0;
365 alert.command_id = ALERT_NOTIFICATION;
366 alert.command_status = ESME_ROK;
367 alert.sequence_number = esme->own_seq_nr++;
368 alert.source_addr_ton = ton;
369 alert.source_addr_npi = npi;
370 snprintf(alert.source_addr, sizeof(alert.source_addr), "%s", addr);
371
372 tlv.tag = TLVID_ms_availability_status;
373 tlv.length = sizeof(uint8_t);
374 tlv.value.val08 = avail_status;
375 build_tlv(&alert.tlv, &tlv);
376
377 LOGP(DSMPP, LOGL_DEBUG, "[%s] Tx ALERT_NOTIFICATION (%s/%u/%u): %s\n",
378 esme->system_id, alert.source_addr, alert.source_addr_ton,
379 alert.source_addr_npi,
380 get_value_string(smpp_avail_strs, avail_status));
381
382 return PACK_AND_SEND(esme, &alert);
383}
384
Harald Weltee94db492012-11-08 20:11:05 +0100385/*! \brief handle an incoming SMPP SUBMIT-SM */
Harald Weltef1033cc2012-11-08 16:14:37 +0100386static int smpp_handle_submit(struct osmo_esme *esme, struct msgb *msg)
387{
388 struct submit_sm_t submit;
389 struct submit_sm_resp_t submit_r;
390 int rc;
391
Harald Welte4dbcdad2012-11-08 22:12:45 +0100392 memset(&submit, 0, sizeof(submit));
393 SMPP34_UNPACK(rc, SUBMIT_SM, &submit, msgb_data(msg),
Harald Weltef1033cc2012-11-08 16:14:37 +0100394 msgb_length(msg));
395 if (rc < 0)
396 return rc;
397
398 INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit);
399
400 if (!(esme->bind_flags & ESME_BIND_TX)) {
401 submit_r.command_status = ESME_RINVBNDSTS;
402 return PACK_AND_SEND(esme, &submit_r);
403 }
404
405 LOGP(DSMPP, LOGL_INFO, "%s: SUBMIT-SM(%s)\n", esme->system_id,
406 submit.service_type);
407
408 INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit);
409
410 rc = handle_smpp_submit(esme, &submit, &submit_r);
411 if (rc == 0)
412 return PACK_AND_SEND(esme, &submit_r);
413
414 return rc;
415}
416
Harald Weltee94db492012-11-08 20:11:05 +0100417/*! \brief one complete SMPP PDU from the ESME has been received */
Harald Weltef1033cc2012-11-08 16:14:37 +0100418static int smpp_pdu_rx(struct osmo_esme *esme, struct msgb *msg)
419{
420 uint32_t cmd_id = smpp_msgb_cmdid(msg);
421 int rc = 0;
422
423 LOGP(DSMPP, LOGL_DEBUG, "%s: smpp_pdu_rx(%s)\n", esme->system_id,
424 osmo_hexdump(msgb_data(msg), msgb_length(msg)));
425
426 switch (cmd_id) {
427 case GENERIC_NACK:
428 rc = smpp_handle_gen_nack(esme, msg);
429 break;
430 case BIND_RECEIVER:
431 rc = smpp_handle_bind_rx(esme, msg);
432 break;
433 case BIND_TRANSMITTER:
434 rc = smpp_handle_bind_tx(esme, msg);
435 break;
436 case BIND_TRANSCEIVER:
437 rc = smpp_handle_bind_trx(esme, msg);
438 break;
439 case UNBIND:
440 rc = smpp_handle_unbind(esme, msg);
441 break;
442 case ENQUIRE_LINK:
443 rc = smpp_handle_enq_link(esme, msg);
444 break;
445 case SUBMIT_SM:
446 rc = smpp_handle_submit(esme, msg);
447 break;
448 case DELIVER_SM:
449 break;
450 case DATA_SM:
451 break;
452 case CANCEL_SM:
453 case QUERY_SM:
454 case REPLACE_SM:
455 case SUBMIT_MULTI:
456 LOGP(DSMPP, LOGL_NOTICE, "%s: Unimplemented PDU Commmand "
457 "0x%08x\n", esme->system_id, cmd_id);
458 break;
459 default:
460 LOGP(DSMPP, LOGL_ERROR, "%s: Unknown PDU Command 0x%08x\n",
461 esme->system_id, cmd_id);
462 rc = smpp_tx_gen_nack(esme, smpp_msgb_seq(msg), ESME_RINVCMDID);
463 break;
464 }
465
466 return rc;
467}
468
Harald Weltee94db492012-11-08 20:11:05 +0100469/* !\brief call-back when per-ESME TCP socket has some data to be read */
Harald Weltef1033cc2012-11-08 16:14:37 +0100470static int esme_link_read_cb(struct osmo_fd *ofd)
471{
472 struct osmo_esme *esme = ofd->data;
473 uint32_t len;
474 uint8_t *lenptr = (uint8_t *) &len;
475 uint8_t *cur;
476 struct msgb *msg;
477 int rdlen;
478 int rc;
479
480 switch (esme->read_state) {
481 case READ_ST_IN_LEN:
482 rdlen = sizeof(uint32_t) - esme->read_idx;
483 rc = read(ofd->fd, lenptr + esme->read_idx, rdlen);
484 if (rc < 0) {
485 LOGP(DSMPP, LOGL_ERROR, "read returned %d\n", rc);
486 } else if (rc == 0) {
487 goto dead_socket;
488 } else
489 esme->read_idx += rc;
490 if (esme->read_idx >= sizeof(uint32_t)) {
491 esme->read_len = ntohl(len);
492 msg = msgb_alloc(esme->read_len, "SMPP Rx");
493 if (!msg)
494 return -ENOMEM;
495 esme->read_msg = msg;
496 cur = msgb_put(msg, sizeof(uint32_t));
497 memcpy(cur, lenptr, sizeof(uint32_t));
498 esme->read_state = READ_ST_IN_MSG;
499 esme->read_idx = sizeof(uint32_t);
500 }
501 break;
502 case READ_ST_IN_MSG:
503 msg = esme->read_msg;
504 rdlen = esme->read_len - esme->read_idx;
505 rc = read(ofd->fd, msg->tail, OSMO_MIN(rdlen, msgb_tailroom(msg)));
506 if (rc < 0) {
507 LOGP(DSMPP, LOGL_ERROR, "read returned %d\n", rc);
508 } else if (rc == 0) {
509 goto dead_socket;
510 } else {
511 esme->read_idx += rc;
512 msgb_put(msg, rc);
513 }
514
515 if (esme->read_idx >= esme->read_len) {
516 rc = smpp_pdu_rx(esme, esme->read_msg);
517 esme->read_msg = NULL;
518 esme->read_idx = 0;
519 esme->read_len = 0;
520 esme->read_state = READ_ST_IN_LEN;
521 }
522 break;
523 }
524
525 return 0;
526dead_socket:
527 msgb_free(esme->read_msg);
Harald Weltee94db492012-11-08 20:11:05 +0100528 osmo_fd_unregister(&esme->wqueue.bfd);
529 close(esme->wqueue.bfd.fd);
530 esme->wqueue.bfd.fd = -1;
531 smpp_esme_put(esme);
Harald Weltef1033cc2012-11-08 16:14:37 +0100532
533 return 0;
534}
535
536/* call-back of write queue once it wishes to write a message to the socket */
537static void esme_link_write_cb(struct osmo_fd *ofd, struct msgb *msg)
538{
539 struct osmo_esme *esme = ofd->data;
540 int rc;
541
542 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
543 if (rc == 0) {
Harald Weltee94db492012-11-08 20:11:05 +0100544 osmo_fd_unregister(&esme->wqueue.bfd);
545 close(esme->wqueue.bfd.fd);
546 esme->wqueue.bfd.fd = -1;
547 smpp_esme_put(esme);
Harald Weltef1033cc2012-11-08 16:14:37 +0100548 } else if (rc < msgb_length(msg)) {
549 LOGP(DSMPP, LOGL_ERROR, "%s: Short write\n", esme->system_id);
550 return;
551 }
552}
553
554/* callback for already-accepted new TCP socket */
555static int link_accept_cb(struct smsc *smsc, int fd,
556 struct sockaddr_storage *s, socklen_t s_len)
557{
558 struct osmo_esme *esme = talloc_zero(smsc, struct osmo_esme);
559 if (!esme)
560 return -ENOMEM;
561
Harald Weltee94db492012-11-08 20:11:05 +0100562 smpp_esme_get(esme);
Harald Welte8a1b0562012-11-09 12:51:44 +0100563 esme->own_seq_nr = rand();
Harald Weltef1033cc2012-11-08 16:14:37 +0100564 esme->smsc = smsc;
565 osmo_wqueue_init(&esme->wqueue, 10);
566 esme->wqueue.bfd.fd = fd;
567 esme->wqueue.bfd.data = esme;
568 esme->wqueue.bfd.when = BSC_FD_READ;
569 osmo_fd_register(&esme->wqueue.bfd);
570
571 esme->wqueue.read_cb = esme_link_read_cb;
572 esme->wqueue.write_cb = esme_link_write_cb;
573
574 esme->sa_len = OSMO_MIN(sizeof(esme->sa), s_len);
575 memcpy(&esme->sa, s, esme->sa_len);
576
577 llist_add_tail(&esme->list, &smsc->esme_list);
578
579 return 0;
580}
581
582/* callback of listening TCP socket */
583static int smsc_fd_cb(struct osmo_fd *ofd, unsigned int what)
584{
585 int rc;
586 struct sockaddr_storage sa;
587 socklen_t sa_len = sizeof(sa);
588
589 rc = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
590 if (rc < 0) {
591 LOGP(DSMPP, LOGL_ERROR, "Accept returns %d (%s)\n",
592 rc, strerror(errno));
593 return rc;
594 }
595 return link_accept_cb(ofd->data, rc, &sa, sa_len);
596}
597
Harald Weltee94db492012-11-08 20:11:05 +0100598/*! \brief Initialize the SMSC-side SMPP implementation */
Harald Weltef1033cc2012-11-08 16:14:37 +0100599int smpp_smsc_init(struct smsc *smsc, uint16_t port)
600{
601 int rc;
602
603 INIT_LLIST_HEAD(&smsc->esme_list);
604 smsc->listen_ofd.data = smsc;
605 smsc->listen_ofd.cb = smsc_fd_cb;
606 rc = osmo_sock_init_ofd(&smsc->listen_ofd, AF_UNSPEC, SOCK_STREAM,
607 IPPROTO_TCP, NULL, port, OSMO_SOCK_F_BIND);
608
609 return rc;
610}