blob: 5ca7ec90f1a62f427404064daa716ec1d2782267 [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* OpenBSC sokcet code, taken from Abis input driver for ip.access */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by Holger Hans Peter Freyther
5 * (C) 2010 by On-Waves
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9f75c352010-04-30 20:26:32 +020012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9f75c352010-04-30 20:26:32 +020021 *
22 */
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <string.h>
29#include <time.h>
30#include <sys/fcntl.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010036#include <osmocom/core/select.h>
37#include <osmocom/gsm/tlv.h>
38#include <osmocom/core/msgb.h>
Harald Welte9f75c352010-04-30 20:26:32 +020039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010041#include <osmocom/core/talloc.h>
Harald Welte9f75c352010-04-30 20:26:32 +020042
Pablo Neira Ayuso165fe562011-04-05 18:33:24 +020043int make_sock(struct bsc_fd *bfd, int proto,
44 u_int32_t ip, u_int16_t port, int priv_nr,
45 int (*cb)(struct bsc_fd *fd, unsigned int what), void *data)
Harald Welte9f75c352010-04-30 20:26:32 +020046{
47 struct sockaddr_in addr;
48 int ret, on = 1;
49 int type = SOCK_STREAM;
50
Harald Welte3a318ab2010-05-19 14:11:05 +020051 switch (proto) {
52 case IPPROTO_TCP:
53 type = SOCK_STREAM;
54 break;
55 case IPPROTO_UDP:
Harald Welte9f75c352010-04-30 20:26:32 +020056 type = SOCK_DGRAM;
Harald Welte3a318ab2010-05-19 14:11:05 +020057 break;
58 case IPPROTO_GRE:
59 type = SOCK_RAW;
60 break;
61 default:
62 return -EINVAL;
63 }
Harald Welte9f75c352010-04-30 20:26:32 +020064
65 bfd->fd = socket(AF_INET, type, proto);
66 bfd->cb = cb;
67 bfd->when = BSC_FD_READ;
Pablo Neira Ayuso165fe562011-04-05 18:33:24 +020068 bfd->data = data;
69 bfd->priv_nr = priv_nr;
Harald Welte9f75c352010-04-30 20:26:32 +020070
71 if (bfd->fd < 0) {
Harald Welte3a318ab2010-05-19 14:11:05 +020072 LOGP(DINP, LOGL_ERROR, "could not create socket.\n");
Harald Welte9f75c352010-04-30 20:26:32 +020073 return -EIO;
74 }
75
76 memset(&addr, 0, sizeof(addr));
77 addr.sin_family = AF_INET;
78 addr.sin_port = htons(port);
Pablo Neira Ayuso165fe562011-04-05 18:33:24 +020079 if (ip != INADDR_ANY)
Harald Welte5540c4c2010-05-19 14:38:50 +020080 addr.sin_addr.s_addr = htonl(ip);
81 else
82 addr.sin_addr.s_addr = INADDR_ANY;
Harald Welte9f75c352010-04-30 20:26:32 +020083
84 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
85
86 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
87 if (ret < 0) {
Harald Welte3a318ab2010-05-19 14:11:05 +020088 LOGP(DINP, LOGL_ERROR, "could not bind socket %s\n",
Harald Welte9f75c352010-04-30 20:26:32 +020089 strerror(errno));
90 close(bfd->fd);
91 return -EIO;
92 }
93
Harald Welte3a318ab2010-05-19 14:11:05 +020094 if (proto == IPPROTO_TCP) {
Harald Welte9f75c352010-04-30 20:26:32 +020095 ret = listen(bfd->fd, 1);
96 if (ret < 0) {
97 perror("listen");
Holger Hans Peter Freyther9ecf6782010-10-12 23:19:37 +020098 close(bfd->fd);
Harald Welte9f75c352010-04-30 20:26:32 +020099 return ret;
100 }
101 }
102
103 ret = bsc_register_fd(bfd);
104 if (ret < 0) {
105 perror("register_listen_fd");
106 close(bfd->fd);
107 return ret;
108 }
109 return 0;
110}