Harald Welte | d1991e7 | 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 |
| 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 Welte | ba1f931 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 44 | int make_sock(struct bsc_fd *bfd, int proto, u_int32_t ip, u_int16_t port, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 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 Welte | 7a0d4d8 | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 51 | switch (proto) { |
| 52 | case IPPROTO_TCP: |
| 53 | type = SOCK_STREAM; |
| 54 | break; |
| 55 | case IPPROTO_UDP: |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 56 | type = SOCK_DGRAM; |
Harald Welte | 7a0d4d8 | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 57 | break; |
| 58 | case IPPROTO_GRE: |
| 59 | type = SOCK_RAW; |
| 60 | break; |
| 61 | default: |
| 62 | return -EINVAL; |
| 63 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 64 | |
| 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 Welte | 7a0d4d8 | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 71 | LOGP(DINP, LOGL_ERROR, "could not create socket.\n"); |
Harald Welte | d1991e7 | 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); |
Harald Welte | ba1f931 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 78 | if (ip) |
| 79 | addr.sin_addr.s_addr = htonl(ip); |
| 80 | else |
| 81 | addr.sin_addr.s_addr = INADDR_ANY; |
Harald Welte | d1991e7 | 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) { |
Harald Welte | 7a0d4d8 | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 87 | LOGP(DINP, LOGL_ERROR, "could not bind socket %s\n", |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 88 | strerror(errno)); |
| 89 | close(bfd->fd); |
| 90 | return -EIO; |
| 91 | } |
| 92 | |
Harald Welte | 7a0d4d8 | 2010-05-19 14:11:05 +0200 | [diff] [blame] | 93 | if (proto == IPPROTO_TCP) { |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 94 | ret = listen(bfd->fd, 1); |
| 95 | if (ret < 0) { |
| 96 | perror("listen"); |
| 97 | return ret; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | ret = bsc_register_fd(bfd); |
| 102 | if (ret < 0) { |
| 103 | perror("register_listen_fd"); |
| 104 | close(bfd->fd); |
| 105 | return ret; |
| 106 | } |
| 107 | return 0; |
| 108 | } |