blob: 571bb12c268cbbee1a94ed31723ed99e7a5cb0fe [file] [log] [blame]
Harald Welteb3dae302015-08-30 12:20:09 +02001#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 Weltea2e6a7a2015-08-29 21:47:39 +02008
Harald Welteb3dae302015-08-30 12:20:09 +02009#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 Weltea2e6a7a2015-08-29 21:47:39 +020015#include <osmocom/core/select.h>
Harald Welteb3dae302015-08-30 12:20:09 +020016#include <osmocom/core/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020017#include <osmocom/core/socket.h>
Harald Welteb3dae302015-08-30 12:20:09 +020018#include <osmocom/core/msgb.h>
19
20#include <osmocom/vty/telnet_interface.h>
21#include <osmocom/vty/logging.h>
Harald Weltea2e6a7a2015-08-29 21:47:39 +020022
23#include "hnbgw.h"
Harald Welteb3dae302015-08-30 12:20:09 +020024#include "hnbgw_hnbap.h"
25
26static void *tall_hnb_ctx;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020027
28struct hnb_gw g_hnb_gw = {
29 .config = {
30 .iuh_listen_port = IUH_DEFAULT_SCTP_PORT,
31 },
32};
33
34static 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 Welteee77cff2015-08-30 16:57:53 +020049 /* FIXME: clean up after disappeared HNB */
Harald Weltea2e6a7a2015-08-29 21:47:39 +020050 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 Welteb3dae302015-08-30 12:20:09 +020059 //rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020060 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 */
79static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
80{
81 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +020082 struct hnb_context *ctx;
83 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020084 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 Welteb3dae302015-08-30 12:20:09 +0200103 osmo_fd_register(&ctx->socket);
104
105 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200106
107 return 0;
108}
109
Harald Welteb3dae302015-08-30 12:20:09 +0200110static 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 Weltea2e6a7a2015-08-29 21:47:39 +0200117
Harald Welteb3dae302015-08-30 12:20:09 +0200118static const struct log_info hnbgw_log_info = {
119 .cat = log_cat,
120 .num_cat = ARRAY_SIZE(log_cat),
121};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200122
Harald Welteb3dae302015-08-30 12:20:09 +0200123static struct vty_app_info vty_info = {
124 .name = "OsmoHNBGW",
125 .version = "0",
126};
127
128static int daemonize = 0;
129
130int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200131{
Harald Welteb3dae302015-08-30 12:20:09 +0200132 int rc;
133
134 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
135
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200136 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 Welteb3dae302015-08-30 12:20:09 +0200140 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 Weltea2e6a7a2015-08-29 21:47:39 +0200153 IPPROTO_SCTP, "127.0.0.1",
154 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200155 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 Weltea2e6a7a2015-08-29 21:47:39 +0200176}