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; |
| 57 | case IPPROTO_GRE: |
| 58 | type = SOCK_RAW; |
| 59 | break; |
| 60 | default: |
| 61 | return -EINVAL; |
| 62 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 63 | |
| 64 | bfd->fd = socket(AF_INET, type, proto); |
| 65 | bfd->cb = cb; |
| 66 | bfd->when = BSC_FD_READ; |
Pablo Neira Ayuso | 165fe56 | 2011-04-05 18:33:24 +0200 | [diff] [blame] | 67 | bfd->data = data; |
| 68 | bfd->priv_nr = priv_nr; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 69 | |
| 70 | if (bfd->fd < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 71 | LOGP(DLINP, LOGL_ERROR, "could not create socket.\n"); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 72 | return -EIO; |
| 73 | } |
| 74 | |
| 75 | memset(&addr, 0, sizeof(addr)); |
| 76 | addr.sin_family = AF_INET; |
| 77 | addr.sin_port = htons(port); |
Pablo Neira Ayuso | 165fe56 | 2011-04-05 18:33:24 +0200 | [diff] [blame] | 78 | if (ip != INADDR_ANY) |
Harald Welte | 5540c4c | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 79 | addr.sin_addr.s_addr = htonl(ip); |
| 80 | else |
| 81 | addr.sin_addr.s_addr = INADDR_ANY; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 82 | |
| 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) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 87 | LOGP(DLINP, LOGL_ERROR, "could not bind socket %s\n", |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 88 | strerror(errno)); |
| 89 | close(bfd->fd); |
| 90 | return -EIO; |
| 91 | } |
| 92 | |
Harald Welte | 3a318ab | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 93 | if (proto == IPPROTO_TCP) { |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 94 | ret = listen(bfd->fd, 1); |
| 95 | if (ret < 0) { |
| 96 | perror("listen"); |
Holger Hans Peter Freyther | 9ecf678 | 2010-10-12 23:19:37 +0200 | [diff] [blame] | 97 | close(bfd->fd); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 98 | return ret; |
| 99 | } |
| 100 | } |
| 101 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 102 | ret = osmo_fd_register(bfd); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 103 | if (ret < 0) { |
| 104 | perror("register_listen_fd"); |
| 105 | close(bfd->fd); |
| 106 | return ret; |
| 107 | } |
| 108 | return 0; |
| 109 | } |