Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* 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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * 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 Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * 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 Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 21 | * |
| 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 Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 31 | #include <sys/socket.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <arpa/inet.h> |
| 34 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 35 | #include <osmocom/core/select.h> |
| 36 | #include <osmocom/gsm/tlv.h> |
| 37 | #include <osmocom/core/msgb.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 38 | #include <openbsc/debug.h> |
| 39 | #include <openbsc/gsm_data.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 40 | #include <osmocom/core/talloc.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 41 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 42 | int make_sock(struct osmo_fd *bfd, int proto, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 43 | uint32_t ip, uint16_t port, int priv_nr, |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 44 | int (*cb)(struct osmo_fd *fd, unsigned int what), void *data) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | { |
| 46 | struct sockaddr_in addr; |
| 47 | int ret, on = 1; |
| 48 | int type = SOCK_STREAM; |
| 49 | |
Harald Welte | 3a318ab | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 50 | switch (proto) { |
| 51 | case IPPROTO_TCP: |
| 52 | type = SOCK_STREAM; |
| 53 | break; |
| 54 | case IPPROTO_UDP: |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 55 | type = SOCK_DGRAM; |
Harald Welte | 3a318ab | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 56 | break; |
Harald Welte | a334c29 | 2012-04-08 15:56:00 +0200 | [diff] [blame] | 57 | #ifdef IPPROTO_GRE |
Harald Welte | 3a318ab | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 58 | case IPPROTO_GRE: |
| 59 | type = SOCK_RAW; |
| 60 | break; |
Harald Welte | a334c29 | 2012-04-08 15:56:00 +0200 | [diff] [blame] | 61 | #endif |
Harald Welte | 3a318ab | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 62 | default: |
| 63 | return -EINVAL; |
| 64 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 65 | |
| 66 | bfd->fd = socket(AF_INET, type, proto); |
| 67 | bfd->cb = cb; |
| 68 | bfd->when = BSC_FD_READ; |
Pablo Neira Ayuso | 165fe56 | 2011-04-05 18:33:24 +0200 | [diff] [blame] | 69 | bfd->data = data; |
| 70 | bfd->priv_nr = priv_nr; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 71 | |
| 72 | if (bfd->fd < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 73 | LOGP(DLINP, LOGL_ERROR, "could not create socket.\n"); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 74 | return -EIO; |
| 75 | } |
| 76 | |
| 77 | memset(&addr, 0, sizeof(addr)); |
| 78 | addr.sin_family = AF_INET; |
| 79 | addr.sin_port = htons(port); |
Pablo Neira Ayuso | 165fe56 | 2011-04-05 18:33:24 +0200 | [diff] [blame] | 80 | if (ip != INADDR_ANY) |
Harald Welte | 5540c4c | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 81 | addr.sin_addr.s_addr = htonl(ip); |
| 82 | else |
| 83 | addr.sin_addr.s_addr = INADDR_ANY; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 84 | |
| 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 Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 89 | LOGP(DLINP, LOGL_ERROR, "could not bind socket %s\n", |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 90 | strerror(errno)); |
| 91 | close(bfd->fd); |
| 92 | return -EIO; |
| 93 | } |
| 94 | |
Harald Welte | 3a318ab | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 95 | if (proto == IPPROTO_TCP) { |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 96 | ret = listen(bfd->fd, 1); |
| 97 | if (ret < 0) { |
| 98 | perror("listen"); |
Holger Hans Peter Freyther | 9ecf678 | 2010-10-12 23:19:37 +0200 | [diff] [blame] | 99 | close(bfd->fd); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 100 | return ret; |
| 101 | } |
| 102 | } |
| 103 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 104 | ret = osmo_fd_register(bfd); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 105 | if (ret < 0) { |
| 106 | perror("register_listen_fd"); |
| 107 | close(bfd->fd); |
| 108 | return ret; |
| 109 | } |
| 110 | return 0; |
| 111 | } |