summaryrefslogtreecommitdiff
path: root/src/libgsmd/libgsmd_passthrough.c
blob: 5362e4ef506e4144d8a6495f67e18226fbc3ba12 (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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <gsmd/usock.h>
#include <libgsmd/libgsmd.h>

#include "lgsm_internals.h"

#define PT_BUF_SIZE	1024
static char passthrough_buf[sizeof(struct gsmd_msg_hdr)+PT_BUF_SIZE];
static char passthrough_rbuf[sizeof(struct gsmd_msg_hdr)+PT_BUF_SIZE];

int lgsm_passthrough_send(struct lgsm_handle *lh, const char *tx)
{
	struct gsmd_msg_hdr *gmh = (struct gsmd_msg_hdr *)passthrough_buf;
	char *tx_buf = (char *)gmh + sizeof(*gmh);
	int len = strlen(tx);

	if (len > PT_BUF_SIZE)
		return -EINVAL;

	gmh->version = GSMD_PROTO_VERSION;
	gmh->msg_type = GSMD_MSG_PASSTHROUGH;
	gmh->msg_subtype = GSMD_PASSTHROUGH_REQ;
	gmh->len = len+1;
	strcpy(tx_buf, tx);

	if (lgsm_send(lh, gmh) < len+sizeof(*gmh))
		return -EIO;

	return gmh->id;
}

int lgsm_passthrough(struct lgsm_handle *lh, const char *tx, char *rx, unsigned int *rx_len)
{
	struct gsmd_msg_hdr *rgmh = (struct gsmd_msg_hdr *)passthrough_rbuf;
	char *rx_buf = (char *)rgmh + sizeof(*rgmh);
	int rc;

	rc = lgsm_passthrough_send(lh, tx);
	if (rc < 0)
		return rc;

	/* since we synchronously want to wait for a response, we need to
	 * _internally_ loop over incoming packets and call the callbacks for
	 * intermediate messages (if applicable) */
	rc = lgsm_blocking_wait_packet(lh, rc, passthrough_rbuf, 
					sizeof(passthrough_rbuf));
	if (rc <= 0)
		return rc;

	if (rc < sizeof(*rgmh))
		return -EINVAL;

	if (rc < sizeof(*rgmh) + rgmh->len)
		return -EINVAL;
	
	/* FIXME: make sure rx_buf is zero-terminated */
	strcpy(rx, rx_buf);
	*rx_len = rgmh->len;

	return rx_len;
}
personal git repositories of Harald Welte. Your mileage may vary