10000 Update applemidi.c · g-coder/midikit@a5015f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5015f3

Browse files
committed
Update applemidi.c
Added support for IPv6 connections.
1 parent c44d317 commit a5015f3

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

driver/applemidi/applemidi.c

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,32 @@ static int _applemidi_update_runloop_source( struct MIDIDriverAppleMIDI * driver
167167

168168

169169
static int _applemidi_connect( struct MIDIDriverAppleMIDI * driver ) {
170-
struct sockaddr_in addr;
171170
int result = 0;
172-
171+
#if (defined(AF_INET6) && defined(ENABLE_IPV6))
172+
struct sockaddr_in6 addr;
173+
memset(&addr, 0, sizeof(addr));
174+
175+
if( driver->control_socket <= 0 ) {
176+
addr.sin6_family = AF_INET6;
177+
addr.sin6_addr = in6addr_any;
178+
addr.sin6_port = htons( driver->port );
179+
180+
driver->control_socket = socket( PF_INET6, SOCK_DGRAM, 0 );
181+
if (driver->control_socket != -1)
182+
result = bind( driver->control_socket, (struct sockaddr *) &addr, sizeof(addr) );
183+
}
184+
185+
if( driver->rtp_socket <= 0 ) {
186+
addr.sin6_family = AF_INET6;
187+
addr.sin6_addr = in6addr_any;
188+
addr.sin6_port = htons( driver->port + 1 );
189+
190+
driver->rtp_socket = socket( PF_INET6, SOCK_DGRAM, 0 );
191+
if (driver->rtp_socket != -1)
192+
result = bind( driver->rtp_socket, (struct sockaddr *) &addr, sizeof(addr) );
193+
}
194+
#else
195+
struct sockaddr_in addr;
173196
memset(&addr, 0, sizeof(addr));
174197
if( driver->control_socket <= 0 ) {
175198
addr.sin_family = AF_INET;
@@ -190,7 +213,7 @@ static int _applemidi_connect( struct MIDIDriverAppleMIDI * driver ) {
190213
if (driver->control_socket != -1)
191214
result = bind( driver->rtp_socket, (struct sockaddr *) &addr, sizeof(addr) );
192215
}
193-
216+
#endif
194217
return result;
195218
}
196219

@@ -271,6 +294,7 @@ static void _driver_destroy( struct MIDIDriver * driverp ) {
271294
struct MIDIDriverAppleMIDI * MIDIDriverAppleMIDICreate( char * name, unsigned short port ) {
272295
struct MIDIDriverAppleMIDI * driver;
273296
MIDITimestamp timestamp;
297+
int ret = 0;
274298

275299
driver = malloc( sizeof( struct MIDIDriverAppleMIDI ) );
276300
MIDIPrecondReturn( driver != NULL, ENOMEM, NULL );
@@ -289,7 +313,9 @@ struct MIDIDriverAppleMIDI * MIDIDriverAppleMIDICreate( char * name, unsigned sh
289313
driver->base.send = &_driver_send;
290314
driver->base.destroy = &_driver_destroy;
291315

292-
_applemidi_connect( driver );
316+
ret = _applemidi_connect( driver );
317+
if (ret==-1)
318+
MIDILog( ERROR, "Bind failed for port: %hu\n", port);
293319

294320
driver->peer = NULL;
295321
driver->rtp_session = RTPSessionCreate( driver->rtp_socket );
@@ -537,6 +563,14 @@ static int _applemidi_send_command( struct MIDIDriverAppleMIDI * driver, int fd,
537563
if( command->addr.ss_family == AF_INET ) {
538564
struct sockaddr_in * a = (struct sockaddr_in *) &(command->addr);
539565
MIDILog( DEBUG, "send %i bytes to %s:%i on s(%i)\n", len, inet_ntoa( a->sin_addr ), ntohs( a->sin_port ), fd );
566+
#if (defined(AF_INET6))
567+
} else if (command->addr.ss_family == AF_INET6 ) {
568+
char straddr[INET6_ADDRSTRLEN];
569+
struct sockaddr_in6 * a = (struct sockaddr_in6 *) &(command->addr);
570+
MIDILog( DEBUG, "send %i bytes to %s:%i on s(%i)\n", len,
571+
inet_ntop(AF_INET6, &a->sin6_addr, straddr, sizeof(straddr)), ntohs( a->sin6_port ), fd );
572+
}
573+
#endif
540574
} else {
541575
MIDILog( DEBUG, "send %i bytes to <unknown addr family> on s(%i)\n", len, fd );
542576
}
@@ -569,6 +603,14 @@ static int _applemidi_recv_command( struct MIDIDriverAppleMIDI * driver, int fd,
569603
if( command->addr.ss_family == AF_INET ) {
570604
struct sockaddr_in * a = (struct sockaddr_in *) &(command->addr);
571605
MIDILog( DEBUG, "recv %i bytes from %s:%i on s(%i)\n", len, inet_ntoa( a->sin_addr ), ntohs( a->sin_port ), fd );
606+
#if (defined(AF_INET6))
607+
} else if (command->addr.ss_family == AF_INET6 ) {
608+
char straddr[INET6_ADDRSTRLEN];
609+
struct sockaddr_in6 * a = (struct sockaddr_in6 *) &(command->addr);
610+
MIDILog( DEBUG, "recv %i bytes from %s:%i on s(%i)\n", len,
611+
inet_ntop(AF_INET6, &a->sin6_addr, straddr, sizeof(straddr)), ntohs( a->sin6_port ), fd );
612+
}
613+
#endif
572614
} else {
573615
MIDILog( DEBUG, "recv %i bytes from <unknown addr family> on s(%i)\n", len, fd );
574616
}
@@ -734,14 +776,22 @@ static int _applemidi_endsession( struct MIDIDriverAppleMIDI * driver, int fd, s
734776
* @return >0 on error.
735777
*/
736778
static int _applemidi_rtp_addr( socklen_t size, struct sockaddr * control_addr, struct sockaddr * rtp_addr ) {
737-
struct sockaddr_in * in_addr;
738779< 57AE /code>
if( control_addr != rtp_addr ) {
739780
memcpy( rtp_addr, control_addr, size );
740781
}
741782
if( rtp_addr->sa_family == AF_INET ) {
783+
struct sockaddr_in * in_addr;
742784
in_addr = (struct sockaddr_in *) rtp_addr;
743785
in_addr->sin_port = htons( ntohs( in_addr->sin_port ) + 1 );
744786
return 0;
787+
#if (defined(AF_INET6))
788+
} else if ( rtp_addr->sa_family == AF_INET6 ) {
789+
struct sockaddr_in6 * in_addr;
790+
in_addr = (struct sockaddr_in6 *) rtp_addr;
791+
in_addr->sin6_port = htons( ntohs( in_addr->sin6_port ) + 1 );
792+
return 0;
793+
}
794+
#endif
745795
} else {
746796
return 1;
747797
}
@@ -756,14 +806,22 @@ static int _applemidi_rtp_addr( socklen_t size, struct sockaddr * control_addr,
756806
* @return >0 on error.
757807
*/
758808
static int _applemidi_control_addr( socklen_t size, struct sockaddr * rtp_addr, struct sockaddr * control_addr ) {
759-
struct sockaddr_in * in_addr;
760809
if( rtp_addr != control_addr ) {
761810
memcpy( control_addr, rtp_addr, size );
762811
}
763812
if( control_addr->sa_family == AF_INET ) {
813+
struct sockaddr_in * in_addr;
764814
in_addr = (struct sockaddr_in *) control_addr;
765815
in_addr->sin_port = htons( ntohs( in_addr->sin_port ) - 1 );
766816
return 0;
817+
#if (defined(AF_INET6))
818+
} else if ( control_addr->sa_family == AF_INET6 ) {
819+
struct sockaddr_in6 * in_addr;
820+
in_addr = (struct sockaddr_in6 *) control_addr;
821+
in_addr->sin6_port = htons( ntohs( in_addr->sin6_port ) - 1 );
822+
return 0;
823+
}
824+
#endif
767825
} else {
768826
return 1;
769827
}

0 commit comments

Comments
 (0)
0