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 $
31 static WSADATA g_wsaData;
32 typedef int socklen_t;
35 #include <sys/ioctl.h>
36 inline void closesocket(int s) { ::close(s); }
39 #include <jack/jack.h>
40 #include <jack/midiport.h>
42 #include "pbd/xml++.h"
43 #include "pbd/error.h"
44 #include "pbd/failed_constructor.h"
45 #include "pbd/convert.h"
46 #include "pbd/compose.h"
48 #include "midi++/types.h"
49 #include "midi++/ipmidi_port.h"
50 #include "midi++/channel.h"
56 IPMIDIPort::IPMIDIPort (int base_port, const string& iface)
57 : Port (string_compose ("IPmidi@%1", base_port), Port::Flags (Port::IsInput|Port::IsOutput))
61 if (!open_sockets (base_port, iface)) {
62 throw (failed_constructor ());
66 IPMIDIPort::IPMIDIPort (const XMLNode& node)
69 /* base class does not class set_state() */
73 IPMIDIPort::~IPMIDIPort ()
79 IPMIDIPort::selectable () const
85 IPMIDIPort::get_state () const
87 return Port::get_state ();
91 IPMIDIPort::set_state (const XMLNode& node)
93 Port::set_state (node);
97 IPMIDIPort::close_sockets ()
100 ::closesocket (sockin);
105 ::closesocket (sockout);
111 get_address (int sock, struct in_addr *inaddr, const string& ifname )
113 // Get interface address from supplied name.
117 ::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
119 if (::ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
120 ::perror("ioctl(SIOCGIFFLAGS)");
124 if ((ifr.ifr_flags & IFF_UP) == 0) {
125 error << string_compose ("interface %1 is down", ifname) << endmsg;
129 if (::ioctl(sock, SIOCGIFADDR, (char *) &ifr)) {
130 ::perror("ioctl(SIOCGIFADDR)");
134 struct sockaddr_in sa;
135 ::memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
136 inaddr->s_addr = sa.sin_addr.s_addr;
148 IPMIDIPort::open_sockets (int base_port, const string& ifname)
151 struct protoent *proto = ::getprotobyname("IP");
154 protonum = proto->p_proto;
157 sockin = ::socket (PF_INET, SOCK_DGRAM, protonum);
159 ::perror("socket(in)");
163 struct sockaddr_in addrin;
164 ::memset(&addrin, 0, sizeof(addrin));
165 addrin.sin_family = AF_INET;
166 addrin.sin_addr.s_addr = htonl(INADDR_ANY);
167 addrin.sin_port = htons(base_port);
169 if (::bind(sockin, (struct sockaddr *) (&addrin), sizeof(addrin)) < 0) {
175 // INADDR_ANY will bind to default interface,
176 // specify alternate interface nameon which to bind...
177 struct in_addr if_addr_in;
178 if (!ifname.empty()) {
179 if (!get_address(sockin, &if_addr_in, ifname)) {
180 error << string_compose ("socket(in): could not find interface address for %1", ifname) << endmsg;
183 if (::setsockopt(sockin, IPPROTO_IP, IP_MULTICAST_IF,
184 (char *) &if_addr_in, sizeof(if_addr_in))) {
185 ::perror("setsockopt(IP_MULTICAST_IF)");
189 if_addr_in.s_addr = htonl (INADDR_ANY);
193 mreq.imr_multiaddr.s_addr = ::inet_addr("225.0.0.37");
194 mreq.imr_interface.s_addr = if_addr_in.s_addr;
195 if(::setsockopt (sockin, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)) < 0) {
196 ::perror("setsockopt(IP_ADD_MEMBERSHIP)");
197 fprintf(stderr, "socket(in): your kernel is probably missing multicast support.\n");
203 sockout = ::socket (AF_INET, SOCK_DGRAM, protonum);
206 ::perror("socket(out)");
210 // Will Hall, Oct 2007
211 if (!ifname.empty()) {
212 struct in_addr if_addr_out;
213 if (!get_address(sockout, &if_addr_out, ifname)) {
214 error << string_compose ("socket(out): could not find interface address for %1", ifname) << endmsg;
217 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_IF, (char *) &if_addr_out, sizeof(if_addr_out))) {
218 ::perror("setsockopt(IP_MULTICAST_IF)");
223 ::memset(&addrout, 0, sizeof(struct sockaddr_in));
224 addrout.sin_family = AF_INET;
225 addrout.sin_addr.s_addr = ::inet_addr("225.0.0.37");
226 addrout.sin_port = htons (base_port);
228 // Turn off loopback...
230 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
231 ::perror("setsockopt(IP_MULTICAST_LOOP)");
235 if (fcntl (sockin, F_SETFL, O_NONBLOCK)) {
236 error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
240 if (fcntl (sockout, F_SETFL, O_NONBLOCK)) {
241 error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
249 IPMIDIPort::write (const byte* msg, size_t msglen, timestamp_t /* ignored */) {
252 Glib::Threads::Mutex::Lock lm (write_lock);
253 if (::sendto (sockout, (const char *) msg, msglen, 0, (struct sockaddr *) &addrout, sizeof(struct sockaddr_in)) < 0) {
263 IPMIDIPort::read (byte* /*buf*/, size_t /*bufsize*/)
265 /* nothing to do here - all handled by parse() */
270 IPMIDIPort::parse (framecnt_t timestamp)
272 /* input was detected on the socket, so go get it and hand it to the
273 * parser. This will emit appropriate signals that will be handled
274 * by anyone who cares.
277 unsigned char buf[1024];
278 struct sockaddr_in sender;
279 socklen_t slen = sizeof(sender);
280 int r = ::recvfrom (sockin, (char *) buf, sizeof(buf), 0, (struct sockaddr *) &sender, &slen);
284 _parser->set_timestamp (timestamp);
286 for (int i = 0; i < r; ++i) {
287 _parser->scanner (buf[i]);
290 ::perror ("failed to recv from socket");