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 "pbd/xml++.h"
48 #include "pbd/error.h"
49 #include "pbd/failed_constructor.h"
50 #include "pbd/convert.h"
51 #include "pbd/compose.h"
53 #include "midi++/types.h"
54 #include "midi++/ipmidi_port.h"
55 #include "midi++/channel.h"
61 IPMIDIPort::IPMIDIPort (int base_port, const string& iface)
62 : Port (string_compose ("IPmidi@%1", base_port), Port::Flags (Port::IsInput|Port::IsOutput))
66 if (!open_sockets (base_port, iface)) {
67 throw (failed_constructor ());
71 IPMIDIPort::IPMIDIPort (const XMLNode& node)
74 /* base class does not class set_state() */
78 IPMIDIPort::~IPMIDIPort ()
84 IPMIDIPort::selectable () const
90 IPMIDIPort::get_state () const
92 return Port::get_state ();
96 IPMIDIPort::set_state (const XMLNode& node)
98 Port::set_state (node);
102 IPMIDIPort::close_sockets ()
105 ::closesocket (sockin);
110 ::closesocket (sockout);
116 get_address (int sock, struct in_addr *inaddr, const string& ifname )
118 // Get interface address from supplied name.
120 #if !defined(PLATFORM_WINDOWS)
122 ::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
124 if (::ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
125 ::perror("ioctl(SIOCGIFFLAGS)");
129 if ((ifr.ifr_flags & IFF_UP) == 0) {
130 error << string_compose ("interface %1 is down", ifname) << endmsg;
134 if (::ioctl(sock, SIOCGIFADDR, (char *) &ifr)) {
135 ::perror("ioctl(SIOCGIFADDR)");
139 struct sockaddr_in sa;
140 ::memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
141 inaddr->s_addr = sa.sin_addr.s_addr;
149 #endif // !PLATFORM_WINDOWS'
153 IPMIDIPort::open_sockets (int base_port, const string& ifname)
155 #if !defined(PLATFORM_WINDOWS)
157 struct protoent *proto = ::getprotobyname("IP");
160 protonum = proto->p_proto;
163 sockin = ::socket (PF_INET, SOCK_DGRAM, protonum);
165 ::perror("socket(in)");
169 struct sockaddr_in addrin;
170 ::memset(&addrin, 0, sizeof(addrin));
171 addrin.sin_family = AF_INET;
172 addrin.sin_addr.s_addr = htonl(INADDR_ANY);
173 addrin.sin_port = htons(base_port);
175 if (::bind(sockin, (struct sockaddr *) (&addrin), sizeof(addrin)) < 0) {
181 // INADDR_ANY will bind to default interface,
182 // specify alternate interface nameon which to bind...
183 struct in_addr if_addr_in;
184 if (!ifname.empty()) {
185 if (!get_address(sockin, &if_addr_in, ifname)) {
186 error << string_compose ("socket(in): could not find interface address for %1", ifname) << endmsg;
189 if (::setsockopt(sockin, IPPROTO_IP, IP_MULTICAST_IF,
190 (char *) &if_addr_in, sizeof(if_addr_in))) {
191 ::perror("setsockopt(IP_MULTICAST_IF)");
195 if_addr_in.s_addr = htonl (INADDR_ANY);
199 mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37");
200 mreq.imr_interface.s_addr = if_addr_in.s_addr;
201 if(::setsockopt (sockin, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)) < 0) {
202 ::perror("setsockopt(IP_ADD_MEMBERSHIP)");
203 fprintf(stderr, "socket(in): your kernel is probably missing multicast support.\n");
209 sockout = ::socket (AF_INET, SOCK_DGRAM, protonum);
212 ::perror("socket(out)");
216 // Will Hall, Oct 2007
217 if (!ifname.empty()) {
218 struct in_addr if_addr_out;
219 if (!get_address(sockout, &if_addr_out, ifname)) {
220 error << string_compose ("socket(out): could not find interface address for %1", ifname) << endmsg;
223 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_IF, (char *) &if_addr_out, sizeof(if_addr_out))) {
224 ::perror("setsockopt(IP_MULTICAST_IF)");
229 ::memset(&addrout, 0, sizeof(struct sockaddr_in));
230 addrout.sin_family = AF_INET;
231 addrout.sin_addr.s_addr = ::inet_addr("225.0.0.37");
232 addrout.sin_port = htons (base_port);
234 // Turn off loopback...
236 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
237 ::perror("setsockopt(IP_MULTICAST_LOOP)");
241 if (fcntl (sockin, F_SETFL, O_NONBLOCK)) {
242 error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
246 if (fcntl (sockout, F_SETFL, O_NONBLOCK)) {
247 error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
254 #endif // !PLATFORM_WINDOWS'
258 IPMIDIPort::write (const MIDI::byte* msg, size_t msglen, timestamp_t /* ignored */) {
261 Glib::Threads::Mutex::Lock lm (write_lock);
262 if (::sendto (sockout, (const char *) msg, msglen, 0, (struct sockaddr *) &addrout, sizeof(struct sockaddr_in)) < 0) {
272 IPMIDIPort::read (MIDI::byte* /*buf*/, size_t /*bufsize*/)
274 /* nothing to do here - all handled by parse() */
279 IPMIDIPort::parse (framecnt_t timestamp)
281 /* input was detected on the socket, so go get it and hand it to the
282 * parser. This will emit appropriate signals that will be handled
283 * by anyone who cares.
286 unsigned char buf[1024];
287 struct sockaddr_in sender;
288 socklen_t slen = sizeof(sender);
289 int r = ::recvfrom (sockin, (char *) buf, sizeof(buf), 0, (struct sockaddr *) &sender, &slen);
293 _parser->set_timestamp (timestamp);
295 for (int i = 0; i < r; ++i) {
296 _parser->scanner (buf[i]);
299 ::perror ("failed to recv from socket");