blob: c72f6bc27acf0d8e3f5f2e5c45ffe0cc124f22db [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
44int make_sock(struct bsc_fd *bfd, int proto, u_int16_t port,
45 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);
78 addr.sin_addr.s_addr = INADDR_ANY;
79
80 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
81
82 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
83 if (ret < 0) {
Harald Welte3a318ab2010-05-19 14:11:05 +020084 LOGP(DINP, LOGL_ERROR, "could not bind socket %s\n",
Harald Welte9f75c352010-04-30 20:26:32 +020085 strerror(errno));
86 close(bfd->fd);
87 return -EIO;
88 }
89
Harald Welte3a318ab2010-05-19 14:11:05 +020090 if (proto == IPPROTO_TCP) {
Harald Welte9f75c352010-04-30 20:26:32 +020091 ret = listen(bfd->fd, 1);
92 if (ret < 0) {
93 perror("listen");
94 return ret;
95 }
96 }
97
98 ret = bsc_register_fd(bfd);
99 if (ret < 0) {
100 perror("register_listen_fd");
101 close(bfd->fd);
102 return ret;
103 }
104 return 0;
105}