Support midnam files with a channel (not patch) NoteNameList.
[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 #include <netdb.h>
29
30 #if defined(WIN32)
31 static WSADATA g_wsaData;
32 typedef int socklen_t;
33 #else
34 #include <unistd.h>
35 #include <sys/ioctl.h>
36 inline void closesocket(int s) { ::close(s); }
37 #endif
38
39 #include "pbd/xml++.h"
40 #include "pbd/error.h"
41 #include "pbd/failed_constructor.h"
42 #include "pbd/convert.h"
43 #include "pbd/compose.h"
44
45 #include "midi++/types.h"
46 #include "midi++/ipmidi_port.h"
47 #include "midi++/channel.h"
48
49 using namespace MIDI;
50 using namespace std;
51 using namespace PBD;
52
53 IPMIDIPort::IPMIDIPort (int base_port, const string& iface)
54         : Port (string_compose ("IPmidi@%1", base_port), Port::Flags (Port::IsInput|Port::IsOutput))
55         , sockin (-1)
56         , sockout (-1)
57 {
58         if (!open_sockets (base_port, iface)) {
59                 throw (failed_constructor ());
60         }
61 }
62
63 IPMIDIPort::IPMIDIPort (const XMLNode& node)
64         : Port (node)
65 {
66         /* base class does not class set_state() */
67         set_state (node);
68 }
69
70 IPMIDIPort::~IPMIDIPort ()
71 {
72         close_sockets ();
73 }
74
75 int
76 IPMIDIPort::selectable () const
77
78         return sockin; 
79 }
80
81 XMLNode&
82 IPMIDIPort::get_state () const
83 {
84         return Port::get_state ();
85 }
86
87 void
88 IPMIDIPort::set_state (const XMLNode& node)
89 {
90         Port::set_state (node);
91 }
92
93 void
94 IPMIDIPort::close_sockets ()
95 {
96         if (sockin >= 0) {
97                 ::closesocket (sockin);
98                 sockin = -1;
99         }
100         
101         if (sockout >= 0) {
102                 ::closesocket (sockout);
103                 sockout = -1;
104         }
105 }
106
107 static bool 
108 get_address (int sock, struct in_addr *inaddr, const string& ifname )
109 {
110         // Get interface address from supplied name.
111
112 #if !defined(WIN32)
113         struct ifreq ifr;
114         ::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
115
116         if (::ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
117                 ::perror("ioctl(SIOCGIFFLAGS)");
118                 return false;
119         }
120
121         if ((ifr.ifr_flags & IFF_UP) == 0) {
122                 error << string_compose ("interface %1 is down", ifname) << endmsg;
123                 return false;
124         }
125
126         if (::ioctl(sock, SIOCGIFADDR, (char *) &ifr)) {
127                 ::perror("ioctl(SIOCGIFADDR)");
128                 return false;
129         }
130
131         struct sockaddr_in sa;
132         ::memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
133         inaddr->s_addr = sa.sin_addr.s_addr;
134
135         return true;
136
137 #else
138
139         return false;
140
141 #endif  // !WIN32
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 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 (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