blob: b430cf7ae35a385c5cd340554271eef89fe362d1 [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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <time.h>
31#include <sys/fcntl.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <arpa/inet.h>
36
37#include <osmocore/select.h>
38#include <osmocore/tlv.h>
39#include <osmocore/msgb.h>
40#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
42#include <osmocore/talloc.h>
43
Harald Welte5540c4c2010-05-19 14:38:50 +020044int make_sock(struct bsc_fd *bfd, int proto, u_int32_t ip, u_int16_t port,
Harald Welte9f75c352010-04-30 20:26:32 +020045 int (*cb)(struct bsc_fd *fd, unsigned int what))
46{
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;
68 //bfd->data = line;
69
70 if (bfd->fd < 0) {
Harald Welte3a318ab2010-05-19 14:11:05 +020071 LOGP(DINP, LOGL_ERROR, "could not create socket.\n");
Harald Welte9f75c352010-04-30 20:26:32 +020072 return -EIO;
73 }
74
75 memset(&addr, 0, sizeof(addr));
76 addr.sin_family = AF_INET;
77 addr.sin_port = htons(port);
Harald Welte5540c4c2010-05-19 14:38:50 +020078 if (ip)
79 addr.sin_addr.s_addr = htonl(ip);
80 else
81 addr.sin_addr.s_addr = INADDR_ANY;
Harald Welte9f75c352010-04-30 20:26:32 +020082
83 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
84
85 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
86 if (ret < 0) {
Harald Welte3a318ab2010-05-19 14:11:05 +020087 LOGP(DINP, LOGL_ERROR, "could not bind socket %s\n",
Harald Welte9f75c352010-04-30 20:26:32 +020088 strerror(errno));
89 close(bfd->fd);
90 return -EIO;
91 }
92
Harald Welte3a318ab2010-05-19 14:11:05 +020093 if (proto == IPPROTO_TCP) {
Harald Welte9f75c352010-04-30 20:26:32 +020094 ret = listen(bfd->fd, 1);
95 if (ret < 0) {
96 perror("listen");
Holger Hans Peter Freyther9ecf6782010-10-12 23:19:37 +020097 close(bfd->fd);
Harald Welte9f75c352010-04-30 20:26:32 +020098 return ret;
99 }
100 }
101
102 ret = bsc_register_fd(bfd);
103 if (ret < 0) {
104 perror("register_listen_fd");
105 close(bfd->fd);
106 return ret;
107 }
108 return 0;
109}