blob: a5015ae1c04c7be6ee7ed67099e0f0db76ce5e1c [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");
49 return rc;
50 } else
51 msgb_put(msg, rc);
52
53 switch (sinfo.sinfo_ppid) {
54 case IUH_PPI_HNBAP:
55 rc = hnbgw_hnbap_rx(hnb, msg);
56 break;
57 case IUH_PPI_RUA:
Harald Welteb3dae302015-08-30 12:20:09 +020058 //rc = hnbgw_rua_rx(hnb, msg);
Harald Weltea2e6a7a2015-08-29 21:47:39 +020059 break;
60 case IUH_PPI_SABP:
61 case IUH_PPI_RNA:
62 case IUH_PPI_PUA:
63 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
64 sinfo.sinfo_ppid);
65 rc = 0;
66 break;
67 default:
68 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
69 sinfo.sinfo_ppid);
70 rc = 0;
71 break;
72 }
73
74 return rc;
75}
76
77/*! call-back when the listen FD has something to read */
78static int listen_fd_cb(struct osmo_fd *fd, unsigned int what)
79{
80 struct hnb_gw *gw = fd->data;
Harald Welteb3dae302015-08-30 12:20:09 +020081 struct hnb_context *ctx;
82 struct sockaddr_storage sockaddr;
Harald Weltea2e6a7a2015-08-29 21:47:39 +020083 socklen_t len = sizeof(sockaddr);
84
85 int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len);
86 if (new_fd < 0) {
87 LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n");
88 return new_fd;
89 }
90
91 LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n");
92
93 ctx = talloc_zero(tall_hnb_ctx, struct hnb_context);
94 if (!ctx)
95 return -ENOMEM;
96
97 ctx->gw = gw;
98 ctx->socket.data = ctx;
99 ctx->socket.fd = new_fd;
100 ctx->socket.when = BSC_FD_READ;
101 ctx->socket.cb = hnb_socket_cb;
Harald Welteb3dae302015-08-30 12:20:09 +0200102 osmo_fd_register(&ctx->socket);
103
104 llist_add_tail(&ctx->list, &gw->hnb_list);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200105
106 return 0;
107}
108
Harald Welteb3dae302015-08-30 12:20:09 +0200109static const struct log_info_cat log_cat[] = {
110 [DMAIN] = {
111 .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1,
112 .color = "",
113 .description = "Main program",
114 },
115};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200116
Harald Welteb3dae302015-08-30 12:20:09 +0200117static const struct log_info hnbgw_log_info = {
118 .cat = log_cat,
119 .num_cat = ARRAY_SIZE(log_cat),
120};
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200121
Harald Welteb3dae302015-08-30 12:20:09 +0200122static struct vty_app_info vty_info = {
123 .name = "OsmoHNBGW",
124 .version = "0",
125};
126
127static int daemonize = 0;
128
129int main(int argc, char **argv)
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200130{
Harald Welteb3dae302015-08-30 12:20:09 +0200131 int rc;
132
133 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
134
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200135 g_hnb_gw.listen_fd.cb = listen_fd_cb;
136 g_hnb_gw.listen_fd.when = BSC_FD_READ;
137 g_hnb_gw.listen_fd.data = &g_hnb_gw;
138
Harald Welteb3dae302015-08-30 12:20:09 +0200139 rc = osmo_init_logging(&hnbgw_log_info);
140 if (rc < 0)
141 exit(1);
142
143 vty_init(&vty_info);
144
145 rc = telnet_init(NULL, &g_hnb_gw, 2323);
146 if (rc < 0) {
147 perror("Error binding VTY port");
148 exit(1);
149 }
150
151 rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM,
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200152 IPPROTO_SCTP, "127.0.0.1",
153 g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND);
Harald Welteb3dae302015-08-30 12:20:09 +0200154 if (rc < 0) {
155 perror("Error binding Iuh port");
156 exit(1);
157 }
158
159 if (daemonize) {
160 rc = osmo_daemonize();
161 if (rc < 0) {
162 perror("Error during daemonize");
163 exit(1);
164 }
165 }
166
167 while (1) {
168 rc = osmo_select_main(0);
169 if (rc < 0)
170 exit(3);
171 }
172
173 /* not reached */
174 exit(0);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200175}