Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdint.h> |
| 4 | #include <errno.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | |
| 9 | #include <smpp34.h> |
| 10 | #include <smpp34_structs.h> |
| 11 | #include <smpp34_params.h> |
| 12 | |
Holger Hans Peter Freyther | 5ecbc93 | 2013-07-27 18:39:30 +0200 | [diff] [blame] | 13 | #include <osmocom/core/application.h> |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 14 | #include <osmocom/core/utils.h> |
| 15 | #include <osmocom/core/msgb.h> |
| 16 | #include <osmocom/core/talloc.h> |
| 17 | #include <osmocom/core/select.h> |
| 18 | #include <osmocom/core/socket.h> |
| 19 | #include <osmocom/core/write_queue.h> |
| 20 | |
| 21 | #include <openbsc/debug.h> |
| 22 | |
| 23 | /* FIXME: merge with smpp_smsc.c */ |
| 24 | #define SMPP_SYS_ID_LEN 16 |
| 25 | enum esme_read_state { |
| 26 | READ_ST_IN_LEN = 0, |
| 27 | READ_ST_IN_MSG = 1, |
| 28 | }; |
| 29 | /* FIXME: merge with smpp_smsc.c */ |
| 30 | |
| 31 | struct esme { |
| 32 | struct osmo_fd ofd; |
| 33 | |
| 34 | uint32_t own_seq_nr; |
| 35 | |
| 36 | struct osmo_wqueue wqueue; |
| 37 | enum esme_read_state read_state; |
| 38 | uint32_t read_len; |
| 39 | uint32_t read_idx; |
| 40 | struct msgb *read_msg; |
| 41 | |
| 42 | uint8_t smpp_version; |
| 43 | char system_id[SMPP_SYS_ID_LEN+1]; |
| 44 | char password[SMPP_SYS_ID_LEN+1]; |
| 45 | }; |
| 46 | |
| 47 | /* FIXME: merge with smpp_smsc.c */ |
| 48 | #define SMPP34_UNPACK(rc, type, str, data, len) \ |
| 49 | memset(str, 0, sizeof(*str)); \ |
| 50 | rc = smpp34_unpack(type, str, data, len) |
| 51 | #define INIT_RESP(type, resp, req) { \ |
| 52 | memset((resp), 0, sizeof(*(resp))); \ |
| 53 | (resp)->command_length = 0; \ |
| 54 | (resp)->command_id = type; \ |
| 55 | (resp)->command_status = ESME_ROK; \ |
| 56 | (resp)->sequence_number = (req)->sequence_number; \ |
| 57 | } |
| 58 | #define PACK_AND_SEND(esme, ptr) pack_and_send(esme, (ptr)->command_id, ptr) |
| 59 | static inline uint32_t smpp_msgb_cmdid(struct msgb *msg) |
| 60 | { |
| 61 | uint8_t *tmp = msgb_data(msg) + 4; |
| 62 | return ntohl(*(uint32_t *)tmp); |
| 63 | } |
| 64 | static uint32_t esme_inc_seq_nr(struct esme *esme) |
| 65 | { |
| 66 | esme->own_seq_nr++; |
| 67 | if (esme->own_seq_nr > 0x7fffffff) |
| 68 | esme->own_seq_nr = 1; |
| 69 | |
| 70 | return esme->own_seq_nr; |
| 71 | } |
| 72 | static int pack_and_send(struct esme *esme, uint32_t type, void *ptr) |
| 73 | { |
| 74 | struct msgb *msg = msgb_alloc(4096, "SMPP_Tx"); |
| 75 | int rc, rlen; |
| 76 | if (!msg) |
| 77 | return -ENOMEM; |
| 78 | |
| 79 | rc = smpp34_pack(type, msg->tail, msgb_tailroom(msg), &rlen, ptr); |
| 80 | if (rc != 0) { |
| 81 | LOGP(DSMPP, LOGL_ERROR, "[%s] Error during smpp34_pack(): %s\n", |
| 82 | esme->system_id, smpp34_strerror); |
| 83 | msgb_free(msg); |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | msgb_put(msg, rlen); |
| 87 | |
Holger Hans Peter Freyther | fd603ed | 2015-03-28 18:09:31 +0100 | [diff] [blame^] | 88 | if (osmo_wqueue_enqueue(&esme->wqueue, msg) != 0) { |
| 89 | LOGP(DSMPP, LOGL_ERROR, "[%s] Write queue full. Dropping message\n", |
| 90 | esme->system_id); |
| 91 | msgb_free(msg); |
| 92 | return -EAGAIN; |
| 93 | } |
| 94 | return 0; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 95 | } |
| 96 | /* FIXME: merge with smpp_smsc.c */ |
| 97 | |
| 98 | |
| 99 | static int smpp_handle_deliver(struct esme *esme, struct msgb *msg) |
| 100 | { |
| 101 | struct deliver_sm_t deliver; |
| 102 | struct deliver_sm_resp_t deliver_r; |
| 103 | struct submit_sm_t submit; |
| 104 | int rc; |
| 105 | |
| 106 | memset(&deliver, 0, sizeof(deliver)); |
| 107 | SMPP34_UNPACK(rc, DELIVER_SM, &deliver, msgb_data(msg), msgb_length(msg)); |
| 108 | if (rc < 0) |
| 109 | return rc; |
| 110 | |
| 111 | INIT_RESP(DELIVER_SM_RESP, &deliver_r, &deliver); |
| 112 | |
| 113 | PACK_AND_SEND(esme, &deliver_r); |
| 114 | |
| 115 | memset(&submit, 0, sizeof(submit)); |
| 116 | submit.command_id = SUBMIT_SM; |
| 117 | submit.command_status = ESME_ROK; |
| 118 | submit.sequence_number = esme_inc_seq_nr(esme); |
| 119 | |
| 120 | submit.dest_addr_ton = deliver.source_addr_ton; |
| 121 | submit.dest_addr_npi = deliver.source_addr_npi; |
| 122 | memcpy(submit.destination_addr, deliver.source_addr, |
| 123 | OSMO_MIN(sizeof(submit.destination_addr), |
| 124 | sizeof(deliver.source_addr))); |
| 125 | |
| 126 | submit.source_addr_ton = deliver.dest_addr_ton; |
| 127 | submit.source_addr_npi = deliver.dest_addr_npi; |
| 128 | memcpy(submit.source_addr, deliver.destination_addr, |
| 129 | OSMO_MIN(sizeof(submit.source_addr), |
| 130 | sizeof(deliver.destination_addr))); |
| 131 | |
| 132 | submit.esm_class = deliver.esm_class; |
| 133 | submit.protocol_id = deliver.protocol_id; |
| 134 | submit.priority_flag = deliver.priority_flag; |
| 135 | memcpy(submit.schedule_delivery_time, deliver.schedule_delivery_time, |
| 136 | OSMO_MIN(sizeof(submit.schedule_delivery_time), |
| 137 | sizeof(deliver.schedule_delivery_time))); |
| 138 | memcpy(submit.validity_period, deliver.validity_period, |
| 139 | OSMO_MIN(sizeof(submit.validity_period), |
| 140 | sizeof(deliver.validity_period))); |
| 141 | submit.registered_delivery = deliver.registered_delivery; |
| 142 | submit.replace_if_present_flag = deliver.replace_if_present_flag; |
| 143 | submit.data_coding = deliver.data_coding; |
| 144 | submit.sm_default_msg_id = deliver.sm_default_msg_id; |
| 145 | submit.sm_length = deliver.sm_length; |
| 146 | memcpy(submit.short_message, deliver.short_message, |
| 147 | OSMO_MIN(sizeof(submit.short_message), |
| 148 | sizeof(deliver.short_message))); |
| 149 | /* FIXME: TLV? */ |
| 150 | |
| 151 | return PACK_AND_SEND(esme, &submit); |
| 152 | } |
| 153 | |
| 154 | static int bind_transceiver(struct esme *esme) |
| 155 | { |
| 156 | struct bind_transceiver_t bind; |
| 157 | |
| 158 | memset(&bind, 0, sizeof(bind)); |
| 159 | bind.command_id = BIND_TRANSCEIVER; |
| 160 | bind.sequence_number = esme_inc_seq_nr(esme); |
| 161 | snprintf((char *)bind.system_id, sizeof(bind.system_id), "%s", esme->system_id); |
| 162 | snprintf((char *)bind.password, sizeof(bind.password), "%s", esme->password); |
| 163 | snprintf((char *)bind.system_type, sizeof(bind.system_type), "mirror"); |
| 164 | bind.interface_version = esme->smpp_version; |
| 165 | |
| 166 | return PACK_AND_SEND(esme, &bind); |
| 167 | } |
| 168 | |
| 169 | static int smpp_pdu_rx(struct esme *esme, struct msgb *msg) |
| 170 | { |
| 171 | uint32_t cmd_id = smpp_msgb_cmdid(msg); |
| 172 | int rc; |
| 173 | |
| 174 | switch (cmd_id) { |
| 175 | case DELIVER_SM: |
| 176 | rc = smpp_handle_deliver(esme, msg); |
| 177 | break; |
| 178 | default: |
Holger Hans Peter Freyther | 7672db3 | 2013-12-12 15:23:26 +0100 | [diff] [blame] | 179 | LOGP(DSMPP, LOGL_NOTICE, "unhandled case %d\n", cmd_id); |
| 180 | rc = 0; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | |
| 184 | return rc; |
| 185 | } |
| 186 | |
| 187 | /* FIXME: merge with smpp_smsc.c */ |
| 188 | static int esme_read_cb(struct osmo_fd *ofd) |
| 189 | { |
| 190 | struct esme *esme = ofd->data; |
| 191 | uint32_t len; |
| 192 | uint8_t *lenptr = (uint8_t *) &len; |
| 193 | uint8_t *cur; |
| 194 | struct msgb *msg; |
| 195 | int rdlen; |
| 196 | int rc; |
| 197 | |
| 198 | switch (esme->read_state) { |
| 199 | case READ_ST_IN_LEN: |
| 200 | rdlen = sizeof(uint32_t) - esme->read_idx; |
| 201 | rc = read(ofd->fd, lenptr + esme->read_idx, rdlen); |
| 202 | if (rc < 0) { |
| 203 | LOGP(DSMPP, LOGL_ERROR, "[%s] read returned %d\n", |
| 204 | esme->system_id, rc); |
| 205 | } else if (rc == 0) { |
| 206 | goto dead_socket; |
| 207 | } else |
| 208 | esme->read_idx += rc; |
| 209 | if (esme->read_idx >= sizeof(uint32_t)) { |
| 210 | esme->read_len = ntohl(len); |
| 211 | msg = msgb_alloc(esme->read_len, "SMPP Rx"); |
| 212 | if (!msg) |
| 213 | return -ENOMEM; |
| 214 | esme->read_msg = msg; |
| 215 | cur = msgb_put(msg, sizeof(uint32_t)); |
| 216 | memcpy(cur, lenptr, sizeof(uint32_t)); |
| 217 | esme->read_state = READ_ST_IN_MSG; |
| 218 | esme->read_idx = sizeof(uint32_t); |
| 219 | } |
| 220 | break; |
| 221 | case READ_ST_IN_MSG: |
| 222 | msg = esme->read_msg; |
| 223 | rdlen = esme->read_len - esme->read_idx; |
| 224 | rc = read(ofd->fd, msg->tail, OSMO_MIN(rdlen, msgb_tailroom(msg))); |
| 225 | if (rc < 0) { |
| 226 | LOGP(DSMPP, LOGL_ERROR, "[%s] read returned %d\n", |
| 227 | esme->system_id, rc); |
| 228 | } else if (rc == 0) { |
| 229 | goto dead_socket; |
| 230 | } else { |
| 231 | esme->read_idx += rc; |
| 232 | msgb_put(msg, rc); |
| 233 | } |
| 234 | |
| 235 | if (esme->read_idx >= esme->read_len) { |
| 236 | rc = smpp_pdu_rx(esme, esme->read_msg); |
| 237 | esme->read_msg = NULL; |
| 238 | esme->read_idx = 0; |
| 239 | esme->read_len = 0; |
| 240 | esme->read_state = READ_ST_IN_LEN; |
| 241 | } |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | dead_socket: |
| 247 | msgb_free(esme->read_msg); |
| 248 | osmo_fd_unregister(&esme->wqueue.bfd); |
| 249 | close(esme->wqueue.bfd.fd); |
| 250 | esme->wqueue.bfd.fd = -1; |
| 251 | exit(2342); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Harald Welte | 7b1d25a | 2015-01-01 12:32:03 +0100 | [diff] [blame] | 256 | static int esme_write_cb(struct osmo_fd *ofd, struct msgb *msg) |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 257 | { |
| 258 | struct esme *esme = ofd->data; |
| 259 | int rc; |
| 260 | |
| 261 | rc = write(ofd->fd, msgb_data(msg), msgb_length(msg)); |
| 262 | if (rc == 0) { |
| 263 | osmo_fd_unregister(&esme->wqueue.bfd); |
| 264 | close(esme->wqueue.bfd.fd); |
| 265 | esme->wqueue.bfd.fd = -1; |
| 266 | exit(99); |
| 267 | } else if (rc < msgb_length(msg)) { |
| 268 | LOGP(DSMPP, LOGL_ERROR, "[%s] Short write\n", esme->system_id); |
Harald Welte | 7b1d25a | 2015-01-01 12:32:03 +0100 | [diff] [blame] | 269 | return 0; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 270 | } |
Harald Welte | 7b1d25a | 2015-01-01 12:32:03 +0100 | [diff] [blame] | 271 | |
| 272 | return 0; |
Harald Welte | c0de14d | 2012-11-23 23:35:01 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int smpp_esme_init(struct esme *esme, const char *host, uint16_t port) |
| 276 | { |
| 277 | int rc; |
| 278 | |
| 279 | if (port == 0) |
| 280 | port = 2775; |
| 281 | |
| 282 | esme->own_seq_nr = rand(); |
| 283 | esme_inc_seq_nr(esme); |
| 284 | osmo_wqueue_init(&esme->wqueue, 10); |
| 285 | esme->wqueue.bfd.data = esme; |
| 286 | esme->wqueue.read_cb = esme_read_cb; |
| 287 | esme->wqueue.write_cb = esme_write_cb; |
| 288 | |
| 289 | rc = osmo_sock_init_ofd(&esme->wqueue.bfd, AF_UNSPEC, SOCK_STREAM, |
| 290 | IPPROTO_TCP, host, port, OSMO_SOCK_F_CONNECT); |
| 291 | if (rc < 0) |
| 292 | return rc; |
| 293 | |
| 294 | return bind_transceiver(esme); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | int main(int argc, char **argv) |
| 299 | { |
| 300 | struct esme esme; |
| 301 | char *host = "localhost"; |
| 302 | int port = 0; |
| 303 | int rc; |
| 304 | |
| 305 | memset(&esme, 0, sizeof(esme)); |
| 306 | |
| 307 | osmo_init_logging(&log_info); |
| 308 | |
| 309 | snprintf((char *) esme.system_id, sizeof(esme.system_id), "mirror"); |
| 310 | snprintf((char *) esme.password, sizeof(esme.password), "mirror"); |
| 311 | esme.smpp_version = 0x34; |
| 312 | |
| 313 | if (argc >= 2) |
| 314 | host = argv[1]; |
| 315 | if (argc >= 3) |
| 316 | port = atoi(argv[2]); |
| 317 | |
| 318 | rc = smpp_esme_init(&esme, host, port); |
| 319 | if (rc < 0) |
| 320 | exit(1); |
| 321 | |
| 322 | while (1) { |
| 323 | osmo_select_main(0); |
| 324 | } |
| 325 | |
| 326 | exit(0); |
| 327 | } |