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