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 | |
| 44 | int 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 | |
| 51 | if (proto == IPPROTO_UDP) |
| 52 | type = SOCK_DGRAM; |
| 53 | |
| 54 | bfd->fd = socket(AF_INET, type, proto); |
| 55 | bfd->cb = cb; |
| 56 | bfd->when = BSC_FD_READ; |
| 57 | //bfd->data = line; |
| 58 | |
| 59 | if (bfd->fd < 0) { |
| 60 | LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n"); |
| 61 | return -EIO; |
| 62 | } |
| 63 | |
| 64 | memset(&addr, 0, sizeof(addr)); |
| 65 | addr.sin_family = AF_INET; |
| 66 | addr.sin_port = htons(port); |
| 67 | addr.sin_addr.s_addr = INADDR_ANY; |
| 68 | |
| 69 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 70 | |
| 71 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 72 | if (ret < 0) { |
| 73 | LOGP(DINP, LOGL_ERROR, "could not bind l2 socket %s\n", |
| 74 | strerror(errno)); |
| 75 | close(bfd->fd); |
| 76 | return -EIO; |
| 77 | } |
| 78 | |
| 79 | if (proto != IPPROTO_UDP) { |
| 80 | ret = listen(bfd->fd, 1); |
| 81 | if (ret < 0) { |
| 82 | perror("listen"); |
| 83 | return ret; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | ret = bsc_register_fd(bfd); |
| 88 | if (ret < 0) { |
| 89 | perror("register_listen_fd"); |
| 90 | close(bfd->fd); |
| 91 | return ret; |
| 92 | } |
| 93 | return 0; |
| 94 | } |