blob: 2a64767f817d4a754a03e6ce559a9c97583590e0 [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>
Harald Welte9f75c352010-04-30 20:26:32 +020031#include <sys/socket.h>
32#include <sys/ioctl.h>
33#include <arpa/inet.h>
34
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010035#include <osmocom/core/select.h>
36#include <osmocom/gsm/tlv.h>
37#include <osmocom/core/msgb.h>
Harald Welte9f75c352010-04-30 20:26:32 +020038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010040#include <osmocom/core/talloc.h>
Harald Welte9f75c352010-04-30 20:26:32 +020041
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020042int make_sock(struct osmo_fd *bfd, int proto,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020043 uint32_t ip, uint16_t port, int priv_nr,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020044 int (*cb)(struct osmo_fd *fd, unsigned int what), void *data)
Harald Welte9f75c352010-04-30 20:26:32 +020045{
46 struct sockaddr_in addr;
47 int ret, on = 1;
48 int type = SOCK_STREAM;
49
Harald Welte3a318ab2010-05-19 14:11:05 +020050 switch (proto) {
51 case IPPROTO_TCP:
52 type = SOCK_STREAM;
53 break;
54 case IPPROTO_UDP:
Harald Welte9f75c352010-04-30 20:26:32 +020055 type = SOCK_DGRAM;
Harald Welte3a318ab2010-05-19 14:11:05 +020056 break;
Harald Weltea334c292012-04-08 15:56:00 +020057#ifdef IPPROTO_GRE
Harald Welte3a318ab2010-05-19 14:11:05 +020058 case IPPROTO_GRE:
59 type = SOCK_RAW;
60 break;
Harald Weltea334c292012-04-08 15:56:00 +020061#endif
Harald Welte3a318ab2010-05-19 14:11:05 +020062 default:
63 return -EINVAL;
64 }
Harald Welte9f75c352010-04-30 20:26:32 +020065
66 bfd->fd = socket(AF_INET, type, proto);
67 bfd->cb = cb;
68 bfd->when = BSC_FD_READ;
Pablo Neira Ayuso165fe562011-04-05 18:33:24 +020069 bfd->data = data;
70 bfd->priv_nr = priv_nr;
Harald Welte9f75c352010-04-30 20:26:32 +020071
72 if (bfd->fd < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020073 LOGP(DLINP, LOGL_ERROR, "could not create socket.\n");
Harald Welte9f75c352010-04-30 20:26:32 +020074 return -EIO;
75 }
76
77 memset(&addr, 0, sizeof(addr));
78 addr.sin_family = AF_INET;
79 addr.sin_port = htons(port);
Pablo Neira Ayuso165fe562011-04-05 18:33:24 +020080 if (ip != INADDR_ANY)
Harald Welte5540c4c2010-05-19 14:38:50 +020081 addr.sin_addr.s_addr = htonl(ip);
82 else
83 addr.sin_addr.s_addr = INADDR_ANY;
Harald Welte9f75c352010-04-30 20:26:32 +020084
85 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
86
87 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
88 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020089 LOGP(DLINP, LOGL_ERROR, "could not bind socket %s\n",
Harald Welte9f75c352010-04-30 20:26:32 +020090 strerror(errno));
91 close(bfd->fd);
92 return -EIO;
93 }
94
Harald Welte3a318ab2010-05-19 14:11:05 +020095 if (proto == IPPROTO_TCP) {
Harald Welte9f75c352010-04-30 20:26:32 +020096 ret = listen(bfd->fd, 1);
97 if (ret < 0) {
98 perror("listen");
Holger Hans Peter Freyther9ecf6782010-10-12 23:19:37 +020099 close(bfd->fd);
Harald Welte9f75c352010-04-30 20:26:32 +0200100 return ret;
101 }
102 }
103
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200104 ret = osmo_fd_register(bfd);
Harald Welte9f75c352010-04-30 20:26:32 +0200105 if (ret < 0) {
106 perror("register_listen_fd");
107 close(bfd->fd);
108 return ret;
109 }
110 return 0;
111}