try just removing all PLATFORM_WINDOWS conditionals in ipmidi code to see if it will...
[ardour.git] / libs / midi++2 / ipmidi_port.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3
4     Using code from Rui Nuno Capela's qmidinet as inspiration.
5
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.
10
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.
15
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.
19
20     $Id: port.cc 12065 2012-04-23 16:23:48Z paul $
21 */
22 #include <iostream>
23 #include <cstdio>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #ifdef   COMPILER_MSVC
29 #undef   O_NONBLOCK
30 #define  O_NONBLOCK 0
31 #endif
32 #if defined(PLATFORM_WINDOWS)
33 #include <winsock2.h>
34 #else
35 #include <netdb.h>
36 #endif
37
38 #if defined(PLATFORM_WINDOWS)
39 typedef int socklen_t;
40 #else
41 #include <unistd.h>
42 #include <sys/ioctl.h>
43 inline void closesocket(int s) { ::close(s); }
44 #endif
45
46 #include "pbd/xml++.h"
47 #include "pbd/error.h"
48 #include "pbd/failed_constructor.h"
49 #include "pbd/convert.h"
50 #include "pbd/compose.h"
51
52 #include "midi++/types.h"
53 #include "midi++/ipmidi_port.h"
54 #include "midi++/channel.h"
55
56 using namespace MIDI;
57 using namespace std;
58 using namespace PBD;
59
60 IPMIDIPort::IPMIDIPort (int base_port, const string& iface)
61         : Port (string_compose ("IPmidi@%1", base_port), Port::Flags (Port::IsInput|Port::IsOutput))
62         , sockin (-1)
63         , sockout (-1)
64 {
65         if (!open_sockets (base_port, iface)) {
66                 throw (failed_constructor ());
67         }
68 }
69
70 IPMIDIPort::IPMIDIPort (const XMLNode& node)
71         : Port (node)
72 {
73         /* base class does not class set_state() */
74         set_state (node);
75 }
76
77 IPMIDIPort::~IPMIDIPort ()
78 {
79         close_sockets ();
80 }
81
82 int
83 IPMIDIPort::selectable () const
84 {
85         return sockin;
86 }
87
88 XMLNode&
89 IPMIDIPort::get_state () const
90 {
91         return Port::get_state ();
92 }
93
94 void
95 IPMIDIPort::set_state (const XMLNode& node)
96 {
97         Port::set_state (node);
98 }
99
100 void
101 IPMIDIPort::close_sockets ()
102 {
103         if (sockin >= 0) {
104                 ::closesocket (sockin);
105                 sockin = -1;
106         }
107
108         if (sockout >= 0) {
109                 ::closesocket (sockout);
110                 sockout = -1;
111         }
112 }
113
114 static bool
115 get_address (int sock, struct in_addr *inaddr, const string& ifname )
116 {
117         // Get interface address from supplied name.
118
119         struct ifreq ifr;
120         ::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
121
122         if (::ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
123                 ::perror("ioctl(SIOCGIFFLAGS)");
124                 return false;
125         }
126
127         if ((ifr.ifr_flags & IFF_UP) == 0) {
128                 error << string_compose ("interface %1 is down", ifname) << endmsg;
129                 return false;
130         }
131
132         if (::ioctl(sock, SIOCGIFADDR, (char *) &ifr)) {
133                 ::perror("ioctl(SIOCGIFADDR)");
134                 return false;
135         }
136
137         struct sockaddr_in sa;
138         ::memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
139         inaddr->s_addr = sa.sin_addr.s_addr;
140
141         return true;
142 }
143
144 bool
145 IPMIDIPort::open_sockets (int base_port, const string& ifname)
146 {
147         int protonum = 0;
148         struct protoent *proto = ::getprotobyname("IP");
149
150         if (proto) {
151                 protonum = proto->p_proto;
152         }
153
154         sockin = ::socket (PF_INET, SOCK_DGRAM, protonum);
155         if (sockin < 0) {
156                 ::perror("socket(in)");
157                 return false;
158         }
159
160         struct sockaddr_in addrin;
161         ::memset(&addrin, 0, sizeof(addrin));
162         addrin.sin_family = AF_INET;
163         addrin.sin_addr.s_addr = htonl(INADDR_ANY);
164         addrin.sin_port = htons(base_port);
165
166         if (::bind(sockin, (struct sockaddr *) (&addrin), sizeof(addrin)) < 0) {
167                 ::perror("bind");
168                 return false;
169         }
170
171         // Will Hall, 2007
172         // INADDR_ANY will bind to default interface,
173         // specify alternate interface nameon which to bind...
174         struct in_addr if_addr_in;
175         if (!ifname.empty()) {
176                 if (!get_address(sockin, &if_addr_in, ifname)) {
177                         error << string_compose ("socket(in): could not find interface address for %1", ifname) << endmsg;
178                         return false;
179                 }
180                 if (::setsockopt(sockin, IPPROTO_IP, IP_MULTICAST_IF,
181                                  (char *) &if_addr_in, sizeof(if_addr_in))) {
182                         ::perror("setsockopt(IP_MULTICAST_IF)");
183                         return false;
184                 }
185         } else {
186                 if_addr_in.s_addr = htonl (INADDR_ANY);
187         }
188
189         struct ip_mreq mreq;
190         mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37");
191         mreq.imr_interface.s_addr = if_addr_in.s_addr;
192         if(::setsockopt (sockin, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)) < 0) {
193                 ::perror("setsockopt(IP_ADD_MEMBERSHIP)");
194                 fprintf(stderr, "socket(in): your kernel is probably missing multicast support.\n");
195                 return false;
196         }
197
198         // Output socket...
199
200         sockout = ::socket (AF_INET, SOCK_DGRAM, protonum);
201
202         if (sockout < 0) {
203                 ::perror("socket(out)");
204                 return false;
205         }
206
207         // Will Hall, Oct 2007
208         if (!ifname.empty()) {
209                 struct in_addr if_addr_out;
210                 if (!get_address(sockout, &if_addr_out, ifname)) {
211                         error << string_compose ("socket(out): could not find interface address for %1", ifname) << endmsg;
212                         return false;
213                 }
214                 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_IF, (char *) &if_addr_out, sizeof(if_addr_out))) {
215                         ::perror("setsockopt(IP_MULTICAST_IF)");
216                         return false;
217                 }
218         }
219
220         ::memset(&addrout, 0, sizeof(struct sockaddr_in));
221         addrout.sin_family = AF_INET;
222         addrout.sin_addr.s_addr = ::inet_addr("225.0.0.37");
223         addrout.sin_port = htons (base_port);
224
225         // Turn off loopback...
226         int loop = 0;
227         if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
228                 ::perror("setsockopt(IP_MULTICAST_LOOP)");
229                 return false;
230         }
231
232         if (fcntl (sockin, F_SETFL, O_NONBLOCK)) {
233                 error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
234                 return false;
235         }
236
237         if (fcntl (sockout, F_SETFL, O_NONBLOCK)) {
238                 error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
239                 return false;
240         }
241
242         return true;
243 }
244
245 int
246 IPMIDIPort::write (const MIDI::byte* msg, size_t msglen, timestamp_t /* ignored */) {
247
248         if (sockout) {
249                 Glib::Threads::Mutex::Lock lm (write_lock);
250                 if (::sendto (sockout, (const char *) msg, msglen, 0, (struct sockaddr *) &addrout, sizeof(struct sockaddr_in)) < 0) {
251                         ::perror("sendto");
252                         return -1;
253                 }
254                 return msglen;
255         }
256         return 0;
257 }
258
259 int
260 IPMIDIPort::read (MIDI::byte* /*buf*/, size_t /*bufsize*/)
261 {
262         /* nothing to do here - all handled by parse() */
263         return 0;
264 }
265
266 void
267 IPMIDIPort::parse (framecnt_t timestamp)
268 {
269         /* input was detected on the socket, so go get it and hand it to the
270          * parser. This will emit appropriate signals that will be handled
271          * by anyone who cares.
272          */
273
274         unsigned char buf[1024];
275         struct sockaddr_in sender;
276         socklen_t slen = sizeof(sender);
277         int r = ::recvfrom (sockin, (char *) buf, sizeof(buf), 0, (struct sockaddr *) &sender, &slen);
278
279         if (r >= 0) {
280
281                 _parser->set_timestamp (timestamp);
282
283                 for (int i = 0; i < r; ++i) {
284                         _parser->scanner (buf[i]);
285                 }
286         } else {
287                 ::perror ("failed to recv from socket");
288         }
289 }
290