Skip to content

Commit

Permalink
Merge pull request #218 from slokhorst/ipv6-optional
Browse files Browse the repository at this point in the history
Put back option to disable IPv6
  • Loading branch information
jogu authored Oct 9, 2016
2 parents c1e9024 + 2984a15 commit c27023b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ config_param config_params[] = {
"\n############################################################\n"
"# Global Network Options\n"
"############################################################\n\n"
"# Listen to IPv6 localhost instead of IPv4 (default: off)",
"# Enable IPv6 (default: off)",
0,
CONF_OFFSET(ipv6_enabled),
copy_bool,
Expand Down
2 changes: 1 addition & 1 deletion motion-dist.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ timelapse_filename %Y%m%d-timelapse
############################################################
# Global Network Options
############################################################
# Listen to IPv6 localhost instead of IPv4 (default: off)
# Enable IPv6 (default: off)
ipv6_enabled off

############################################################
Expand Down
72 changes: 39 additions & 33 deletions stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ struct auth_param {
struct config *conf;
};

// IPv4 localhost mapped in IPv6 address
// ::ffff:127.0.0.1
#define IN6ADDR_LOOPBACK4_INIT { { { 0,0,0,0,0,0,0,0,0,0,0xff,0xff,0x7f,0,0,1 } } }
const struct in6_addr in6addr_loopback4 = IN6ADDR_LOOPBACK4_INIT;

pthread_mutex_t stream_auth_mutex;

/**
Expand Down Expand Up @@ -720,32 +715,44 @@ static void do_client_auth(struct context *cnt, int sc)
*
* Returns: socket descriptor or -1 if any error happens
*/
int http_bindsock(int port, int local, int ipv6_localhost)
int http_bindsock(int port, int local, int ipv6_enabled)
{
int sd = socket(AF_INET6, SOCK_STREAM, 0);
int sd = socket(ipv6_enabled?AF_INET6:AF_INET, SOCK_STREAM, 0);

int yes = 1, no = 0;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
if (ipv6_enabled)
setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));

char *addr_str;
struct sockaddr_in6 sin;
bzero(&sin, sizeof(struct sockaddr_in6));
sin.sin6_family = AF_INET6;
sin.sin6_port = htons(port);
if(local) {
if(ipv6_localhost) {
struct sockaddr_storage sin;
bzero(&sin, sizeof(struct sockaddr_storage));
sin.ss_family = ipv6_enabled?AF_INET6:AF_INET;
if (ipv6_enabled) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&sin;
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(port);
if(local) {
addr_str = "::1";
sin.sin6_addr = in6addr_loopback;
sin6->sin6_addr = in6addr_loopback;
} else {
addr_str = "127.0.0.1";
sin.sin6_addr = in6addr_loopback4;
addr_str = "any IPv4/IPv6 address";
sin6->sin6_addr = in6addr_any;
}
} else {
addr_str = "any address";
sin.sin6_addr = in6addr_any;
struct sockaddr_in *sin4 = (struct sockaddr_in*)&sin;
sin4->sin_family = AF_INET;
sin4->sin_port = htons(port);
if(local) {
addr_str = "127.0.0.1";
sin4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
} else {
addr_str = "any IPv4 address";
sin4->sin_addr.s_addr = htonl(INADDR_ANY);
}
}
int no = 0, yes = 1;
setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

if (bind(sd, &sin, sizeof(sin)) != 0) {
if (bind(sd, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, "%s: error binding on %s port %d", addr_str, port);
close(sd);
return -1;
Expand All @@ -771,19 +778,18 @@ int http_bindsock(int port, int local, int ipv6_localhost)
static int http_acceptsock(int sl)
{
int sc;
unsigned long i;
struct sockaddr_in6 sin;
socklen_t sin_len = sizeof(sin);

if ((sc = accept(sl, &sin, &sin_len)) >= 0) {
i = 1;
ioctl(sc, FIONBIO, &i);
return sc;
}
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
sc = accept(sl, (struct sockaddr*)&addr, &addr_len);

MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream accept()");
if (sc < 0) {
MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: motion-stream accept()");
return -1;
}

return -1;
unsigned long i = 1;
ioctl(sc, FIONBIO, &i);
return sc;
}


Expand Down

0 comments on commit c27023b

Please sign in to comment.