aboutsummaryrefslogtreecommitdiffstats
path: root/src/proto.c
blob: 479175aa50c6ba07f7f0e8ad83540aac13a59068 (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
/*
 * proto.c
 *
 * (C) 2019 by Sylvain Munaut <tnt@246tNt.com>
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

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

#include <osmocom/core/logging.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/select.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/utils.h>

#include <osmocom/e1d/proto.h>

#include "log.h"

const struct value_string osmo_e1dp_msg_type_names[] = {
	{ E1DP_CMD_INTF_QUERY,	"CMD_INTF_QUERY" },
	{ E1DP_CMD_LINE_QUERY,	"CMD_LINE_QUERY" },
	{ E1DP_CMD_TS_QUERY,	"CMD_TS_QUERY" },
	{ E1DP_CMD_TS_OPEN,	"CMD_TS_OPEN" },
	{ E1DP_EVT_TYPE,	"EVT_TYPE" },
	{ E1DP_RESP_TYPE,	"RESP_TYPE" },
	{ E1DP_ERR_TYPE,	"ERR_TYPE" },
	{ 0, NULL }
};
const struct value_string osmo_e1dp_line_mode_names[] = {
	{ E1DP_LMODE_OFF,		"OFF" },
	{ E1DP_LMODE_CHANNELIZED,	"CHANNELIZED" },
	{ E1DP_LMODE_SUPERCHANNEL,	"SUPERCHANNEL" },
	{ 0, NULL }
};
const struct value_string osmo_e1dp_ts_mode_names[] = {
	{ E1DP_TSMODE_OFF,	"OFF" },
	{ E1DP_TSMODE_RAW,	"RAW" },
	{ E1DP_TSMODE_HDLCFCS,	"HDLC-FCS" },
	{ 0, NULL }
};

struct msgb *
osmo_e1dp_recv(struct osmo_fd *ofd, int *fd)
{
	struct msgb *msgb;
	struct osmo_e1dp_msg_hdr *hdr;
	struct iovec iov;
	struct msghdr msg;
	struct cmsghdr *cmsg;
	char cms[CMSG_SPACE(sizeof(int))];
	int rc;

	msgb = msgb_alloc(E1DP_MAX_LEN, "e1d proto rx message");

	memset(&msg, 0x00, sizeof(msg));

	iov.iov_base = msgb->data;
	iov.iov_len = E1DP_MAX_LEN;

	msg.msg_name = 0;
	msg.msg_namelen = 0;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_flags = 0;
	msg.msg_control = (caddr_t) cms;
	msg.msg_controllen = sizeof(cms);

	rc = recvmsg(ofd->fd, &msg, MSG_WAITALL | MSG_CMSG_CLOEXEC);
	if (rc == 0)
		goto err;
	if (rc < (int)sizeof(struct osmo_e1dp_msg_hdr)) {
		LOGP(DE1D, LOGL_ERROR, "Failed to read packet header.\n");
		goto err;
	}

	msgb->l1h = msgb_put(msgb, sizeof(struct osmo_e1dp_msg_hdr));
	hdr = msgb_l1(msgb);

	if ((hdr->magic != E1DP_MAGIC) || (hdr->len < sizeof(struct osmo_e1dp_msg_hdr))) {
		LOGP(DE1D, LOGL_ERROR, "Invalid packet header.\n");
		goto err;
	}

	if (hdr->len > sizeof(struct osmo_e1dp_msg_hdr))
		msgb->l2h = msgb_put(msgb, hdr->len - sizeof(struct osmo_e1dp_msg_hdr));
	else
		msgb->l2h = msgb->tail;

	if (fd) {
		cmsg = CMSG_FIRSTHDR(&msg);
		if (cmsg) {
			memmove(fd, CMSG_DATA(cmsg), sizeof(int));
		} else {
			*fd = -1;
		}
	}

	LOGP(DE1D, LOGL_DEBUG, "rx pkt: %d %s\n", fd ? *fd : -2, msgb_hexdump(msgb));

	return msgb;

err:
	msgb_free(msgb);
	return NULL;
}

int
osmo_e1dp_send(struct osmo_fd *ofd, struct msgb *msgb, int fd)
{
	struct msghdr msg;
	struct iovec iov;
	char cmsgbuf[CMSG_SPACE(sizeof(int))];
	int rc;

	memset(&msg, 0x00, sizeof(msg));

	iov.iov_base = msgb->data;
	iov.iov_len  = msgb_length(msgb);

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_flags = 0;

	if (fd >= 0) {
		msg.msg_control = cmsgbuf;
		msg.msg_controllen = CMSG_LEN(sizeof(int));

		struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
		cmsg->cmsg_level = SOL_SOCKET;
		cmsg->cmsg_type = SCM_RIGHTS;
		cmsg->cmsg_len = CMSG_LEN(sizeof(int));

		memmove(CMSG_DATA(cmsg), &fd, sizeof(int));
	}

	rc = sendmsg(ofd->fd, &msg, 0);
	if (rc < 0) {
		LOGP(DE1D, LOGL_ERROR, "Failed to send packet.\n");
		perror("tx");
	}

	if (fd >= 0)
		close(fd);

	LOGP(DE1D, LOGL_DEBUG, "tx pkt: %d %s\n", msgb_length(msgb), msgb_hexdump(msgb));

	return rc;
}