aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/mncc_sock.c
blob: 437dec1853e52c3dce939dccfd0da4218c998fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* Mobie Network Call Control (MNCC) socket handling
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>
#include <unistd.h>
#include "sample.h"
#include "debug.h"
#include "call.h"
#include "mncc_sock.h"

static int listen_sock = -1;
static int mncc_sock = -1;

/* write to mncc socket, return error or -EIO if no socket connection */
int mncc_write(uint8_t *buf, int length)
{
	int rc;

	if (mncc_sock <= 0) {
		PDEBUG(DMNCC, DEBUG_NOTICE, "MNCC not connected.\n");
		return -EIO;
	}
	rc = send(mncc_sock, buf, length, 0);
	if (rc < 0) {
		PDEBUG(DMNCC, DEBUG_ERROR, "MNCC connection failed (errno = %d).\n", errno);
		mncc_sock_close();
		return 0;
	}
	if (rc != length) {
		PDEBUG(DMNCC, DEBUG_NOTICE, "MNCC write failed.\n");
		mncc_sock_close();
		return 0;
	}

	return rc;
}


/* read from mncc socket */
static int mncc_read(void)
{
	uint8_t buf[sizeof(struct gsm_mncc)+1024];
	int rc;

	memset(buf, 0, sizeof(buf));
	rc = recv(mncc_sock, buf, sizeof(buf), 0);
	if (rc == 0) {
		PDEBUG(DMNCC, DEBUG_NOTICE, "MNCC connection closed.\n");
		mncc_sock_close();
		return 0;
	}
	if (rc < 0) {
		if (errno == EWOULDBLOCK)
			return -errno;
		PDEBUG(DMNCC, DEBUG_ERROR, "MNCC connection failed (errno = %d).\n", errno);
		mncc_sock_close();
		return -errno;
	}

	call_mncc_recv(buf, rc);

	return rc;
}

static void mncc_hello(void)
{
	struct gsm_mncc_hello hello;

	memset(&hello, 0, sizeof(hello));
	hello.msg_type = MNCC_SOCKET_HELLO;
	hello.version = MNCC_SOCK_VERSION;
	hello.mncc_size = sizeof(struct gsm_mncc);
	hello.data_frame_size = sizeof(struct gsm_data_frame);
	hello.called_offset = offsetof(struct gsm_mncc, called);
	hello.signal_offset = offsetof(struct gsm_mncc, signal);
	hello.emergency_offset = offsetof(struct gsm_mncc, emergency);
	hello.lchan_type_offset = offsetof(struct gsm_mncc, lchan_type);

	mncc_write((uint8_t *) &hello, sizeof(hello));
}


static int mncc_accept(void)
{
	struct sockaddr_un __attribute__((__unused__)) un_addr;
	socklen_t __attribute__((__unused__)) len;
	int flags;
	int rc;

	len = sizeof(un_addr);
	rc = accept(listen_sock, (struct sockaddr *) &un_addr, &len);
	if (rc < 0) {
		if (errno == EWOULDBLOCK)
			return 0;
		PDEBUG(DMNCC, DEBUG_ERROR, "Failed to accept incoming connection (errno=%d).\n", errno);
		return rc;
	}

	if (mncc_sock > 0) {
		PDEBUG(DMNCC, DEBUG_NOTICE, "Rejecting multiple incoming connections.\n");
		close(rc);
		return -EIO;
	}

	mncc_sock = rc;
	flags = fcntl(mncc_sock, F_GETFL, 0);
	rc = fcntl(mncc_sock, F_SETFL, flags | O_NONBLOCK);
	if (rc < 0) {
		PDEBUG(DMNCC, DEBUG_ERROR, "Failed to set socket into non-blocking IO mode.\n");
		mncc_sock_close();
		return rc;
	}

	PDEBUG(DMNCC, DEBUG_NOTICE, "MNCC socket connected.\n");

	mncc_hello();

	return 1;
}

void mncc_handle(void)
{
	mncc_accept();

	if (mncc_sock > 0) {
		while ((mncc_read()) > 0)
			;
	}
}


void mncc_sock_close(void)
{
	if (mncc_sock > 0) {
		PDEBUG(DMNCC, DEBUG_NOTICE, "MNCC socket disconnected.\n");
		close(mncc_sock);
		mncc_sock = -1;
		/* clear all call instances */
		call_mncc_flush();
	}
}

int mncc_init(const char *sock_name)
{
	struct sockaddr_un local;
	unsigned int namelen;
	int flags;
	int rc;

	listen_sock = socket(PF_UNIX, SOCK_SEQPACKET, 0);
	if (listen_sock < 0) {
		PDEBUG(DMNCC, DEBUG_ERROR, "Failed to create socket.\n");
		return listen_sock;
	}

	local.sun_family = AF_UNIX;
	strncpy(local.sun_path, sock_name, sizeof(local.sun_path));
	local.sun_path[sizeof(local.sun_path) - 1] = '\0';
	unlink(local.sun_path);

	/* we use the same magic that X11 uses in Xtranssock.c for
	 * calculating the proper length of the sockaddr */
#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
	local.sun_len = strlen(local.sun_path);
#endif
#if defined(BSD44SOCKETS) || defined(SUN_LEN)
	namelen = SUN_LEN(&local);
#else
	namelen = strlen(local.sun_path) +
	offsetof(struct sockaddr_un, sun_path);
#endif

	rc = bind(listen_sock, (struct sockaddr *) &local, namelen);
	if (rc < 0) {
		PDEBUG(DMNCC, DEBUG_ERROR, "Failed to bind the unix domain "
			"socket. '%s'\n", local.sun_path);
		mncc_exit();
		return rc;
	}

	rc = listen(listen_sock, 0);
	if (rc < 0) {
		PDEBUG(DMNCC, DEBUG_ERROR, "Failed to listen.\n");
		mncc_exit();
		return rc;
	}

	flags = fcntl(listen_sock, F_GETFL, 0);
		flags = 0;
	rc = fcntl(listen_sock, F_SETFL, flags | O_NONBLOCK);
	if (rc < 0) {
		PDEBUG(DMNCC, DEBUG_ERROR, "Failed to set socket into non-blocking IO mode.\n");
		mncc_exit();
		return rc;
	}

	PDEBUG(DMNCC, DEBUG_DEBUG, "MNCC socket at '%s' initialized, waiting for connection.\n", sock_name);

	return 0;
}

void mncc_exit(void)
{
	mncc_sock_close();

	if (listen_sock > 0) {
		close(listen_sock);
		listen_sock = -1;
	}

	PDEBUG(DMNCC, DEBUG_DEBUG, "MNCC socket removed.\n");
}