8000 Merge branch 'ipv6' · g-coder/midikit@2c54c9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c54c9f

Browse files
committed
Merge branch 'ipv6'
* ipv6: Less ifs Allow setting USE_IPV6 without touching config.mk De-duplicate binding De-duplicate socket creation & binding Fix missing dependencies Fix some typos around the #ifdefs Create driver_rtpv6.c Create driver_applemidiv6.c Update Makefile Update osc.c Update applemidi.c Update config.mk
2 parents 74e5fa7 + 539b772 commit 2c54c9f

File tree

7 files changed

+664
-42
lines changed

7 files changed

+664
-42
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test: test/.make
2727
test-clean: test/.make-clean
2828

2929
driver/.make: midi
30-
test/.make: midi
30+
test/.make: midi driver
3131

3232
%/.make:
3333
cd $$(dirname $@) && $(MAKE)

config.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
MKDIR_P = mkdir -p
32
LN_S = ln -s
43

@@ -9,11 +8,16 @@ BUILDDIR := $(PROJECTDIR)/build
98
OBJDIR := $(BUILDDIR)/$(SUBDIR)
109
LIBDIR := $(BUILDDIR)
1110
BINDIR := $(BUILDDIR)
11+
USE_IPV6 ?= 1
1212

1313
AR = ar
1414
ARFLAGS = c
1515
CC = gcc
16-
CFLAGS = -O3 -I$(PROJECTDIR) -DSUBDIR=\"$(SUBDIR)\" #-DNO_LOG -DNO_ASSERT -DNO_PRECOND -DNO_ERROR
16+
CFLAGS = -O3 -I$(PROJECTDIR) -DSUBDIR=\"$(SUBDIR)\" #-DNO_ASSERT -DNO_LOG -DNO_PRECOND -DNO_ERROR
17+
ifeq ($(USE_IPV6),1)
18+
CFLAGS += -DENABLE_IPV6
19+
endif
20+
1721
CFLAGS_OBJ_SHARED = $(CFLAGS) -c -fPIC
1822
CFLAGS_OBJ_STATIC = $(CFLAGS) -c
1923
CFLAGS_OBJ = $(CFLAGS_OBJ_$(COMPILE_MODE))

driver/applemidi/applemidi.c

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,29 +166,42 @@ static int _applemidi_update_runloop_source( struct MIDIDriverAppleMIDI * driver
166166
}
167167

168168

169-
static int _applemidi_connect( struct MIDIDriverAppleMIDI * driver ) {
169+
static int _applemidi_bind( int fd, int port ) {
170+
#if (defined(AF_INET6) && defined(ENABLE_IPV6))
171+
struct sockaddr_in6 addr;
172+
memset(&addr, 0, sizeof(addr));
173+
addr.sin6_family = AF_INET6;
174+
addr.sin6_addr = in6addr_any;
175+
addr.sin6_port = htons( port );
176+
#else
170177
struct sockaddr_in addr;
171-
int result = 0;
172-
173178
memset(&addr, 0, sizeof(addr));
174-
if( driver->control_socket <= 0 ) {
175-
addr.sin_family = AF_INET;
176-
addr.sin_addr.s_addr = INADDR_ANY;
177-
addr.sin_port = htons( driver->port );
179+
addr.sin_family = AF_INET;
180+
addr.sin_addr.s_addr = INADDR_ANY;
181+
addr.sin_port = htons( port );
182+
#endif
183+
return bind( fd, (struct sockaddr *) &addr, sizeof(addr) );
184+
}
178185

179-
driver->control_socket = socket( PF_INET, SOCK_DGRAM, 0 );
180-
if (driver->control_socket != -1)
181< A93C /td>-
result = bind( driver->control_socket, (struct sockaddr *) &addr, sizeof(addr) );
182-
}
183186

184-
if( driver->rtp_socket <= 0 ) {
185-
addr.sin_family = AF_INET;
186-
addr.sin_addr.s_addr = INADDR_ANY;
187-
addr.sin_port = htons( driver->port + 1 );
187+
static int _applemidi_connect( struct MIDIDriverAppleMIDI * driver ) {
188+
int result = 0;
189+
#if (defined(AF_INET6) && defined(ENABLE_IPV6))
190+
int pf = PF_INET6;
191+
#else
192+
int pf = PF_INET;
193+
#endif
188194

189-
driver->rtp_socket = socket( PF_INET, SOCK_DGRAM, 0 );
190-
if (driver->control_socket != -1)
191-
result = bind( driver->rtp_socket, (struct sockaddr *) &addr, sizeof(addr) );
195+
if( driver->control_socket <= 0 ) {
196+
driver->control_socket = socket( pf, SOCK_DGRAM, 0 );
197+
if( driver->control_socket != -1 )
198+
result = _applemidi_bind( driver->control_socket, driver->port );
199+
}
200+
201+
if( result == 0 && driver->rtp_socket <= 0 ) {
202+
driver->rtp_socket = socket( pf, SOCK_DGRAM, 0 );
203+
if( driver->rtp_socket != -1 )
204+
result = _applemidi_bind( driver->rtp_socket, driver->port + 1 );
192205
}
193206

194207
return result;
@@ -271,6 +284,7 @@ static void _driver_destroy( struct MIDIDriver * driverp ) {
271284
struct MIDIDriverAppleMIDI * MIDIDriverAppleMIDICreate( char * name, unsigned short port ) {
272285
struct MIDIDriverAppleMIDI * driver;
273286
MIDITimestamp timestamp;
287+
int ret = 0;
274288

275289
driver = malloc( sizeof( struct MIDIDriverAppleMIDI ) );
276290
MIDIPrecondReturn( driver != NULL, ENOMEM, NULL );
@@ -289,7 +303,9 @@ struct MIDIDriverAppleMIDI * MIDIDriverAppleMIDICreate( char * name, unsigned sh
289303
driver->base.send = &_driver_send;
290304
driver->base.destroy = &_driver_destroy;
291305

292-
_applemidi_connect( driver );
306+
ret = _applemidi_connect( driver );
307+
if (ret==-1)
308+
MIDILog( ERROR, "Bind failed for port: %hu\n", port);
293309

294310
driver->peer = NULL;
295311
driver->rtp_session = RTPSessionCreate( driver->rtp_socket );
@@ -537,6 +553,13 @@ static int _applemidi_send_command( struct MIDIDriverAppleMIDI * driver, int fd,
537553
if( command->addr.ss_family == AF_INET ) {
538554
struct sockaddr_in * a = (struct sockaddr_in *) &(command->addr);
539555
MIDILog( DEBUG, "send %i bytes to %s:%i on s(%i)\n", len, inet_ntoa( a->sin_addr ), ntohs( a->sin_port ), fd );
556+
#if (defined(AF_INET6))
557+
} else if (command->addr.ss_family == AF_INET6 ) {
558+
char straddr[INET6_ADDRSTRLEN];
559+
struct sockaddr_in6 * a = (struct sockaddr_in6 *) &(command->addr);
560+
MIDILog( DEBUG, "send %i bytes to %s:%i on s(%i)\n", len,
561+
inet_ntop(AF_INET6, &a->sin6_addr, straddr, sizeof(straddr)), ntohs( a->sin6_port ), fd );
562+
#endif
540563
} else {
541564
MIDILog( DEBUG, "send %i bytes to <unknown addr family> on s(%i)\n", len, fd );
542565
}
@@ -569,6 +592,13 @@ static int _applemidi_recv_command( struct MIDIDriverAppleMIDI * driver, int fd,
569592
if( command->addr.ss_family == AF_INET ) {
570593
struct sockaddr_in * a = (struct sockaddr_in *) &(command->addr);
571594
MIDILog( DEBUG, "recv %i bytes from %s:%i on s(%i)\n", len, inet_ntoa( a->sin_addr ), ntohs( a->sin_port ), fd );
595+
#if (defined(AF_INET6))
596+
} else if (command->addr.ss_family == AF_INET6 ) {
597+
char straddr[INET6_ADDRSTRLEN];
598+
struct sockaddr_in6 * a = (struct sockaddr_in6 *) &(command->addr);
599+
MIDILog( DEBUG, "recv %i bytes from %s:%i on s(%i)\n", len,
600+
inet_ntop(AF_INET6, &a->sin6_addr, straddr, sizeof(straddr)), ntohs( a->sin6_port ), fd );
601+
#endif
572602
} else {
573603
MIDILog( DEBUG, "recv %i bytes from <unknown addr family> on s(%i)\n", len, fd );
574604
}
@@ -734,14 +764,21 @@ static int _applemidi_endsession( struct MIDIDriverAppleMIDI * driver, int fd, s
734764
* @return >0 on error.
735765
*/
736766
static int _applemidi_rtp_addr( socklen_t size, struct sockaddr * control_addr, struct sockaddr * rtp_addr ) {
737-
struct sockaddr_in * in_addr;
738767
if( control_addr != rtp_addr ) {
739768
memcpy( rtp_addr, control_addr, size );
740769
}
741770
if( rtp_addr->sa_family == AF_INET ) {
771+
struct sockaddr_in * in_addr;
742772
in_addr = (struct sockaddr_in *) rtp_addr;
743773
in_addr->sin_port = htons( ntohs( in_addr->sin_port ) + 1 );
744774
return 0;
775+
#if (defined(AF_INET6))
776+
} else if ( rtp_addr->sa_family == AF_INET6 ) {
777+
struct sockaddr_in6 * in_addr;
778+
in_addr = (struct sockaddr_in6 *) rtp_addr;
779+
in_addr->sin6_port = htons( ntohs( in_addr->sin6_port ) + 1 );
780+
return 0;
781+
#endif
745782
} else {
746783
return 1;
747784
}
@@ -756,14 +793,21 @@ static int _applemidi_rtp_addr( socklen_t size, struct sockaddr * control_addr,
756793
* @return >0 on error.
757794
*/
758795
static int _applemidi_control_addr( socklen_t size, struct sockaddr * rtp_addr, struct sockaddr * control_addr ) {
759-
struct sockaddr_in * in_addr;
760796
if( rtp_addr != control_addr ) {
761797
memcpy( control_addr, rtp_addr, size );
762798
}
763799
if( control_addr->sa_family == AF_INET ) {
800+
struct sockaddr_in * in_addr;
764801
in_addr = (struct sockaddr_in *) control_addr;
765802
in_addr->sin_port = htons( ntohs( in_addr->sin_port ) - 1 );
766803
return 0;
< F438 code>804+
#if (defined(AF_INET6))
805+
} else if ( control_addr->sa_family == AF_INET6 ) {
806+
struct sockaddr_in6 * in_addr;
807+
in_addr = (struct sockaddr_in6 *) control_addr;
808+
in_addr->sin6_port = htons( ntohs( in_addr->sin6_port ) - 1 );
809+
return 0;
810+
#endif
767811
} else {
768812
return 1;
769813
}

driver/osc/osc.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,45 @@ struct MIDIDriverOSC {
2222
struct MIDIMessageQueue * out_queue;
2323
};
2424

25-
struct MIDIDriverOSC * MIDIDriverOSCCreate( ) {
26-
struct MIDIDriverOSC * driver = malloc( sizeof( struct MIDIDriverOSC ) );
25+
static int _osc_bind( int fd, int port ) {
26+
#if (defined(AF_INET6) && defined(ENABLE_IPV6))
27+
struct sockaddr_in6 addr;
28+
memset(&addr, 0, sizeof(addr));
29+
addr.sin6_family = AF_INET6;
30+
addr.sin6_addr = in6addr_any;
31+
addr.sin6_port = htons( port );
32+
#else
2733
struct sockaddr_in addr;
2834
memset(&addr, 0, sizeof(addr));
29-
30-
driver->refs = 1;
31-
driver->socket = socket( PF_INET, SOCK_DGRAM, 0 );
32-
33-
if (driver->socket == -1)
34-
{
35-
free (driver);
36-
return NULL;
37-
}
38-
3935
addr.sin_family = AF_INET;
40-
addr.sin_port = 5006;
4136
addr.sin_addr.s_addr = INADDR_ANY;
42-
43-
bind( driver->socket, (struct sockaddr *) &addr, sizeof(addr) );
44-
37+
addr.sin_port = htons( port );
38+
#endif
39+
return bind( fd, (struct sockaddr *) &addr, sizeof(addr) );
40+
}
41+
42+
43+
struct MIDIDriverOSC * MIDIDriverOSCCreate( ) {
44+
struct MIDIDriverOSC * driver = malloc( sizeof( struct MIDIDriverOSC ) );
45+
46+
driver->refs = 1;
47+
#if (defined(AF_INET6) && defined(ENABLE_IPV6))
48+
driver->socket = socket( PF_INET6, SOCK_DGRAM, 0 );
49+
#else
50+
driver->socket = socket( PF_INET, SOCK_DGRAM, 0 );
51+
#endif
52+
53+
if( driver->socket == -1 ) {
54+
free( driver );
55+
return NULL;
56+
}
57+
58+
if( _osc_bind( driver->socket, 5006 ) ) {
59+
close( driver->socket );
60+
free( driver );
61+
return NULL;
62+
}
63+
4564
driver->in_queue = MIDIMessageQueueCreate();
4665
driver->out_queue = MIDIMessageQueueCreate();
4766
return driver;

test/Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
PROJECTDIR=..
32
SUBDIR=test
43

@@ -14,6 +13,12 @@ OBJS=$(OBJDIR)/midi.o $(OBJDIR)/util.o $(OBJDIR)/list.o $(OBJDIR)/port.o \
1413
$(OBJDIR)/device.o $(OBJDIR)/driver.o $(OBJDIR)/message_queue.o \
1514
$(OBJDIR)/integration.o $(OBJDIR)/runloop.o \
1615
$(OBJDIR)/driver_rtp.o $(OBJDIR)/driver_applemidi.o
16+
SRCS=midi.c util.c list.c port.c clock.c message_format.c message.c device.c \
17+
driver.c integration.c runloop.c driver_rtp.c driver_applemidi.c
18+
ifeq ($(USE_IPV6),1)
19+
OBJS += $(OBJDIR)/driver_rtpv6.o $(OBJDIR)/driver_applemidiv6.o
20+
SRCS += driver_applemidiv6.c driver_rtpv6.c
21+
endif
1722
BIN=test_main
1823

1924
MAIN_C=main.c
@@ -52,9 +57,11 @@ $(OBJDIR)/integration.o: integration.c test.h
5257
$(OBJDIR)/runloop.o: runloop.c test.h
5358
$(OBJDIR)/driver_rtp.o: driver_rtp.c test.h
5459
$(OBJDIR)/driver_applemidi.o: driver_applemidi.c test.h
60+
$(OBJDIR)/driver_rtpv6.o: driver_rtpv6.c test.h
61+
$(OBJDIR)/driver_applemidiv6.o: driver_applemidiv6.c test.h
5562

5663
tests.passed: $(BINDIR)/$(BIN) $(LIBDIR)/libmidikit$(LIB_SUFFIX) $(LIBDIR)/libmidikit-driver$(LIB_SUFFIX)
5764
LD_LIBRARY_PATH=$(LIBDIR) $(BINDIR)/$(BIN) && touch $@
5865

59-
$(MAIN_C): midi.c util.c list.c port.c clock.c message_format.c message.c device.c driver.c integration.c runloop.c driver_rtp.c driver_applemidi.c
66+
$(MAIN_C): $(SRCS)
6067
./generate_main.sh -o $(MAIN_C) $^

0 commit comments

Comments
 (0)
0