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 "pbd/xml++.h"
40 #include "pbd/error.h"
41 #include "pbd/failed_constructor.h"
42 #include "pbd/convert.h"
43 #include "pbd/compose.h"
45 #include "midi++/types.h"
46 #include "midi++/ipmidi_port.h"
47 #include "midi++/channel.h"
53 IPMIDIPort::IPMIDIPort (int base_port, const string& iface)
54 : Port (string_compose ("IPmidi@%1", base_port), Port::Flags (Port::IsInput|Port::IsOutput))
58 if (!open_sockets (base_port, iface)) {
59 throw (failed_constructor ());
63 IPMIDIPort::IPMIDIPort (const XMLNode& node)
66 /* base class does not class set_state() */
70 IPMIDIPort::~IPMIDIPort ()
76 IPMIDIPort::selectable () const
82 IPMIDIPort::get_state () const
84 return Port::get_state ();
88 IPMIDIPort::set_state (const XMLNode& node)
90 Port::set_state (node);
94 IPMIDIPort::close_sockets ()
97 ::closesocket (sockin);
102 ::closesocket (sockout);
108 get_address (int sock, struct in_addr *inaddr, const string& ifname )
110 // Get interface address from supplied name.
114 ::strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
116 if (::ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
117 ::perror("ioctl(SIOCGIFFLAGS)");
121 if ((ifr.ifr_flags & IFF_UP) == 0) {
122 error << string_compose ("interface %1 is down", ifname) << endmsg;
126 if (::ioctl(sock, SIOCGIFADDR, (char *) &ifr)) {
127 ::perror("ioctl(SIOCGIFADDR)");
131 struct sockaddr_in sa;
132 ::memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
133 inaddr->s_addr = sa.sin_addr.s_addr;
145 IPMIDIPort::open_sockets (int base_port, const string& ifname)
148 struct protoent *proto = ::getprotobyname("IP");
151 protonum = proto->p_proto;
154 sockin = ::socket (PF_INET, SOCK_DGRAM, protonum);
156 ::perror("socket(in)");
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);
166 if (::bind(sockin, (struct sockaddr *) (&addrin), sizeof(addrin)) < 0) {
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;
180 if (::setsockopt(sockin, IPPROTO_IP, IP_MULTICAST_IF,
181 (char *) &if_addr_in, sizeof(if_addr_in))) {
182 ::perror("setsockopt(IP_MULTICAST_IF)");
186 if_addr_in.s_addr = htonl (INADDR_ANY);
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");
200 sockout = ::socket (AF_INET, SOCK_DGRAM, protonum);
203 ::perror("socket(out)");
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;
214 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_IF, (char *) &if_addr_out, sizeof(if_addr_out))) {
215 ::perror("setsockopt(IP_MULTICAST_IF)");
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);
225 // Turn off loopback...
227 if (::setsockopt(sockout, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &loop, sizeof (loop)) < 0) {
228 ::perror("setsockopt(IP_MULTICAST_LOOP)");
232 if (fcntl (sockin, F_SETFL, O_NONBLOCK)) {
233 error << "cannot set non-blocking mode for IP MIDI input socket (" << ::strerror (errno) << ')' << endmsg;
237 if (fcntl (sockout, F_SETFL, O_NONBLOCK)) {
238 error << "cannot set non-blocking mode for IP MIDI output socket (" << ::strerror (errno) << ')' << endmsg;
246 IPMIDIPort::write (const byte* msg, size_t msglen, timestamp_t /* ignored */) {
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) {
260 IPMIDIPort::read (byte* /*buf*/, size_t /*bufsize*/)
262 /* nothing to do here - all handled by parse() */
267 IPMIDIPort::parse (framecnt_t timestamp)
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.
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);
281 _parser->set_timestamp (timestamp);
283 for (int i = 0; i < r; ++i) {
284 _parser->scanner (buf[i]);
287 ::perror ("failed to recv from socket");