summaryrefslogtreecommitdiff
path: root/libs/midi++2/ipmidi_port.cc
blob: 862d9e672314635f9379994fc4b0473b3e7a001c (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (C) 2012-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <iostream>
#include <cstdio>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef   COMPILER_MSVC
#undef   O_NONBLOCK
#define  O_NONBLOCK 0
#endif
#if defined(PLATFORM_WINDOWS)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#endif

#if defined(PLATFORM_WINDOWS)
typedef int socklen_t;
#else
#include <unistd.h>
#include <sys/ioctl.h>
inline void closesocket(int s) { ::close(s); }
#endif

#include "pbd/xml++.h"
#include "pbd/error.h"
#include "pbd/failed_constructor.h"
#include "pbd/convert.h"
#include "pbd/compose.h"

#include "midi++/types.h"
#include "midi++/ipmidi_port.h"
#include "midi++/channel.h"

using namespace MIDI;
using namespace std;
using namespace PBD;

IPMIDIPort::IPMIDIPort (int base_port, const string& iface)
	: Port (string_compose ("IPmidi@%1", base_port), Port::Flags (Port::IsInput|Port::IsOutput))
	, sockin (-1)
	, sockout (-1)
{
	if (!open_sockets (base_port, iface)) {
		throw (failed_constructor ());
	}
}

IPMIDIPort::IPMIDIPort (const XMLNode& node)
	: Port (node)
{
	/* base class does not class set_state() */
	set_state (node);
}

IPMIDIPort::~IPMIDIPort ()
{
	close_sockets ();
}

int
IPMIDIPort::selectable () const
{
	return sockin;
}

XMLNode&
IPMIDIPort::get_state () const
{
	return Port::get_state ();
}

void
IPMIDIPort::set_state (const XMLNode& node)
{
	Port::set_state (node);
}

void
IPMIDIPort::close_sockets ()
{
	if (sockin >= 0) {
		::closesocket (sockin);
		sockin = -1;
	}

	if (sockout >= 0) {
		::closesocket (sockout);
		sockout = -1;
	}
}

#ifndef PLATFORM_WINDOWS
static bool
get_address (int sock, struct in_addr *inaddr, const string& ifname )
{
	// Get interface address from supplied name.

	struct ifreq ifr;
	::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));

	if (::ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
		::perror("ioctl(SIOCGIFFLAGS)");
		return false;
	}

	if ((ifr.ifr_flags & IFF_UP) == 0) {
		error << string_compose ("interface %1 is down", ifname) << endmsg;
		return false;
	}

	if (::ioctl(sock, SIOCGIFADDR, (char *) &ifr)) {
		::perror("ioctl(SIOCGIFADDR)");
		return false;
	}

	struct sockaddr_in sa;
	::memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
	inaddr->s_addr = sa.sin_addr.s_addr;

	return true;
}
#endif

bool
IPMIDIPort::open_sockets (int base_port, const string& ifname)
{
	int protonum = 0;
	struct protoent *proto = ::getprotobyname("IP");

	if (proto) {
		protonum = proto->p_proto;
	}

	sockin = ::socket (PF_INET, SOCK_DGRAM, protonum);
	if (sockin < 0) {
		::perror("socket(in)");
		return false;
	}

	struct sockaddr_in addrin;
	::memset(&addrin, 0, sizeof(addrin));
	addrin.sin_family = AF_INET;
	addrin.sin_addr.s_addr = htonl(INADDR_ANY);
	addrin.sin_port = htons(base_port);

	if (::bind(sockin, (struct sockaddr *) (&addrin), sizeof(addrin)) < 0) {
		::perror("bind");
		return false;
	}

	// Will Hall, 2007
	// INADDR_ANY will bind to default interface,
	// specify alternate interface nameon which to bind...
	struct in_addr if_addr_in;
#ifndef PLATFORM_WINDOWS
	if (!ifname.empty()) {
		if (!get_address(sockin, &if_addr_in, ifname)) {
			error << string_compose ("socket(in): could not find interface address for %1", ifname) << endmsg;
			return false;
		}
		if (::setsockopt(sockin, IPPROTO_IP, IP_MULTICAST_IF,
				 (char *) &if_addr_in, sizeof(if_addr_in))) {
			::perror("setsockopt(IP_MULTICAST_IF)");
			return false;
		}
	} else {
		if_addr_in.s_addr = htonl (INADDR_ANY);
	}
#else
	if_addr_in.s_addr = htonl (INADDR_ANY);
#endif

	struct ip_mreq mreq;
	mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37"); /* ipMIDI group multicast address */
	mreq.imr_interface.s_addr = if_addr_in.s_addr;
	if(::setsockopt (sockin, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)) < 0) {
		::perror("setsockopt(IP_ADD_MEMBERSHIP)");
		fprintf(stderr, "socket(in): your kernel is probably missing multicast support.\n");
		return false;
	}

	// Output socket...

	sockout = ::socket (AF_INET, SOCK_DGRAM, protonum);

	if (sockout < 0) {
		::perror("socket(out)");
		return false;
	}

	// Will Hall, Oct 2007
#ifndef PLATFORM_WINDOWS
	if (!ifname.empty()) {
		struct in_addr if_addr_out;
		if (!get_address(sockout, &if_addr_out, ifname)) {
			error << string_compose ("socket(out): could not find interface address for %1", ifname) << endmsg;
			return false;
		}
		if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_IF, (char *) &if_addr_out, sizeof(if_addr_out))) {
			::perror("setsockopt(IP_MULTICAST_IF)");
			return false;
		}
	}
#endif

	::memset(&addrout, 0, sizeof(struct sockaddr_in));
	addrout.sin_family = AF_INET;
	addrout.sin_addr.s_addr = ::inet_addr("225.0.0.37");
	addrout.sin_port = htons (base_port);

	// Turn off loopback...
	int loop = 0;

#ifdef PLATFORM_WINDOWS

	/* https://msdn.microsoft.com/en-us/library/windows/desktop/ms739161%28v=vs.85%29.aspx
	 *
	 * ------------------------------------------------------------------------------
	 * Note The Winsock version of the IP_MULTICAST_LOOP option is
	 * semantically different than the UNIX version of the
	 * IP_MULTICAST_LOOP option:
	 *
	 * In Winsock, the IP_MULTICAST_LOOP option applies only to the receive path.
	 * In the UNIX version, the IP_MULTICAST_LOOP option applies to the send path.
	 *
	 * For example, applications ON and OFF (which are easier to track than
	 * X and Y) join the same group on the same interface; application ON
	 * sets the IP_MULTICAST_LOOP option on, application OFF sets the
	 * IP_MULTICAST_LOOP option off. If ON and OFF are Winsock
	 * applications, OFF can send to ON, but ON cannot sent to OFF. In
	 * contrast, if ON and OFF are UNIX applications, ON can send to OFF,
	 * but OFF cannot send to ON.
	 * ------------------------------------------------------------------------------
	 *
	 * Alles klar? Gut! 
	 */

	const int target_sock = sockin;
#else
	const int target_sock = sockout;
#endif

	if (::setsockopt (target_sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
		::perror("setsockopt(IP_MULTICAST_LOOP)");
		return false;
	}

#ifndef PLATFORM_WINDOWS

	if (fcntl (sockin, F_SETFL, O_NONBLOCK)) {
		error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
		return false;
	}

	if (fcntl (sockout, F_SETFL, O_NONBLOCK)) {
		error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
		return false;
	}

#else
	// If imode !=0, non-blocking mode is enabled.
	u_long mode=1;
	if (ioctlsocket(sockin,FIONBIO,&mode)) {
		error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
		return false;
	}
	mode = 1; /* just in case it was modified in the previous call */
	if (ioctlsocket(sockout,FIONBIO,&mode)) {
		error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
		return false;
	}
#endif

	return true;
}

int
IPMIDIPort::write (const MIDI::byte* msg, size_t msglen, timestamp_t /* ignored */) {

	if (sockout) {
		Glib::Threads::Mutex::Lock lm (write_lock);
		if (::sendto (sockout, (const char *) msg, msglen, 0, (struct sockaddr *) &addrout, sizeof(struct sockaddr_in)) < 0) {
			::perror("sendto");
			return -1;
		}
		return msglen;
	}
	return 0;
}

int
IPMIDIPort::read (MIDI::byte* /*buf*/, size_t /*bufsize*/)
{
	/* nothing to do here - all handled by parse() */
	return 0;
}

void
IPMIDIPort::parse (samplecnt_t timestamp)
{
	/* input was detected on the socket, so go get it and hand it to the
	 * parser. This will emit appropriate signals that will be handled
	 * by anyone who cares.
	 */

	unsigned char buf[1024];
	struct sockaddr_in sender;
	socklen_t slen = sizeof(sender);
	int r = ::recvfrom (sockin, (char *) buf, sizeof(buf), 0, (struct sockaddr *) &sender, &slen);

	if (r >= 0) {

		_parser->set_timestamp (timestamp);

		for (int i = 0; i < r; ++i) {
			_parser->scanner (buf[i]);
		}
	} else {
		::perror ("failed to recv from socket");
	}
}