Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 1 | /* 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 | |
| 44 | enum emse_bind { |
| 45 | ESME_BIND_RX = 0x01, |
| 46 | ESME_BIND_TX = 0x02, |
| 47 | }; |
| 48 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 49 | /*! \brief increaes the use/reference count */ |
| 50 | void smpp_esme_get(struct osmo_esme *esme) |
| 51 | { |
| 52 | esme->use++; |
| 53 | } |
| 54 | |
| 55 | static void esme_destroy(struct osmo_esme *esme) |
| 56 | { |
| 57 | osmo_wqueue_clear(&esme->wqueue); |
| 58 | if (esme->wqueue.bfd.fd >= 0) { |
| 59 | osmo_fd_unregister(&esme->wqueue.bfd); |
| 60 | close(esme->wqueue.bfd.fd); |
| 61 | } |
| 62 | llist_del(&esme->list); |
| 63 | talloc_free(esme); |
| 64 | } |
| 65 | |
| 66 | /*! \brief decrease the use/reference count, free if it is 0 */ |
| 67 | void smpp_esme_put(struct osmo_esme *esme) |
| 68 | { |
| 69 | esme->use--; |
| 70 | if (esme->use <= 0) |
| 71 | esme_destroy(esme); |
| 72 | } |
| 73 | |
Harald Welte | 4341309 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 74 | static struct osmo_esme * |
| 75 | esme_by_system_id(const struct smsc *smsc, char *system_id) |
| 76 | { |
| 77 | struct osmo_esme *e; |
| 78 | |
| 79 | llist_for_each_entry(e, &smsc->esme_list, list) { |
| 80 | if (!strcmp(e->system_id, system_id)) |
| 81 | return e; |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 85 | |
| 86 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 87 | /*! \brief initialize the libsmpp34 data structure for a response */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 88 | #define INIT_RESP(type, resp, req) { \ |
| 89 | memset((resp), 0, sizeof(*(resp))); \ |
| 90 | (resp)->command_length = 0; \ |
| 91 | (resp)->command_id = type; \ |
| 92 | (resp)->command_status = ESME_ROK; \ |
| 93 | (resp)->sequence_number = (req)->sequence_number; \ |
| 94 | } |
| 95 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 96 | /*! \brief pack a libsmpp34 data strcutrure and send it to the ESME */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 97 | #define PACK_AND_SEND(esme, ptr) pack_and_send(esme, (ptr)->command_id, ptr) |
| 98 | static int pack_and_send(struct osmo_esme *esme, uint32_t type, void *ptr) |
| 99 | { |
| 100 | struct msgb *msg = msgb_alloc(4096, "SMPP_Tx"); |
| 101 | int rc, rlen; |
| 102 | if (!msg) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | rc = smpp34_pack(type, msg->tail, msgb_tailroom(msg), &rlen, ptr); |
| 106 | if (rc != 0) { |
| 107 | LOGP(DSMPP, LOGL_ERROR, "Error during smpp34_pack(): %s\n", |
| 108 | smpp34_strerror); |
| 109 | msgb_free(msg); |
| 110 | return -EINVAL; |
| 111 | } |
| 112 | msgb_put(msg, rlen); |
| 113 | |
| 114 | return osmo_wqueue_enqueue(&esme->wqueue, msg); |
| 115 | } |
| 116 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 117 | /*! \brief transmit a generic NACK to a remote ESME */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 118 | static int smpp_tx_gen_nack(struct osmo_esme *esme, uint32_t seq, uint32_t status) |
| 119 | { |
| 120 | struct generic_nack_t nack; |
| 121 | |
| 122 | nack.command_length = 0; |
| 123 | nack.command_id = GENERIC_NACK; |
| 124 | nack.sequence_number = seq; |
| 125 | nack.command_status = status; |
| 126 | |
| 127 | return PACK_AND_SEND(esme, &nack); |
| 128 | } |
| 129 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 130 | /*! \brief retrieve SMPP command ID from a msgb */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 131 | static inline uint32_t smpp_msgb_cmdid(struct msgb *msg) |
| 132 | { |
| 133 | uint8_t *tmp = msgb_data(msg) + 4; |
| 134 | return ntohl(*(uint32_t *)tmp); |
| 135 | } |
| 136 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 137 | /*! \brief retrieve SMPP sequence number from a msgb */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 138 | static inline uint32_t smpp_msgb_seq(struct msgb *msg) |
| 139 | { |
| 140 | uint8_t *tmp = msgb_data(msg); |
| 141 | return ntohl(*(uint32_t *)tmp); |
| 142 | } |
| 143 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 144 | /*! \brief handle an incoming SMPP generic NACK */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 145 | static int smpp_handle_gen_nack(struct osmo_esme *esme, struct msgb *msg) |
| 146 | { |
| 147 | struct generic_nack_t nack; |
| 148 | char buf[SMALL_BUFF]; |
| 149 | int rc; |
| 150 | |
| 151 | rc = smpp34_unpack(GENERIC_NACK, &nack, msgb_data(msg), |
| 152 | msgb_length(msg)); |
| 153 | if (rc < 0) |
| 154 | return rc; |
| 155 | |
| 156 | LOGP(DSMPP, LOGL_ERROR, "%s: GENERIC NACK: %s\n", esme->system_id, |
| 157 | str_command_status(nack.command_status, buf)); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 162 | /*! \brief handle an incoming SMPP BIND RECEIVER */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 163 | static int smpp_handle_bind_rx(struct osmo_esme *esme, struct msgb *msg) |
| 164 | { |
| 165 | struct bind_receiver_t bind; |
| 166 | struct bind_receiver_resp_t bind_r; |
| 167 | int rc; |
| 168 | |
| 169 | rc = smpp34_unpack(BIND_RECEIVER, &bind, msgb_data(msg), |
| 170 | msgb_length(msg)); |
| 171 | if (rc < 0) |
| 172 | return rc; |
| 173 | |
| 174 | INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind); |
| 175 | |
| 176 | LOGP(DSMPP, LOGL_INFO, "%s: BIND Rx from (Version %02x)\n", |
| 177 | bind.system_id, bind.interface_version); |
| 178 | |
| 179 | if (bind.interface_version != SMPP_VERSION) { |
| 180 | bind_r.command_status = ESME_RSYSERR; |
| 181 | goto err; |
| 182 | } |
| 183 | |
| 184 | if (esme->bind_flags) { |
| 185 | bind_r.command_status = ESME_RALYBND; |
| 186 | goto err; |
| 187 | } |
| 188 | |
| 189 | esme->smpp_version = bind.interface_version; |
| 190 | snprintf(esme->system_id, sizeof(esme->system_id), "%s", |
| 191 | bind.system_id); |
| 192 | esme->bind_flags = ESME_BIND_RX; |
| 193 | |
| 194 | /* FIXME */ |
| 195 | err: |
| 196 | return 0; |
| 197 | } |
| 198 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 199 | /*! \brief handle an incoming SMPP BIND TRANSMITTER */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 200 | static int smpp_handle_bind_tx(struct osmo_esme *esme, struct msgb *msg) |
| 201 | { |
| 202 | struct bind_transmitter_t bind; |
| 203 | struct bind_transmitter_resp_t bind_r; |
| 204 | struct tlv_t tlv; |
| 205 | int rc; |
| 206 | |
| 207 | rc = smpp34_unpack(BIND_TRANSMITTER, &bind, msgb_data(msg), |
| 208 | msgb_length(msg)); |
| 209 | if (rc < 0) { |
| 210 | printf("error during unpack: %s\n", smpp34_strerror); |
| 211 | return rc; |
| 212 | } |
| 213 | |
| 214 | INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind); |
| 215 | |
| 216 | LOGP(DSMPP, LOGL_INFO, "%s: BIND Tx (Version %02x)\n", |
| 217 | bind.system_id, bind.interface_version); |
| 218 | |
| 219 | if (bind.interface_version != SMPP_VERSION) { |
| 220 | bind_r.command_status = ESME_RSYSERR; |
| 221 | goto err; |
| 222 | } |
| 223 | |
| 224 | if (esme->bind_flags) { |
| 225 | bind_r.command_status = ESME_RALYBND; |
| 226 | goto err; |
| 227 | } |
| 228 | |
| 229 | esme->smpp_version = bind.interface_version; |
| 230 | snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id); |
| 231 | esme->bind_flags = ESME_BIND_TX; |
| 232 | |
| 233 | /* build response */ |
| 234 | snprintf((char *)bind_r.system_id, sizeof(bind_r.system_id), "%s", |
| 235 | esme->smsc->system_id); |
| 236 | |
| 237 | /* add interface version TLV */ |
| 238 | tlv.tag = TLVID_sc_interface_version; |
| 239 | tlv.length = sizeof(uint8_t); |
| 240 | tlv.value.val16 = esme->smpp_version; |
| 241 | build_tlv(&bind_r.tlv, &tlv); |
| 242 | |
| 243 | err: |
| 244 | return PACK_AND_SEND(esme, &bind_r); |
| 245 | } |
| 246 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 247 | /*! \brief handle an incoming SMPP BIND TRANSCEIVER */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 248 | static int smpp_handle_bind_trx(struct osmo_esme *esme, struct msgb *msg) |
| 249 | { |
| 250 | struct bind_transceiver_t bind; |
| 251 | struct bind_transceiver_resp_t bind_r; |
| 252 | int rc; |
| 253 | |
| 254 | rc = smpp34_unpack(BIND_TRANSCEIVER, &bind, msgb_data(msg), |
| 255 | msgb_length(msg)); |
| 256 | if (rc < 0) |
| 257 | return rc; |
| 258 | |
| 259 | INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind); |
| 260 | |
| 261 | LOGP(DSMPP, LOGL_INFO, "%s: BIND Trx (Version %02x)\n", |
| 262 | bind.system_id, bind.interface_version); |
| 263 | |
| 264 | if (bind.interface_version != SMPP_VERSION) { |
| 265 | bind_r.command_status = ESME_RSYSERR; |
| 266 | goto err; |
| 267 | } |
| 268 | |
| 269 | if (esme->bind_flags) { |
| 270 | bind_r.command_status = ESME_RALYBND; |
| 271 | goto err; |
| 272 | } |
| 273 | |
| 274 | esme->smpp_version = bind.interface_version; |
| 275 | snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id); |
| 276 | esme->bind_flags |= ESME_BIND_TX | ESME_BIND_RX; |
| 277 | |
| 278 | /* FIXME */ |
| 279 | err: |
| 280 | return 0; |
| 281 | } |
| 282 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 283 | /*! \brief handle an incoming SMPP UNBIND */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 284 | static int smpp_handle_unbind(struct osmo_esme *esme, struct msgb *msg) |
| 285 | { |
| 286 | struct unbind_t unbind; |
| 287 | struct unbind_resp_t unbind_r; |
| 288 | int rc; |
| 289 | |
| 290 | rc = smpp34_unpack(UNBIND, &unbind, msgb_data(msg), |
| 291 | msgb_length(msg)); |
| 292 | if (rc < 0) |
| 293 | return rc; |
| 294 | |
| 295 | INIT_RESP(UNBIND_RESP, &unbind_r, &unbind); |
| 296 | |
| 297 | LOGP(DSMPP, LOGL_INFO, "%s: UNBIND\n", esme->system_id); |
| 298 | |
| 299 | if (esme->bind_flags == 0) { |
| 300 | unbind_r.command_status = ESME_RINVBNDSTS; |
| 301 | goto err; |
| 302 | } |
| 303 | |
| 304 | esme->bind_flags = 0; |
| 305 | err: |
| 306 | return PACK_AND_SEND(esme, &unbind_r); |
| 307 | } |
| 308 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 309 | /*! \brief handle an incoming SMPP ENQUIRE LINK */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 310 | static int smpp_handle_enq_link(struct osmo_esme *esme, struct msgb *msg) |
| 311 | { |
| 312 | struct enquire_link_t enq; |
| 313 | struct enquire_link_resp_t enq_r; |
| 314 | int rc; |
| 315 | |
| 316 | rc = smpp34_unpack(ENQUIRE_LINK, &enq, msgb_data(msg), |
| 317 | msgb_length(msg)); |
| 318 | if (rc < 0) |
| 319 | return rc; |
| 320 | |
| 321 | LOGP(DSMPP, LOGL_DEBUG, "%s: Enquire Link\n", esme->system_id); |
| 322 | |
| 323 | INIT_RESP(ENQUIRE_LINK_RESP, &enq_r, &enq); |
| 324 | |
| 325 | return PACK_AND_SEND(esme, &enq_r); |
| 326 | } |
| 327 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 328 | /*! \brief send a SUBMIT-SM RESPONSE to a remote ESME */ |
Harald Welte | 4341309 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 329 | int smpp_tx_submit_r(struct osmo_esme *esme, uint32_t sequence_nr, |
| 330 | uint32_t command_status, char *msg_id) |
| 331 | { |
| 332 | struct submit_sm_resp_t submit_r; |
| 333 | |
| 334 | memset(&submit_r, 0, sizeof(submit_r)); |
| 335 | submit_r.command_length = 0; |
| 336 | submit_r.command_id = SUBMIT_SM_RESP; |
| 337 | submit_r.command_status = command_status; |
| 338 | submit_r.sequence_number= sequence_nr; |
| 339 | snprintf(submit_r.message_id, sizeof(submit_r.message_id), "%s", msg_id); |
| 340 | |
| 341 | return PACK_AND_SEND(esme, &submit_r); |
| 342 | } |
| 343 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 344 | /*! \brief handle an incoming SMPP SUBMIT-SM */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 345 | static int smpp_handle_submit(struct osmo_esme *esme, struct msgb *msg) |
| 346 | { |
| 347 | struct submit_sm_t submit; |
| 348 | struct submit_sm_resp_t submit_r; |
| 349 | int rc; |
| 350 | |
| 351 | rc = smpp34_unpack(SUBMIT_SM, &submit, msgb_data(msg), |
| 352 | msgb_length(msg)); |
| 353 | if (rc < 0) |
| 354 | return rc; |
| 355 | |
| 356 | INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit); |
| 357 | |
| 358 | if (!(esme->bind_flags & ESME_BIND_TX)) { |
| 359 | submit_r.command_status = ESME_RINVBNDSTS; |
| 360 | return PACK_AND_SEND(esme, &submit_r); |
| 361 | } |
| 362 | |
| 363 | LOGP(DSMPP, LOGL_INFO, "%s: SUBMIT-SM(%s)\n", esme->system_id, |
| 364 | submit.service_type); |
| 365 | |
| 366 | INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit); |
| 367 | |
| 368 | rc = handle_smpp_submit(esme, &submit, &submit_r); |
| 369 | if (rc == 0) |
| 370 | return PACK_AND_SEND(esme, &submit_r); |
| 371 | |
| 372 | return rc; |
| 373 | } |
| 374 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 375 | /*! \brief one complete SMPP PDU from the ESME has been received */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 376 | static int smpp_pdu_rx(struct osmo_esme *esme, struct msgb *msg) |
| 377 | { |
| 378 | uint32_t cmd_id = smpp_msgb_cmdid(msg); |
| 379 | int rc = 0; |
| 380 | |
| 381 | LOGP(DSMPP, LOGL_DEBUG, "%s: smpp_pdu_rx(%s)\n", esme->system_id, |
| 382 | osmo_hexdump(msgb_data(msg), msgb_length(msg))); |
| 383 | |
| 384 | switch (cmd_id) { |
| 385 | case GENERIC_NACK: |
| 386 | rc = smpp_handle_gen_nack(esme, msg); |
| 387 | break; |
| 388 | case BIND_RECEIVER: |
| 389 | rc = smpp_handle_bind_rx(esme, msg); |
| 390 | break; |
| 391 | case BIND_TRANSMITTER: |
| 392 | rc = smpp_handle_bind_tx(esme, msg); |
| 393 | break; |
| 394 | case BIND_TRANSCEIVER: |
| 395 | rc = smpp_handle_bind_trx(esme, msg); |
| 396 | break; |
| 397 | case UNBIND: |
| 398 | rc = smpp_handle_unbind(esme, msg); |
| 399 | break; |
| 400 | case ENQUIRE_LINK: |
| 401 | rc = smpp_handle_enq_link(esme, msg); |
| 402 | break; |
| 403 | case SUBMIT_SM: |
| 404 | rc = smpp_handle_submit(esme, msg); |
| 405 | break; |
| 406 | case DELIVER_SM: |
| 407 | break; |
| 408 | case DATA_SM: |
| 409 | break; |
| 410 | case CANCEL_SM: |
| 411 | case QUERY_SM: |
| 412 | case REPLACE_SM: |
| 413 | case SUBMIT_MULTI: |
| 414 | LOGP(DSMPP, LOGL_NOTICE, "%s: Unimplemented PDU Commmand " |
| 415 | "0x%08x\n", esme->system_id, cmd_id); |
| 416 | break; |
| 417 | default: |
| 418 | LOGP(DSMPP, LOGL_ERROR, "%s: Unknown PDU Command 0x%08x\n", |
| 419 | esme->system_id, cmd_id); |
| 420 | rc = smpp_tx_gen_nack(esme, smpp_msgb_seq(msg), ESME_RINVCMDID); |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | return rc; |
| 425 | } |
| 426 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 427 | /* !\brief call-back when per-ESME TCP socket has some data to be read */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 428 | static int esme_link_read_cb(struct osmo_fd *ofd) |
| 429 | { |
| 430 | struct osmo_esme *esme = ofd->data; |
| 431 | uint32_t len; |
| 432 | uint8_t *lenptr = (uint8_t *) &len; |
| 433 | uint8_t *cur; |
| 434 | struct msgb *msg; |
| 435 | int rdlen; |
| 436 | int rc; |
| 437 | |
| 438 | switch (esme->read_state) { |
| 439 | case READ_ST_IN_LEN: |
| 440 | rdlen = sizeof(uint32_t) - esme->read_idx; |
| 441 | rc = read(ofd->fd, lenptr + esme->read_idx, rdlen); |
| 442 | if (rc < 0) { |
| 443 | LOGP(DSMPP, LOGL_ERROR, "read returned %d\n", rc); |
| 444 | } else if (rc == 0) { |
| 445 | goto dead_socket; |
| 446 | } else |
| 447 | esme->read_idx += rc; |
| 448 | if (esme->read_idx >= sizeof(uint32_t)) { |
| 449 | esme->read_len = ntohl(len); |
| 450 | msg = msgb_alloc(esme->read_len, "SMPP Rx"); |
| 451 | if (!msg) |
| 452 | return -ENOMEM; |
| 453 | esme->read_msg = msg; |
| 454 | cur = msgb_put(msg, sizeof(uint32_t)); |
| 455 | memcpy(cur, lenptr, sizeof(uint32_t)); |
| 456 | esme->read_state = READ_ST_IN_MSG; |
| 457 | esme->read_idx = sizeof(uint32_t); |
| 458 | } |
| 459 | break; |
| 460 | case READ_ST_IN_MSG: |
| 461 | msg = esme->read_msg; |
| 462 | rdlen = esme->read_len - esme->read_idx; |
| 463 | rc = read(ofd->fd, msg->tail, OSMO_MIN(rdlen, msgb_tailroom(msg))); |
| 464 | if (rc < 0) { |
| 465 | LOGP(DSMPP, LOGL_ERROR, "read returned %d\n", rc); |
| 466 | } else if (rc == 0) { |
| 467 | goto dead_socket; |
| 468 | } else { |
| 469 | esme->read_idx += rc; |
| 470 | msgb_put(msg, rc); |
| 471 | } |
| 472 | |
| 473 | if (esme->read_idx >= esme->read_len) { |
| 474 | rc = smpp_pdu_rx(esme, esme->read_msg); |
| 475 | esme->read_msg = NULL; |
| 476 | esme->read_idx = 0; |
| 477 | esme->read_len = 0; |
| 478 | esme->read_state = READ_ST_IN_LEN; |
| 479 | } |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | dead_socket: |
| 485 | msgb_free(esme->read_msg); |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 486 | osmo_fd_unregister(&esme->wqueue.bfd); |
| 487 | close(esme->wqueue.bfd.fd); |
| 488 | esme->wqueue.bfd.fd = -1; |
| 489 | smpp_esme_put(esme); |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* call-back of write queue once it wishes to write a message to the socket */ |
| 495 | static void esme_link_write_cb(struct osmo_fd *ofd, struct msgb *msg) |
| 496 | { |
| 497 | struct osmo_esme *esme = ofd->data; |
| 498 | int rc; |
| 499 | |
| 500 | rc = write(ofd->fd, msgb_data(msg), msgb_length(msg)); |
| 501 | if (rc == 0) { |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 502 | osmo_fd_unregister(&esme->wqueue.bfd); |
| 503 | close(esme->wqueue.bfd.fd); |
| 504 | esme->wqueue.bfd.fd = -1; |
| 505 | smpp_esme_put(esme); |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 506 | } else if (rc < msgb_length(msg)) { |
| 507 | LOGP(DSMPP, LOGL_ERROR, "%s: Short write\n", esme->system_id); |
| 508 | return; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* callback for already-accepted new TCP socket */ |
| 513 | static int link_accept_cb(struct smsc *smsc, int fd, |
| 514 | struct sockaddr_storage *s, socklen_t s_len) |
| 515 | { |
| 516 | struct osmo_esme *esme = talloc_zero(smsc, struct osmo_esme); |
| 517 | if (!esme) |
| 518 | return -ENOMEM; |
| 519 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 520 | smpp_esme_get(esme); |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 521 | esme->smsc = smsc; |
| 522 | osmo_wqueue_init(&esme->wqueue, 10); |
| 523 | esme->wqueue.bfd.fd = fd; |
| 524 | esme->wqueue.bfd.data = esme; |
| 525 | esme->wqueue.bfd.when = BSC_FD_READ; |
| 526 | osmo_fd_register(&esme->wqueue.bfd); |
| 527 | |
| 528 | esme->wqueue.read_cb = esme_link_read_cb; |
| 529 | esme->wqueue.write_cb = esme_link_write_cb; |
| 530 | |
| 531 | esme->sa_len = OSMO_MIN(sizeof(esme->sa), s_len); |
| 532 | memcpy(&esme->sa, s, esme->sa_len); |
| 533 | |
| 534 | llist_add_tail(&esme->list, &smsc->esme_list); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /* callback of listening TCP socket */ |
| 540 | static int smsc_fd_cb(struct osmo_fd *ofd, unsigned int what) |
| 541 | { |
| 542 | int rc; |
| 543 | struct sockaddr_storage sa; |
| 544 | socklen_t sa_len = sizeof(sa); |
| 545 | |
| 546 | rc = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len); |
| 547 | if (rc < 0) { |
| 548 | LOGP(DSMPP, LOGL_ERROR, "Accept returns %d (%s)\n", |
| 549 | rc, strerror(errno)); |
| 550 | return rc; |
| 551 | } |
| 552 | return link_accept_cb(ofd->data, rc, &sa, sa_len); |
| 553 | } |
| 554 | |
Harald Welte | bf6d98e | 2012-11-08 20:11:05 +0100 | [diff] [blame^] | 555 | /*! \brief Initialize the SMSC-side SMPP implementation */ |
Harald Welte | 2dba82d | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 556 | int smpp_smsc_init(struct smsc *smsc, uint16_t port) |
| 557 | { |
| 558 | int rc; |
| 559 | |
| 560 | INIT_LLIST_HEAD(&smsc->esme_list); |
| 561 | smsc->listen_ofd.data = smsc; |
| 562 | smsc->listen_ofd.cb = smsc_fd_cb; |
| 563 | rc = osmo_sock_init_ofd(&smsc->listen_ofd, AF_UNSPEC, SOCK_STREAM, |
| 564 | IPPROTO_TCP, NULL, port, OSMO_SOCK_F_BIND); |
| 565 | |
| 566 | return rc; |
| 567 | } |