Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <getopt.h> |
| 6 | #include <errno.h> |
| 7 | #include <signal.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 8 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <netinet/sctp.h> |
| 12 | |
| 13 | #include <osmocom/core/application.h> |
| 14 | #include <osmocom/core/talloc.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 15 | #include <osmocom/core/select.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 16 | #include <osmocom/core/logging.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 17 | #include <osmocom/core/socket.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 18 | #include <osmocom/core/msgb.h> |
| 19 | |
| 20 | #include <osmocom/vty/telnet_interface.h> |
| 21 | #include <osmocom/vty/logging.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 22 | |
| 23 | #include "hnbgw.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 24 | #include "hnbgw_hnbap.h" |
| 25 | |
| 26 | static void *tall_hnb_ctx; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 27 | |
| 28 | struct hnb_gw g_hnb_gw = { |
| 29 | .config = { |
| 30 | .iuh_listen_port = IUH_DEFAULT_SCTP_PORT, |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | static int hnb_socket_cb(struct osmo_fd *fd, unsigned int what) |
| 35 | { |
| 36 | struct hnb_context *hnb = fd->data; |
| 37 | struct sctp_sndrcvinfo sinfo; |
| 38 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
| 39 | int flags; |
| 40 | int rc; |
| 41 | |
| 42 | if (!msg) |
| 43 | return -ENOMEM; |
| 44 | |
| 45 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 46 | NULL, NULL, &sinfo, &flags); |
| 47 | if (rc < 0) { |
| 48 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
Harald Welte | ee77cff | 2015-08-30 16:57:53 +0200 | [diff] [blame] | 49 | /* FIXME: clean up after disappeared HNB */ |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 50 | return rc; |
| 51 | } else |
| 52 | msgb_put(msg, rc); |
| 53 | |
| 54 | switch (sinfo.sinfo_ppid) { |
| 55 | case IUH_PPI_HNBAP: |
| 56 | rc = hnbgw_hnbap_rx(hnb, msg); |
| 57 | break; |
| 58 | case IUH_PPI_RUA: |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 59 | //rc = hnbgw_rua_rx(hnb, msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 60 | break; |
| 61 | case IUH_PPI_SABP: |
| 62 | case IUH_PPI_RNA: |
| 63 | case IUH_PPI_PUA: |
| 64 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 65 | sinfo.sinfo_ppid); |
| 66 | rc = 0; |
| 67 | break; |
| 68 | default: |
| 69 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 70 | sinfo.sinfo_ppid); |
| 71 | rc = 0; |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | return rc; |
| 76 | } |
| 77 | |
| 78 | /*! call-back when the listen FD has something to read */ |
| 79 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 80 | { |
| 81 | struct hnb_gw *gw = fd->data; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 82 | struct hnb_context *ctx; |
| 83 | struct sockaddr_storage sockaddr; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 84 | socklen_t len = sizeof(sockaddr); |
| 85 | |
| 86 | int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len); |
| 87 | if (new_fd < 0) { |
| 88 | LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n"); |
| 89 | return new_fd; |
| 90 | } |
| 91 | |
| 92 | LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n"); |
| 93 | |
| 94 | ctx = talloc_zero(tall_hnb_ctx, struct hnb_context); |
| 95 | if (!ctx) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | ctx->gw = gw; |
| 99 | ctx->socket.data = ctx; |
| 100 | ctx->socket.fd = new_fd; |
| 101 | ctx->socket.when = BSC_FD_READ; |
| 102 | ctx->socket.cb = hnb_socket_cb; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 103 | osmo_fd_register(&ctx->socket); |
| 104 | |
| 105 | llist_add_tail(&ctx->list, &gw->hnb_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 110 | static const struct log_info_cat log_cat[] = { |
| 111 | [DMAIN] = { |
| 112 | .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 113 | .color = "", |
| 114 | .description = "Main program", |
| 115 | }, |
| 116 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 117 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 118 | static const struct log_info hnbgw_log_info = { |
| 119 | .cat = log_cat, |
| 120 | .num_cat = ARRAY_SIZE(log_cat), |
| 121 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 122 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 123 | static struct vty_app_info vty_info = { |
| 124 | .name = "OsmoHNBGW", |
| 125 | .version = "0", |
| 126 | }; |
| 127 | |
| 128 | static int daemonize = 0; |
| 129 | |
| 130 | int main(int argc, char **argv) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 131 | { |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 132 | int rc; |
| 133 | |
| 134 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
| 135 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 136 | g_hnb_gw.listen_fd.cb = listen_fd_cb; |
| 137 | g_hnb_gw.listen_fd.when = BSC_FD_READ; |
| 138 | g_hnb_gw.listen_fd.data = &g_hnb_gw; |
| 139 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 140 | rc = osmo_init_logging(&hnbgw_log_info); |
| 141 | if (rc < 0) |
| 142 | exit(1); |
| 143 | |
| 144 | vty_init(&vty_info); |
| 145 | |
| 146 | rc = telnet_init(NULL, &g_hnb_gw, 2323); |
| 147 | if (rc < 0) { |
| 148 | perror("Error binding VTY port"); |
| 149 | exit(1); |
| 150 | } |
| 151 | |
| 152 | rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM, |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 153 | IPPROTO_SCTP, "127.0.0.1", |
| 154 | g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 155 | if (rc < 0) { |
| 156 | perror("Error binding Iuh port"); |
| 157 | exit(1); |
| 158 | } |
| 159 | |
| 160 | if (daemonize) { |
| 161 | rc = osmo_daemonize(); |
| 162 | if (rc < 0) { |
| 163 | perror("Error during daemonize"); |
| 164 | exit(1); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | while (1) { |
| 169 | rc = osmo_select_main(0); |
| 170 | if (rc < 0) |
| 171 | exit(3); |
| 172 | } |
| 173 | |
| 174 | /* not reached */ |
| 175 | exit(0); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 176 | } |