blob: 67ad4db9042a71d7c847927d6c93baf54c198dba [file] [log] [blame]
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +08001/* Routines to talk to the MSC using the IPA Protocol */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08004 * (C) 2010 by On-Waves
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +08005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <openbsc/bsc_msc.h>
24
25#include <arpa/inet.h>
26#include <sys/socket.h>
27#include <stdio.h>
28#include <string.h>
29#include <unistd.h>
30
31int connect_to_msc(struct bsc_fd *fd, const char *ip, int port)
32{
33 struct sockaddr_in sin;
34 int on = 1, ret;
35
36 printf("Attempting to connect MSC at %s:%d\n", ip, port);
37
38 fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
39 fd->when = BSC_FD_READ;
40 fd->data = NULL;
41 fd->priv_nr = 1;
42
43 if (fd->fd < 0) {
44 perror("Creating TCP socket failed");
45 return fd->fd;
46 }
47
48
49 memset(&sin, 0, sizeof(sin));
50 sin.sin_family = AF_INET;
51 sin.sin_port = htons(port);
52 inet_aton(ip, &sin.sin_addr);
53
54 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
55 ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin));
56
57 if (ret < 0) {
58 perror("Connection failed");
59 return ret;
60 }
61
62 ret = bsc_register_fd(fd);
63 if (ret < 0) {
64 perror("Registering the fd failed");
65 close(fd->fd);
66 return ret;
67 }
68
69 return ret;
70}