2 Copyright (C) 2012 Paul Davis
4 Using code from Rui Nuno Capela's qmidinet as inspiration.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: port.cc 12065 2012-04-23 16:23:48Z paul $
32 #if defined(PLATFORM_WINDOWS)
38 #if defined(PLATFORM_WINDOWS)
39 static WSADATA g_wsaData;
40 typedef int socklen_t;
43 #include <sys/ioctl.h>
44 inline void closesocket(int s) { ::close(s); }
47 #include <jack/jack.h>
48 #include <jack/midiport.h>
50 #include "pbd/xml++.h"
51 #include "pbd/error.h"
52 #include "pbd/failed_constructor.h"
53 #include "pbd/convert.h"
54 #include "pbd/compose.h"
56 #include "midi++/types.h"
57 #include "midi++/ipmidi_port.h"
58 #include "midi++/channel.h"
64 IPMIDIPort::IPMIDIPort (int base_port, const string& iface)
65 : Port (string_compose ("IPmidi@%1", base_port), Port::Flags (Port::IsInput|Port::IsOutput))
69 if (!open_sockets (base_port, iface)) {
70 throw (failed_constructor ());
74 IPMIDIPort::IPMIDIPort (const XMLNode& node)
77 /* base class does not class set_state() */
81 IPMIDIPort::~IPMIDIPort ()
87 IPMIDIPort::selectable () const
93 IPMIDIPort::get_state () const
95 return Port::get_state ();
99 IPMIDIPort::set_state (const XMLNode& node)
101 Port::set_state (node);
105 IPMIDIPort::close_sockets ()
108 ::closesocket (sockin);
113 ::closesocket (sockout);
119 get_address (int sock, struct in_addr *inaddr, const string& ifname )
121 // Get interface address from supplied name.
123 #if !defined(PLATFORM_WINDOWS)
125 ::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
127 if (::ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
128 ::perror("ioctl(SIOCGIFFLAGS)");
132 if ((ifr.ifr_flags & IFF_UP) == 0) {
133 error << string_compose ("interface %1 is down", ifname) << endmsg;
137 if (::ioctl(sock, SIOCGIFADDR, (char *) &ifr)) {
138 ::perror("ioctl(SIOCGIFADDR)");
142 struct sockaddr_in sa;
143 ::memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
144 inaddr->s_addr = sa.sin_addr.s_addr;
152 #endif // !PLATFORM_WINDOWS'
156 IPMIDIPort::open_sockets (int base_port, const string& ifname)
158 #if !defined(PLATFORM_WINDOWS)
160 struct protoent *proto = ::getprotobyname("IP");
163 protonum = proto->p_proto;
166 sockin = ::socket (PF_INET, SOCK_DGRAM, protonum);
168 ::perror("socket(in)");
172 struct sockaddr_in addrin;
173 ::memset(&addrin, 0, sizeof(addrin));
174 addrin.sin_family = AF_INET;
175 addrin.sin_addr.s_addr = htonl(INADDR_ANY);
176 addrin.sin_port = htons(base_port);
178 if (::bind(sockin, (struct sockaddr *) (&addrin), sizeof(addrin)) < 0) {
184 // INADDR_ANY will bind to default interface,
185 // specify alternate interface nameon which to bind...
186 struct in_addr if_addr_in;
187 if (!ifname.empty()) {
188 if (!get_address(sockin, &if_addr_in, ifname)) {
189 error << string_compose ("socket(in): could not find interface address for %1", ifname) << endmsg;
192 if (::setsockopt(sockin, IPPROTO_IP, IP_MULTICAST_IF,
193 (char *) &if_addr_in, sizeof(if_addr_in))) {
194 ::perror("setsockopt(IP_MULTICAST_IF)");
198 if_addr_in.s_addr = htonl (INADDR_ANY);
202 mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37");
203 mreq.imr_interface.s_addr = if_addr_in.s_addr;
204 if(::setsockopt (sockin, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)) < 0) {
205 ::perror("setsockopt(IP_ADD_MEMBERSHIP)");
206 fprintf(stderr, "socket(in): your kernel is probably missing multicast support.\n");
212 sockout = ::socket (AF_INET, SOCK_DGRAM, protonum);
215 ::perror("socket(out)");
219 // Will Hall, Oct 2007
220 if (!ifname.empty()) {
221 struct in_addr if_addr_out;
222 if (!get_address(sockout, &if_addr_out, ifname)) {
223 error << string_compose ("socket(out): could not find interface address for %1", ifname) << endmsg;
226 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_IF, (char *) &if_addr_out, sizeof(if_addr_out))) {
227 ::perror("setsockopt(IP_MULTICAST_IF)");
232 ::memset(&addrout, 0, sizeof(struct sockaddr_in));
233 addrout.sin_family = AF_INET;
234 addrout.sin_addr.s_addr = ::inet_addr("225.0.0.37");
235 addrout.sin_port = htons (base_port);
237 // Turn off loopback...
239 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
240 ::perror("setsockopt(IP_MULTICAST_LOOP)");
244 if (fcntl (sockin, F_SETFL, O_NONBLOCK)) {
245 error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
249 if (fcntl (sockout, F_SETFL, O_NONBLOCK)) {
250 error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
257 #endif // !PLATFORM_WINDOWS'
261 IPMIDIPort::write (const MIDI::byte* msg, size_t msglen, timestamp_t /* ignored */) {
264 Glib::Threads::Mutex::Lock lm (write_lock);
265 if (::sendto (sockout, (const char *) msg, msglen, 0, (struct sockaddr *) &addrout, sizeof(struct sockaddr_in)) < 0) {
275 IPMIDIPort::read (MIDI::byte* /*buf*/, size_t /*bufsize*/)
277 /* nothing to do here - all handled by parse() */
282 IPMIDIPort::parse (framecnt_t timestamp)
284 /* input was detected on the socket, so go get it and hand it to the
285 * parser. This will emit appropriate signals that will be handled
286 * by anyone who cares.
289 unsigned char buf[1024];
290 struct sockaddr_in sender;
291 socklen_t slen = sizeof(sender);
292 int r = ::recvfrom (sockin, (char *) buf, sizeof(buf), 0, (struct sockaddr *) &sender, &slen);
296 _parser->set_timestamp (timestamp);
298 for (int i = 0; i < r; ++i) {
299 _parser->scanner (buf[i]);
302 ::perror ("failed to recv from socket");