@@ -167,9 +167,32 @@ static int _applemidi_update_runloop_source( struct MIDIDriverAppleMIDI * driver
167
167
168
168
169
169
static int _applemidi_connect ( struct MIDIDriverAppleMIDI * driver ) {
170
- struct sockaddr_in addr ;
171
170
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 ;
173
196
memset (& addr , 0 , sizeof (addr ));
174
197
if ( driver -> control_socket <= 0 ) {
175
198
addr .sin_family = AF_INET ;
@@ -190,7 +213,7 @@ static int _applemidi_connect( struct MIDIDriverAppleMIDI * driver ) {
190
213
if (driver -> control_socket != -1 )
191
214
result = bind ( driver -> rtp_socket , (struct sockaddr * ) & addr , sizeof (addr ) );
192
215
}
193
-
216
+ #endif
194
217
return result ;
195
218
}
196
219
@@ -271,6 +294,7 @@ static void _driver_destroy( struct MIDIDriver * driverp ) {
271
294
struct MIDIDriverAppleMIDI * MIDIDriverAppleMIDICreate ( char * name , unsigned short port ) {
272
295
struct MIDIDriverAppleMIDI * driver ;
273
296
MIDITimestamp timestamp ;
297
+ int ret = 0 ;
274
298
275
299
driver = malloc ( sizeof ( struct MIDIDriverAppleMIDI ) );
276
300
MIDIPrecondReturn ( driver != NULL , ENOMEM , NULL );
@@ -289,7 +313,9 @@ struct MIDIDriverAppleMIDI * MIDIDriverAppleMIDICreate( char * name, unsigned sh
289
313
driver -> base .send = & _driver_send ;
290
314
driver -> base .destroy = & _driver_destroy ;
291
315
292
- _applemidi_connect ( driver );
316
+ ret = _applemidi_connect ( driver );
317
+ if (ret == -1 )
318
+ MIDILog ( ERROR , "Bind failed for port: %hu\n" , port );
293
319
294
320
driver -> peer = NULL ;
295
321
driver -> rtp_session = RTPSessionCreate ( driver -> rtp_socket );
@@ -537,6 +563,14 @@ static int _applemidi_send_command( struct MIDIDriverAppleMIDI * driver, int fd,
537
563
if ( command -> addr .ss_family == AF_INET ) {
538
564
struct sockaddr_in * a = (struct sockaddr_in * ) & (command -> addr );
539
565
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
540
574
} else {
541
575
MIDILog ( DEBUG , "send %i bytes to <unknown addr family> on s(%i)\n" , len , fd );
542
576
}
@@ -569,6 +603,14 @@ static int _applemidi_recv_command( struct MIDIDriverAppleMIDI * driver, int fd,
569
603
if ( command -> addr .ss_family == AF_INET ) {
570
604
struct sockaddr_in * a = (struct sockaddr_in * ) & (command -> addr );
571
605
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
572
614
} else {
573
615
MIDILog ( DEBUG , "recv %i bytes from <unknown addr family> on s(%i)\n" , len , fd );
574
616
}
@@ -734,14 +776,22 @@ static int _applemidi_endsession( struct MIDIDriverAppleMIDI * driver, int fd, s
734
776
* @return >0 on error.
735
777
*/
736
778
static int _applemidi_rtp_addr ( socklen_t size , struct sockaddr * control_addr , struct sockaddr * rtp_addr ) {
737
- struct sockaddr_in * in_addr ;
738
779<
57AE
/code>
if ( control_addr != rtp_addr ) {
739
780
memcpy ( rtp_addr , control_addr , size );
740
781
}
741
782
if ( rtp_addr -> sa_family == AF_INET ) {
783
+ struct sockaddr_in * in_addr ;
742
784
in_addr = (struct sockaddr_in * ) rtp_addr ;
743
785
in_addr -> sin_port = htons ( ntohs ( in_addr -> sin_port ) + 1 );
744
786
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
745
795
} else {
746
796
return 1 ;
747
797
}
@@ -756,14 +806,22 @@ static int _applemidi_rtp_addr( socklen_t size, struct sockaddr * control_addr,
756
806
* @return >0 on error.
757
807
*/
758
808
static int _applemidi_control_addr ( socklen_t size , struct sockaddr * rtp_addr , struct sockaddr * control_addr ) {
759
- struct sockaddr_in * in_addr ;
760
809
if ( rtp_addr != control_addr ) {
761
810
memcpy ( control_addr , rtp_addr , size );
762
811
}
763
812
if ( control_addr -> sa_family == AF_INET ) {
813
+ struct sockaddr_in * in_addr ;
764
814
in_addr = (struct sockaddr_in * ) control_addr ;
765
815
in_addr -> sin_port = htons ( ntohs ( in_addr -> sin_port ) - 1 );
766
816
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
767
825
} else {
768
826
return 1 ;
769
827
}
0 commit comments