Harald Welte | f1033cc | 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 | |
Harald Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 44 | /*! \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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 49 | enum emse_bind { |
| 50 | ESME_BIND_RX = 0x01, |
| 51 | ESME_BIND_TX = 0x02, |
| 52 | }; |
| 53 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 54 | struct 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 | |
| 67 | struct 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 | |
| 89 | void 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 106 | /*! \brief increaes the use/reference count */ |
| 107 | void smpp_esme_get(struct osmo_esme *esme) |
| 108 | { |
| 109 | esme->use++; |
| 110 | } |
| 111 | |
| 112 | static 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 */ |
| 124 | void smpp_esme_put(struct osmo_esme *esme) |
| 125 | { |
| 126 | esme->use--; |
| 127 | if (esme->use <= 0) |
| 128 | esme_destroy(esme); |
| 129 | } |
| 130 | |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 131 | static struct osmo_esme * |
| 132 | esme_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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 142 | |
| 143 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 144 | /*! \brief initialize the libsmpp34 data structure for a response */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 145 | #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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 153 | /*! \brief pack a libsmpp34 data strcutrure and send it to the ESME */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 154 | #define PACK_AND_SEND(esme, ptr) pack_and_send(esme, (ptr)->command_id, ptr) |
| 155 | static 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 Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 164 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error during smpp34_pack(): %s\n", |
| 165 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 166 | msgb_free(msg); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | msgb_put(msg, rlen); |
| 170 | |
| 171 | return osmo_wqueue_enqueue(&esme->wqueue, msg); |
| 172 | } |
| 173 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 174 | /*! \brief transmit a generic NACK to a remote ESME */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 175 | static int smpp_tx_gen_nack(struct osmo_esme *esme, uint32_t seq, uint32_t status) |
| 176 | { |
| 177 | struct generic_nack_t nack; |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 178 | char buf[SMALL_BUFF]; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 179 | |
| 180 | nack.command_length = 0; |
| 181 | nack.command_id = GENERIC_NACK; |
| 182 | nack.sequence_number = seq; |
| 183 | nack.command_status = status; |
| 184 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 185 | LOGP(DSMPP, LOGL_ERROR, "[%s] Tx GENERIC NACK: %s\n", |
| 186 | esme->system_id, str_command_status(status, buf)); |
| 187 | |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 188 | return PACK_AND_SEND(esme, &nack); |
| 189 | } |
| 190 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 191 | /*! \brief retrieve SMPP command ID from a msgb */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 192 | static 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 198 | /*! \brief retrieve SMPP sequence number from a msgb */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 199 | static 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 205 | /*! \brief handle an incoming SMPP generic NACK */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 206 | static 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 Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 212 | SMPP34_UNPACK(rc, GENERIC_NACK, &nack, msgb_data(msg), |
| 213 | msgb_length(msg)); |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 214 | if (rc < 0) { |
| 215 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n", |
| 216 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 217 | return rc; |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 218 | } |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 219 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 220 | LOGP(DSMPP, LOGL_ERROR, "[%s] Rx GENERIC NACK: %s\n", |
| 221 | esme->system_id, str_command_status(nack.command_status, buf)); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 226 | /*! \brief handle an incoming SMPP BIND RECEIVER */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 227 | static 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 Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 231 | struct osmo_smpp_acl *acl; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 232 | int rc; |
| 233 | |
Harald Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 234 | SMPP34_UNPACK(rc, BIND_RECEIVER, &bind, msgb_data(msg), |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 235 | msgb_length(msg)); |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 236 | if (rc < 0) { |
| 237 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n", |
| 238 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 239 | return rc; |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 240 | } |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 241 | |
| 242 | INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind); |
| 243 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 244 | LOGP(DSMPP, LOGL_INFO, "[%s] Rx BIND Rx from (Version %02x)\n", |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 245 | 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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 260 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 261 | 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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 280 | err: |
| 281 | return 0; |
| 282 | } |
| 283 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 284 | /*! \brief handle an incoming SMPP BIND TRANSMITTER */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 285 | static 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 Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 289 | struct osmo_smpp_acl *acl; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 290 | struct tlv_t tlv; |
| 291 | int rc; |
| 292 | |
Harald Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 293 | SMPP34_UNPACK(rc, BIND_TRANSMITTER, &bind, msgb_data(msg), |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 294 | msgb_length(msg)); |
| 295 | if (rc < 0) { |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 296 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n", |
| 297 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 298 | return rc; |
| 299 | } |
| 300 | |
| 301 | INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind); |
| 302 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 303 | LOGP(DSMPP, LOGL_INFO, "[%s] Rx BIND Tx (Version %02x)\n", |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 304 | 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 Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 318 | |
| 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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 335 | 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 | |
| 347 | err: |
| 348 | return PACK_AND_SEND(esme, &bind_r); |
| 349 | } |
| 350 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 351 | /*! \brief handle an incoming SMPP BIND TRANSCEIVER */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 352 | static 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 Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 356 | struct osmo_smpp_acl *acl; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 357 | int rc; |
| 358 | |
Harald Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 359 | SMPP34_UNPACK(rc, BIND_TRANSCEIVER, &bind, msgb_data(msg), |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 360 | msgb_length(msg)); |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 361 | if (rc < 0) { |
| 362 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n", |
| 363 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 364 | return rc; |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 365 | } |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 366 | |
| 367 | INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind); |
| 368 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 369 | LOGP(DSMPP, LOGL_INFO, "[%s] Rx BIND Trx (Version %02x)\n", |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 370 | 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 Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 384 | |
| 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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 403 | esme->bind_flags |= ESME_BIND_TX | ESME_BIND_RX; |
| 404 | |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 405 | err: |
| 406 | return 0; |
| 407 | } |
| 408 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 409 | /*! \brief handle an incoming SMPP UNBIND */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 410 | static 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 Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 416 | SMPP34_UNPACK(rc, UNBIND, &unbind, msgb_data(msg), |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 417 | msgb_length(msg)); |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 418 | if (rc < 0) { |
| 419 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n", |
| 420 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 421 | return rc; |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 422 | } |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 423 | |
| 424 | INIT_RESP(UNBIND_RESP, &unbind_r, &unbind); |
| 425 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 426 | LOGP(DSMPP, LOGL_INFO, "[%s] Rx UNBIND\n", esme->system_id); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 427 | |
| 428 | if (esme->bind_flags == 0) { |
| 429 | unbind_r.command_status = ESME_RINVBNDSTS; |
| 430 | goto err; |
| 431 | } |
| 432 | |
| 433 | esme->bind_flags = 0; |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 434 | if (esme->smsc->def_route == esme) |
| 435 | esme->smsc->def_route = NULL; |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 436 | err: |
| 437 | return PACK_AND_SEND(esme, &unbind_r); |
| 438 | } |
| 439 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 440 | /*! \brief handle an incoming SMPP ENQUIRE LINK */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 441 | static 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 Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 447 | SMPP34_UNPACK(rc, ENQUIRE_LINK, &enq, msgb_data(msg), |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 448 | msgb_length(msg)); |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 449 | if (rc < 0) { |
| 450 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n", |
| 451 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 452 | return rc; |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 453 | } |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 454 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 455 | LOGP(DSMPP, LOGL_DEBUG, "[%s] Rx Enquire Link\n", esme->system_id); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 456 | |
| 457 | INIT_RESP(ENQUIRE_LINK_RESP, &enq_r, &enq); |
| 458 | |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 459 | LOGP(DSMPP, LOGL_DEBUG, "[%s] Tx Enquire Link Response\n", esme->system_id); |
| 460 | |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 461 | return PACK_AND_SEND(esme, &enq_r); |
| 462 | } |
| 463 | |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 464 | /*! \brief send a SUBMIT-SM RESPONSE to a remote ESME */ |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 465 | int 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 Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 475 | snprintf((char *) submit_r.message_id, sizeof(submit_r.message_id), "%s", msg_id); |
Harald Welte | d4bdee7 | 2012-11-08 19:44:08 +0100 | [diff] [blame] | 476 | |
| 477 | return PACK_AND_SEND(esme, &submit_r); |
| 478 | } |
| 479 | |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 480 | static 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 */ |
| 488 | int 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 Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 501 | snprintf((char *)alert.source_addr, sizeof(alert.source_addr), "%s", addr); |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 502 | |
| 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 516 | /*! \brief handle an incoming SMPP SUBMIT-SM */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 517 | static 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 Welte | 4dbcdad | 2012-11-08 22:12:45 +0100 | [diff] [blame] | 523 | memset(&submit, 0, sizeof(submit)); |
| 524 | SMPP34_UNPACK(rc, SUBMIT_SM, &submit, msgb_data(msg), |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 525 | msgb_length(msg)); |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 526 | if (rc < 0) { |
| 527 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error in smpp34_unpack():%s\n", |
| 528 | esme->system_id, smpp34_strerror); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 529 | return rc; |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 530 | } |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 531 | |
| 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 Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 539 | 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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 542 | |
| 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 552 | /*! \brief one complete SMPP PDU from the ESME has been received */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 553 | static 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 Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 558 | LOGP(DSMPP, LOGL_DEBUG, "[%s] smpp_pdu_rx(%s)\n", esme->system_id, |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 559 | 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 Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 591 | LOGP(DSMPP, LOGL_NOTICE, "[%s] Unimplemented PDU Commmand " |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 592 | "0x%08x\n", esme->system_id, cmd_id); |
| 593 | break; |
| 594 | default: |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 595 | LOGP(DSMPP, LOGL_ERROR, "[%s] Unknown PDU Command 0x%08x\n", |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 596 | 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 604 | /* !\brief call-back when per-ESME TCP socket has some data to be read */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 605 | static 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 Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 620 | LOGP(DSMPP, LOGL_ERROR, "[%s] read returned %d\n", |
| 621 | esme->system_id, rc); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 622 | } 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 Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 643 | LOGP(DSMPP, LOGL_ERROR, "[%s] read returned %d\n", |
| 644 | esme->system_id, rc); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 645 | } 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; |
| 663 | dead_socket: |
| 664 | msgb_free(esme->read_msg); |
Harald Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 665 | osmo_fd_unregister(&esme->wqueue.bfd); |
| 666 | close(esme->wqueue.bfd.fd); |
| 667 | esme->wqueue.bfd.fd = -1; |
| 668 | smpp_esme_put(esme); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /* call-back of write queue once it wishes to write a message to the socket */ |
| 674 | static 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 681 | osmo_fd_unregister(&esme->wqueue.bfd); |
| 682 | close(esme->wqueue.bfd.fd); |
| 683 | esme->wqueue.bfd.fd = -1; |
| 684 | smpp_esme_put(esme); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 685 | } else if (rc < msgb_length(msg)) { |
Harald Welte | 874f9f1 | 2012-11-09 13:02:49 +0100 | [diff] [blame] | 686 | LOGP(DSMPP, LOGL_ERROR, "[%s] Short write\n", esme->system_id); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 687 | return; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* callback for already-accepted new TCP socket */ |
| 692 | static 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 699 | smpp_esme_get(esme); |
Harald Welte | 8a1b056 | 2012-11-09 12:51:44 +0100 | [diff] [blame] | 700 | esme->own_seq_nr = rand(); |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 701 | 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 */ |
| 720 | static 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 Welte | e94db49 | 2012-11-08 20:11:05 +0100 | [diff] [blame] | 735 | /*! \brief Initialize the SMSC-side SMPP implementation */ |
Harald Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 736 | int smpp_smsc_init(struct smsc *smsc, uint16_t port) |
| 737 | { |
| 738 | int rc; |
| 739 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 740 | /* 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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 756 | rc = osmo_sock_init_ofd(&smsc->listen_ofd, AF_UNSPEC, SOCK_STREAM, |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame^] | 757 | 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 Welte | f1033cc | 2012-11-08 16:14:37 +0100 | [diff] [blame] | 767 | |
| 768 | return rc; |
| 769 | } |