Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 183457 | Differences between
and this patch

Collapse All | Expand All

(-)a/jjo-tests/libtest.sh (+115 lines)
Line 0 Link Here
1
#!/bin/bash
2
3
typeset -i test_num=0 # increments w/each test_define
4
typeset test_msg	# prefix: "test nr#"  for msgs
5
typeset test_name	# test name given	
6
typeset test_sanename	# test name with all whitespace (and alike) replaced by '_'
7
8
TEST_CLEANUP=":"
9
10
say () {
11
	echo "$@" >&3
12
}
13
err () {
14
	echo "$@" >&4
15
}
16
debug () {
17
	echo "$@" >&4
18
}
19
test_define() {
20
	test_name="$*"
21
	test_sanename="$(echo -n $test_name| tr -c '[A-Za-z0-9._]' _ )"
22
	test_num=test_num+1
23
	test_msg="test $test_num"
24
	test_bg_cleanup 
25
}
26
test_expect_success () {
27
	local msg="$1" 
28
	test $# -ge 2 || { err "usage error: test_expect_failure msg cmd args ..."; return 1; }
29
	say -n "$test_msg: -- $msg (expecting success) "
30
	shift
31
	(eval "$@" 4>&1) && { say -e "\n$test_msg: OK %%% $test_sanename" ;  return 0 ; }
32
	say -e "\n$test_msg: FAILED %%% $test_sanename" 
33
	return 1
34
}
35
36
test_expect_failure () {
37
	local msg="$1" 
38
	test $# -ge 2 || { err "usage error: test_expect_failure msg cmd args ...";  return 1;}
39
	say -n "$test_msg: -- $msg (expecting failure) "
40
	shift
41
	(eval "$@") && { say -e "\n$test_msg: FAILED %%% $test_sanename" >&3 ;  return 0 ; }
42
	say -e "\n$test_msg: OK %%% $test_sanename"
43
	return 1
44
}
45
46
test_bg_egrep() {
47
	local nsecs="$1"
48
	local txt="$2"
49
	local ret
50
	shift 2
51
	test_expect_success "$test_name" \
52
		"set -m ; $@ &> $t/out-$test_sanename &
53
		s=1;
54
		for i in \$(seq 1 $nsecs);do 
55
			say -n '.'
56
			kill -0 %1 || { egrep failed $t/out-$test_sanename >&4; break; }
57
			o=\$(egrep \"$txt\" $t/out-$test_sanename ) && { debug \$o; s=0; break; }
58
			sleep 1;
59
		done;
60
		kill %1
61
		wait
62
		exit \$s
63
		" 2>/dev/null 4>$t/err
64
	ret=$?
65
	test $ret -eq 0 && return 0
66
	say "$test_msg: see: $t/out-$test_sanename*" 
67
	return $ret
68
}
69
# run command in *current* shell ignoring stderr, evals args passed
70
quiet2() {
71
	#backup fd=2
72
	exec 250>&2
73
	exec 2>/dev/null
74
	eval "$@"
75
	#close auxfd
76
	exec 2>&250
77
	exec 250>&-
78
}
79
test_bg_prev(){ local out=$t/out-$test_sanename-prev; $@ >& $out & }
80
test_bg_cleanup() { quiet2 "$TEST_CLEANUP;${@:-:};kill % 2>/dev/null;wait" ;}
81
test_set_cleanup () { TEST_CLEANUP="${*:-:}" ;}
82
83
t=/tmp
84
exec 3>&1
85
exec 4>&2
86
87
#=========
88
89
ln -sf $PWD/openvpn $t/openvpn-test
90
export OPENVPN=$t/openvpn-test
91
92
test_set_cleanup "killall $OPENVPN"
93
94
trap 'test_bg_cleanup;exit' 0 2 15
95
96
test_define "UDP6 loopback"
97
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-udp6-0-loopback-self.sh
98
99
test_define "UDP6 loopback byname"
100
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-udp6-0-loopback-byname.sh
101
102
test_define "TCP6 loopback"
103
test_bg_prev ../jjo-tests/run-tcp6-0-loopback-server.sh
104
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-tcp6-0-loopback-client.sh
105
106
test_define "TCP6 loopback byname"
107
test_bg_prev ../jjo-tests/run-tcp6-0-loopback-server-byname.sh
108
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-tcp6-0-loopback-client-byname.sh
109
110
test_define "UDP4 loopback"
111
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-udp4-0-loopback-self.sh
112
113
test_define "TCP4 loopback"
114
test_bg_prev ../jjo-tests/run-tcp4-0-loopback-server.sh 
115
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-tcp4-0-loopback-client.sh
(-)a/jjo-tests/qemu-ifup-2.jjo.sh (+12 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
#sudo sh -c "/sbin/ifconfig $1 hw ether 00:00:00:00:00:01; /sbin/ifconfig $1 192.168.254.1"
3
export PATH=/sbin:/usr/sbin:$PATH
4
NUM=2
5
DEV=qemutun$NUM
6
sudo sh -x <<EOF
7
ip li set $1 name $DEV
8
ip li set $DEV address 00:00:00:00:00:01
9
ip li set $DEV up
10
ip ad add 192.168.254.1 dev $DEV
11
ip ro add 192.168.254.$NUM dev $DEV
12
EOF
(-)a/jjo-tests/qemu-ifup-3.jjo.sh (+12 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
#sudo sh -c "/sbin/ifconfig $1 hw ether 00:00:00:00:00:01; /sbin/ifconfig $1 192.168.254.1"
3
export PATH=/sbin:/usr/sbin:$PATH
4
NUM=3
5
DEV=qemutun$NUM
6
sudo sh -x <<EOF
7
ip li set $1 name $DEV
8
ip li set $DEV address 00:00:00:00:00:01
9
ip li set $DEV up
10
ip ad add 192.168.254.1 dev $DEV
11
ip ro add 192.168.254.$NUM dev $DEV
12
EOF
(-)a/jjo-tests/run-afunix-0-loop.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
rm -fv /tmp/o.s
4
${OPENVPN} --proto unix-dgram --local /tmp/o.s --remote /tmp/o.s --dev tun --ifconfig 1.1.1.1 1.1.1.2 --secret ../openvpn.key $*
(-)a/jjo-tests/run-afunix-1-o1.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN} --proto unix-dgram --local /tmp/o1.s --remote /tmp/o2.s --dev tun --ifconfig 1.1.1.1 1.1.1.2 --secret ../openvpn.key $*
(-)a/jjo-tests/run-afunix-1-o2.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN} --proto unix-dgram --local /tmp/o2.s --remote /tmp/o1.s --dev tun --ifconfig 1.1.1.2 1.1.1.1 --secret ../openvpn.key $*
(-)a/jjo-tests/run-oxg-1.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev tun --proto udp --remote localhost --lport 5020 --rport 1194 --secret ../openvpn.key --ifconfig 1.1.1.1 1.1.1.2 "$@"
(-)a/jjo-tests/run-oxg-2.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev tun --proto udp --remote localhost --lport 5020 --rport 1194 --secret ../openvpn.key --ifconfig 1.1.1.2 1.1.1.1 "$@"
(-)a/jjo-tests/run-tcp4-0-loopback-client.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN} --dev null --proto tcp-client --remote localhost --rport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp4-0-loopback-server.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp-server --remote localhost --lport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-client-byname.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-client --remote ip6-localhost --rport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-client.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-client --remote ::1 --rport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-server-byname.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-server --local ip6-localhost --lport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-server.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-server --local :: --lport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp4-0-loopback-self.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev null --proto udp --remote localhost --port 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp4-1-qemu-guest.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev tun --proto udp --remote 10.0.2.2 --port 5011 --secret ../openvpn.key --ifconfig 1.1.1.253 1.1.1.1
(-)a/jjo-tests/run-udp4-1-qemu-host.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev tun --proto udp --float --port 5011 --secret ../openvpn.key --ifconfig 1.1.1.1 1.1.1.253
(-)a/jjo-tests/run-udp4-2-MH.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
#pass --local ADDR --remote ADDR
3
: ${OPENVPN:=./openvpn}
4
${GDB} ${OPENVPN} --dev tun --proto udp --port 5011 --secret ../openvpn.key --ifconfig-noexec "$@"
(-)a/jjo-tests/run-udp6-0-loopback-byname.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev null --proto udp6 --remote ip6-localhost --port 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp6-0-loopback-self.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev null --proto udp6 --remote ::1 --port 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp6-1-carpediem.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
REM6="fe80::2e0:7dff:fee3:ef17"
3
: ${OPENVPN:=./openvpn}
4
${GDB} ${OPENVPN} --dev tun --proto udp6 --remote "$REM6" --port 5010 --secret ../openvpn.key --ifconfig 1.1.1.253 1.1.1.1
(-)a/jjo-tests/run-udp6-1-cx.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --verb 5 --dev tun --proto udp6 --port 5010 --secret /home/jjo/src/juanjo/openvpn/openvpn.key --float --ifconfig 1.1.1.1 1.1.1.253
(-)a/jjo-tests/run-udp6-2-qemu-guest.sh (+8 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
case "$1" in
4
--freebsd) shift;set --   ed0 "$@";;
5
--openbsd) shift;set --   ne3 --dev tun0 "$@";;
6
esac
7
DEV=${1:?missing devname};shift
8
${GDB} ${OPENVPN} --dev tun --proto udp6 --remote fe80::200:ff:fe00:1%$DEV --port 5010 --secret ../openvpn.key "$@" --ifconfig 1.1.1.253 1.1.1.1
(-)a/jjo-tests/run-udp6-2-qemu-host.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
DEV=${1:?missing devname};shift
4
${GDB} ${OPENVPN} --dev tun --proto udp6 --remote fe80::5054:ff:fe12:3456%$DEV --port 5010 --secret ../openvpn.key --ifconfig  1.1.1.1 1.1.1.253 "$@"
(-)a/openvpn/buffer.c (+13 lines)
Lines 198-203 buf_printf (struct buffer *buf, const ch Link Here
198
    }
198
    }
199
}
199
}
200
200
201
void buf_puts(struct buffer *buf, const char *str)
202
{
203
  uint8_t *ptr = BEND (buf);
204
  int cap = buf_forward_capacity (buf);
205
  if (cap > 0)
206
    {
207
      strncpynt ((char *)ptr,str, cap);
208
      *(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */
209
      buf->len += (int) strlen ((char *)ptr);
210
    }
211
}
212
 
213
201
/*
214
/*
202
 * This is necessary due to certain buggy implementations of snprintf,
215
 * This is necessary due to certain buggy implementations of snprintf,
203
 * that don't guarantee null termination for size > 0.
216
 * that don't guarantee null termination for size > 0.
(-)a/openvpn/configure.ac (+16 lines)
Lines 101-106 AC_ARG_ENABLE(multihome, Link Here
101
   [MULTIHOME="yes"]
101
   [MULTIHOME="yes"]
102
)
102
)
103
103
104
AC_ARG_ENABLE(ipv6,
105
   [  --disable-ipv6          Disable UDP/IPv6 support],
106
   [PF_INET6="$enableval"],
107
   [PF_INET6="yes"]
108
)
109
104
AC_ARG_ENABLE(port-share,
110
AC_ARG_ENABLE(port-share,
105
   [  --disable-port-share    Disable TCP server port-share support (--port-share)],
111
   [  --disable-port-share    Disable TCP server port-share support (--port-share)],
106
   [PORT_SHARE="$enableval"],
112
   [PORT_SHARE="$enableval"],
Lines 406-411 LDFLAGS="$LDFLAGS -Wl,--fatal-warnings" Link Here
406
AC_CHECK_FUNCS(epoll_create, AC_DEFINE([HAVE_EPOLL_CREATE], 1, []))
412
AC_CHECK_FUNCS(epoll_create, AC_DEFINE([HAVE_EPOLL_CREATE], 1, []))
407
LDFLAGS="$OLDLDFLAGS"
413
LDFLAGS="$OLDLDFLAGS"
408
414
415
dnl ipv6 support
416
if test "$PF_INET6" = "yes"; then
417
  AC_CHECKING([for struct sockaddr_in6 for IPv6 support])
418
  AC_CHECK_TYPE(
419
      [struct sockaddr_in6],
420
      [AC_DEFINE(USE_PF_INET6, 1, [struct sockaddr_in6 is needed for IPv6 peer support])],
421
      [],
422
      [#include "syshead.h"])
423
fi
424
409
dnl
425
dnl
410
dnl check for valgrind tool
426
dnl check for valgrind tool
411
dnl
427
dnl
(-)a/openvpn/helper.c (+4 lines)
Lines 358-363 #endif /* P2MP_SERVER */ Link Here
358
358
359
      if (o->proto == PROTO_TCPv4)
359
      if (o->proto == PROTO_TCPv4)
360
	o->proto = PROTO_TCPv4_CLIENT;
360
	o->proto = PROTO_TCPv4_CLIENT;
361
#ifdef USE_PF_INET6
362
      else if (o->proto == PROTO_TCPv6)
363
	o->proto = PROTO_TCPv6_CLIENT;
364
#endif
361
    }
365
    }
362
366
363
#endif /* P2MP */
367
#endif /* P2MP */
(-)a/openvpn/init.c (-15 / +18 lines)
Lines 703-709 #ifdef ENABLE_MANAGEMENT Link Here
703
      const char *detail = "SUCCESS";
703
      const char *detail = "SUCCESS";
704
      if (c->c1.tuntap)
704
      if (c->c1.tuntap)
705
	tun_local = c->c1.tuntap->local;
705
	tun_local = c->c1.tuntap->local;
706
      tun_remote = htonl (c->c1.link_socket_addr.actual.dest.sa.sin_addr.s_addr);
706
      tun_remote = htonl (c->c1.link_socket_addr.actual.dest.addr.in4.sin_addr.s_addr);
707
      if (flags & ISC_ERRORS)
707
      if (flags & ISC_ERRORS)
708
	detail = "ERROR";
708
	detail = "ERROR";
709
      management_set_state (management,
709
      management_set_state (management,
Lines 1105-1111 do_deferred_options (struct context *c, Link Here
1105
#ifdef ENABLE_OCC
1105
#ifdef ENABLE_OCC
1106
  if (found & OPT_P_EXPLICIT_NOTIFY)
1106
  if (found & OPT_P_EXPLICIT_NOTIFY)
1107
    {
1107
    {
1108
      if (c->options.proto != PROTO_UDPv4 && c->options.explicit_exit_notification)
1108
      if (!proto_is_udp(c->options.proto) && c->options.explicit_exit_notification)
1109
	{
1109
	{
1110
	  msg (D_PUSH, "OPTIONS IMPORT: --explicit-exit-notify can only be used with --proto udp");
1110
	  msg (D_PUSH, "OPTIONS IMPORT: --explicit-exit-notify can only be used with --proto udp");
1111
	  c->options.explicit_exit_notification = 0;
1111
	  c->options.explicit_exit_notification = 0;
Lines 1200-1211 #endif Link Here
1200
  switch (c->options.proto)
1200
  switch (c->options.proto)
1201
    {
1201
    {
1202
    case PROTO_UDPv4:
1202
    case PROTO_UDPv4:
1203
#ifdef USE_PF_INET6
1204
    case PROTO_UDPv6:
1205
#endif
1203
      if (proxy)
1206
      if (proxy)
1204
	sec = c->options.connect_retry_seconds;
1207
	sec = c->options.connect_retry_seconds;
1205
      break;
1208
      break;
1209
#ifdef USE_PF_INET6
1210
    case PROTO_TCPv6_SERVER:
1211
#endif
1206
    case PROTO_TCPv4_SERVER:
1212
    case PROTO_TCPv4_SERVER:
1207
      sec = 1;
1213
      sec = 1;
1208
      break;
1214
      break;
1215
#ifdef USE_PF_INET6
1216
    case PROTO_TCPv6_CLIENT:
1217
#endif
1209
    case PROTO_TCPv4_CLIENT:
1218
    case PROTO_TCPv4_CLIENT:
1210
      sec = c->options.connect_retry_seconds;
1219
      sec = c->options.connect_retry_seconds;
1211
      break;
1220
      break;
Lines 2292-2298 do_setup_fast_io (struct context *c) Link Here
2292
#ifdef WIN32
2301
#ifdef WIN32
2293
      msg (M_INFO, "NOTE: --fast-io is disabled since we are running on Windows");
2302
      msg (M_INFO, "NOTE: --fast-io is disabled since we are running on Windows");
2294
#else
2303
#else
2295
      if (c->options.proto != PROTO_UDPv4)
2304
      if (!proto_is_udp(c->options.proto))
2296
	msg (M_INFO, "NOTE: --fast-io is disabled since we are not using UDP");
2305
	msg (M_INFO, "NOTE: --fast-io is disabled since we are not using UDP");
2297
      else
2306
      else
2298
	{
2307
	{
Lines 2549-2555 init_instance (struct context *c, const Link Here
2549
  /* link_socket_mode allows CM_CHILD_TCP
2558
  /* link_socket_mode allows CM_CHILD_TCP
2550
     instances to inherit acceptable fds
2559
     instances to inherit acceptable fds
2551
     from a top-level parent */
2560
     from a top-level parent */
2561
#ifdef USE_PF_INET6
2562
  if (c->options.proto == PROTO_TCPv4_SERVER || c->options.proto == PROTO_TCPv6_SERVER)
2563
#else
2552
  if (c->options.proto == PROTO_TCPv4_SERVER)
2564
  if (c->options.proto == PROTO_TCPv4_SERVER)
2565
#endif
2553
    {
2566
    {
2554
      if (c->mode == CM_TOP)
2567
      if (c->mode == CM_TOP)
2555
	link_socket_mode = LS_MODE_TCP_LISTEN;
2568
	link_socket_mode = LS_MODE_TCP_LISTEN;
Lines 2809-2825 inherit_context_child (struct context *d Link Here
2809
{
2822
{
2810
  CLEAR (*dest);
2823
  CLEAR (*dest);
2811
2824
2812
  switch (src->options.proto)
2825
  dest->mode = proto_is_dgram(src->options.proto)? CM_CHILD_UDP : CM_CHILD_TCP;
2813
    {
2814
    case PROTO_UDPv4:
2815
      dest->mode = CM_CHILD_UDP;
2816
      break;
2817
    case PROTO_TCPv4_SERVER:
2818
      dest->mode = CM_CHILD_TCP;
2819
      break;
2820
    default:
2821
      ASSERT (0);
2822
    }
2823
2826
2824
  dest->gc = gc_new ();
2827
  dest->gc = gc_new ();
2825
2828
Lines 2924-2930 #endif Link Here
2924
  dest->c2.buffers_owned = false;
2927
  dest->c2.buffers_owned = false;
2925
2928
2926
  dest->c2.event_set = NULL;
2929
  dest->c2.event_set = NULL;
2927
  if (src->options.proto == PROTO_UDPv4)
2930
  if (proto_is_dgram(src->options.proto))
2928
    do_event_set_init (dest, false);
2931
    do_event_set_init (dest, false);
2929
}
2932
}
2930
2933
(-)a/openvpn/manage.c (-5 / +5 lines)
Lines 1351-1359 man_settings_init (struct man_settings * Link Here
1351
      /*
1351
      /*
1352
       * Initialize socket address
1352
       * Initialize socket address
1353
       */
1353
       */
1354
      ms->local.sa.sin_family = AF_INET;
1354
      ms->local.addr.in4.sin_family = AF_INET;
1355
      ms->local.sa.sin_addr.s_addr = 0;
1355
      ms->local.addr.in4.sin_addr.s_addr = 0;
1356
      ms->local.sa.sin_port = htons (port);
1356
      ms->local.addr.in4.sin_port = htons (port);
1357
1357
1358
      /*
1358
      /*
1359
       * Run management over tunnel, or
1359
       * Run management over tunnel, or
Lines 1365-1371 man_settings_init (struct man_settings * Link Here
1365
	}
1365
	}
1366
      else
1366
      else
1367
	{
1367
	{
1368
	  ms->local.sa.sin_addr.s_addr = getaddr
1368
	  ms->local.addr.in4.sin_addr.s_addr = getaddr
1369
	    (GETADDR_RESOLVE|GETADDR_WARN_ON_SIGNAL|GETADDR_FATAL, addr, 0, NULL, NULL);
1369
	    (GETADDR_RESOLVE|GETADDR_WARN_ON_SIGNAL|GETADDR_FATAL, addr, 0, NULL, NULL);
1370
	}
1370
	}
1371
      
1371
      
Lines 1634-1640 management_post_tunnel_open (struct mana Link Here
1634
      && man->connection.state == MS_INITIAL)
1634
      && man->connection.state == MS_INITIAL)
1635
    {
1635
    {
1636
      /* listen on our local TUN/TAP IP address */
1636
      /* listen on our local TUN/TAP IP address */
1637
      man->settings.local.sa.sin_addr.s_addr = htonl (tun_local_ip);
1637
      man->settings.local.addr.in4.sin_addr.s_addr = htonl (tun_local_ip);
1638
      man_connection_init (man);
1638
      man_connection_init (man);
1639
    }
1639
    }
1640
1640
(-)a/openvpn/mroute.c (-4 / +56 lines)
Lines 175-199 bool mroute_extract_openvpn_sockaddr (st Link Here
175
				      const struct openvpn_sockaddr *osaddr,
175
				      const struct openvpn_sockaddr *osaddr,
176
				      bool use_port)
176
				      bool use_port)
177
{
177
{
178
  if (osaddr->sa.sin_family == AF_INET)
178
  switch (osaddr->addr.sa.sa_family) 
179
  {
180
    case AF_INET:
179
    {
181
    {
180
      if (use_port)
182
      if (use_port)
181
	{
183
	{
182
	  addr->type = MR_ADDR_IPV4 | MR_WITH_PORT;
184
	  addr->type = MR_ADDR_IPV4 | MR_WITH_PORT;
183
	  addr->netbits = 0;
185
	  addr->netbits = 0;
184
	  addr->len = 6;
186
	  addr->len = 6;
185
	  memcpy (addr->addr, &osaddr->sa.sin_addr.s_addr, 4);
187
	  memcpy (addr->addr, &osaddr->addr.in4.sin_addr.s_addr, 4);
186
	  memcpy (addr->addr + 4, &osaddr->sa.sin_port, 2);
188
	  memcpy (addr->addr + 4, &osaddr->addr.in4.sin_port, 2);
187
	}
189
	}
188
      else
190
      else
189
	{
191
	{
190
	  addr->type = MR_ADDR_IPV4;
192
	  addr->type = MR_ADDR_IPV4;
191
	  addr->netbits = 0;
193
	  addr->netbits = 0;
192
	  addr->len = 4;
194
	  addr->len = 4;
193
	  memcpy (addr->addr, &osaddr->sa.sin_addr.s_addr, 4);
195
	  memcpy (addr->addr, &osaddr->addr.in4.sin_addr.s_addr, 4);
194
	}
196
	}
195
      return true;
197
      return true;
196
    }
198
    }
199
#ifdef USE_PF_INET6
200
    case AF_INET6:
201
      if (use_port)
202
	{
203
	  addr->type = MR_ADDR_IPV6 | MR_WITH_PORT;
204
	  addr->netbits = 0;
205
	  addr->len = 18;
206
	  memcpy (addr->addr, &osaddr->addr.in6.sin6_addr, 16);
207
	  memcpy (addr->addr + 16, &osaddr->addr.in6.sin6_port, 2);
208
	}
209
      else
210
	{
211
	  addr->type = MR_ADDR_IPV6;
212
	  addr->netbits = 0;
213
	  addr->len = 16;
214
	  memcpy (addr->addr, &osaddr->addr.in6.sin6_addr, 16);
215
	}
216
      return true;
217
#endif
218
  }
197
  return false;
219
  return false;
198
}
220
}
199
221
Lines 268-274 mroute_addr_print (const struct mroute_a Link Here
268
	  }
290
	  }
269
	  break;
291
	  break;
270
	case MR_ADDR_IPV6:
292
	case MR_ADDR_IPV6:
293
#ifdef USE_PF_INET6
294
          {
295
	    struct buffer buf;
296
	    struct sockaddr_in6 sin6;
297
	    int port;
298
	    char buf6[INET6_ADDRSTRLEN] = "";
299
	    memset(&sin6, 0, sizeof sin6);
300
	    sin6.sin6_family = AF_INET6;
301
	    buf_set_read (&buf, maddr.addr, maddr.len);
302
            if (buf_read(&buf, &sin6.sin6_addr, sizeof (sin6.sin6_addr)))
303
            {
304
              if (getnameinfo((struct sockaddr *)&sin6, sizeof (struct sockaddr_in6),
305
                                      buf6, sizeof (buf6), NULL, 0, NI_NUMERICHOST) != 0)
306
                {
307
                  buf_printf (&out, "MR_ADDR_IPV6 getnameinfo() err");
308
                  break;
309
		}
310
              buf_puts (&out, buf6);
311
	      if (maddr.type & MR_WITH_NETBITS)
312
	        buf_printf (&out, "/%d", maddr.netbits);
313
              if (maddr.type & MR_WITH_PORT)
314
                {
315
                  port = buf_read_u16 (&buf);
316
                  if (port >= 0)
317
                    buf_printf (&out, ":%d", port);
318
                }
319
	    }
320
          }
321
#else /* old pre IPV6 1-line code: */
271
	  buf_printf (&out, "IPV6"); 
322
	  buf_printf (&out, "IPV6"); 
323
#endif
272
	  break;
324
	  break;
273
	default:
325
	default:
274
	  buf_printf (&out, "UNKNOWN"); 
326
	  buf_printf (&out, "UNKNOWN"); 
(-)a/openvpn/mtcp.c (+1 lines)
Lines 159-164 multi_tcp_instance_specific_init (struct Link Here
159
  ASSERT (mi->context.c2.link_socket);
159
  ASSERT (mi->context.c2.link_socket);
160
  ASSERT (mi->context.c2.link_socket->info.lsa);
160
  ASSERT (mi->context.c2.link_socket->info.lsa);
161
  ASSERT (mi->context.c2.link_socket->mode == LS_MODE_TCP_ACCEPT_FROM);
161
  ASSERT (mi->context.c2.link_socket->mode == LS_MODE_TCP_ACCEPT_FROM);
162
  ASSERT (mi->context.c2.link_socket->info.lsa->actual.dest.addr.sa.sa_family == AF_INET);
162
  if (!mroute_extract_openvpn_sockaddr (&mi->real, &mi->context.c2.link_socket->info.lsa->actual.dest, true))
163
  if (!mroute_extract_openvpn_sockaddr (&mi->real, &mi->context.c2.link_socket->info.lsa->actual.dest, true))
163
    {
164
    {
164
      msg (D_MULTI_ERRORS, "MULTI TCP: TCP client address is undefined");
165
      msg (D_MULTI_ERRORS, "MULTI TCP: TCP client address is undefined");
(-)a/openvpn/multi.c (-15 / +10 lines)
Lines 990-997 multi_learn_in_addr_t (struct multi_cont Link Here
990
  struct mroute_addr addr;
990
  struct mroute_addr addr;
991
991
992
  CLEAR (remote_si);
992
  CLEAR (remote_si);
993
  remote_si.sa.sin_family = AF_INET;
993
  remote_si.addr.in4.sin_family = AF_INET;
994
  remote_si.sa.sin_addr.s_addr = htonl (a);
994
  remote_si.addr.in4.sin_addr.s_addr = htonl (a);
995
  ASSERT (mroute_extract_openvpn_sockaddr (&addr, &remote_si, false));
995
  ASSERT (mroute_extract_openvpn_sockaddr (&addr, &remote_si, false));
996
996
997
  if (netbits >= 0)
997
  if (netbits >= 0)
Lines 2224-2232 management_callback_kill_by_addr (void * Link Here
2224
  int count = 0;
2224
  int count = 0;
2225
2225
2226
  CLEAR (saddr);
2226
  CLEAR (saddr);
2227
  saddr.sa.sin_family = AF_INET;
2227
  saddr.addr.in4.sin_family = AF_INET;
2228
  saddr.sa.sin_addr.s_addr = htonl (addr);
2228
  saddr.addr.in4.sin_addr.s_addr = htonl (addr);
2229
  saddr.sa.sin_port = htons (port);
2229
  saddr.addr.in4.sin_port = htons (port);
2230
  if (mroute_extract_openvpn_sockaddr (&maddr, &saddr, true))
2230
  if (mroute_extract_openvpn_sockaddr (&maddr, &saddr, true))
2231
    {
2231
    {
2232
      hash_iterator_init (m->iter, &hi, true);
2232
      hash_iterator_init (m->iter, &hi, true);
Lines 2287-2304 tunnel_server (struct context *top) Link Here
2287
{
2287
{
2288
  ASSERT (top->options.mode == MODE_SERVER);
2288
  ASSERT (top->options.mode == MODE_SERVER);
2289
2289
2290
  switch (top->options.proto) {
2290
  if (proto_is_dgram(top->options.proto))
2291
  case PROTO_UDPv4:
2291
    tunnel_server_udp(top);
2292
    tunnel_server_udp (top);
2292
  else
2293
    break;
2293
    tunnel_server_tcp(top);
2294
  case PROTO_TCPv4_SERVER:
2295
    tunnel_server_tcp (top);
2296
    break;
2297
  default:
2298
    ASSERT (0);
2299
  }
2300
}
2294
}
2301
2295
2296
2302
#else
2297
#else
2303
static void dummy(void) {}
2298
static void dummy(void) {}
2304
#endif /* P2MP_SERVER */
2299
#endif /* P2MP_SERVER */
(-)a/openvpn/occ.c (-1 / +1 lines)
Lines 375-381 process_received_occ_msg (struct context Link Here
375
	       c->c2.max_send_size_remote,
375
	       c->c2.max_send_size_remote,
376
	       c->c2.max_recv_size_local);
376
	       c->c2.max_recv_size_local);
377
	  if (!c->options.fragment
377
	  if (!c->options.fragment
378
	      && c->options.proto == PROTO_UDPv4
378
	      && (proto_is_dgram(c->options.proto))
379
	      && c->c2.max_send_size_local > TUN_MTU_MIN
379
	      && c->c2.max_send_size_local > TUN_MTU_MIN
380
	      && (c->c2.max_recv_size_remote < c->c2.max_send_size_local
380
	      && (c->c2.max_recv_size_remote < c->c2.max_send_size_local
381
		  || c->c2.max_recv_size_local < c->c2.max_send_size_remote))
381
		  || c->c2.max_recv_size_local < c->c2.max_send_size_remote))
(-)a/openvpn/options.c (-15 / +26 lines)
Lines 73-78 #endif Link Here
73
#ifdef USE_PTHREAD
73
#ifdef USE_PTHREAD
74
  " [PTHREAD]"
74
  " [PTHREAD]"
75
#endif
75
#endif
76
#ifdef ENABLE_IP_PKTINFO
77
  " [MH]"
78
#endif
79
#ifdef USE_PF_INET6
80
  " [PF_INET6]"
81
#endif
76
  " built on " __DATE__
82
  " built on " __DATE__
77
;
83
;
78
84
Lines 93-98 static const char usage_message[] = Link Here
93
  "--mode m        : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
99
  "--mode m        : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
94
  "--proto p       : Use protocol p for communicating with peer.\n"
100
  "--proto p       : Use protocol p for communicating with peer.\n"
95
  "                  p = udp (default), tcp-server, or tcp-client\n"
101
  "                  p = udp (default), tcp-server, or tcp-client\n"
102
#ifdef USE_PF_INET6
103
  "                  p = udp6, tcp6-server, or tcp6-client (IPv6)\n"
104
#endif
96
  "--connect-retry n : For --proto tcp-client, number of seconds to wait\n"
105
  "--connect-retry n : For --proto tcp-client, number of seconds to wait\n"
97
  "                    between connection retries (default=%d).\n"
106
  "                    between connection retries (default=%d).\n"
98
  "--connect-timeout n : For --proto tcp-client, connection timeout (in seconds).\n"
107
  "--connect-timeout n : For --proto tcp-client, connection timeout (in seconds).\n"
Lines 1421-1430 #endif Link Here
1421
   * Sanity check on TCP mode options
1430
   * Sanity check on TCP mode options
1422
   */
1431
   */
1423
1432
1424
  if (options->connect_retry_defined && options->proto != PROTO_TCPv4_CLIENT)
1433
  if (options->connect_retry_defined && options->proto != PROTO_TCPv4_CLIENT && options->proto != PROTO_TCPv6_CLIENT)
1425
    msg (M_USAGE, "--connect-retry doesn't make sense unless also used with --proto tcp-client");
1434
    msg (M_USAGE, "--connect-retry doesn't make sense unless also used with --proto tcp-client");
1426
1435
1427
  if (options->connect_timeout_defined && options->proto != PROTO_TCPv4_CLIENT)
1436
  if (options->connect_timeout_defined && options->proto != PROTO_TCPv4_CLIENT && options->proto != PROTO_TCPv6_CLIENT)
1428
    msg (M_USAGE, "--connect-timeout doesn't make sense unless also used with --proto tcp-client");
1437
    msg (M_USAGE, "--connect-timeout doesn't make sense unless also used with --proto tcp-client");
1429
1438
1430
  /*
1439
  /*
Lines 1434-1440 #endif Link Here
1434
    msg (M_USAGE, "only one of --tun-mtu or --link-mtu may be defined (note that --ifconfig implies --link-mtu %d)", LINK_MTU_DEFAULT);
1443
    msg (M_USAGE, "only one of --tun-mtu or --link-mtu may be defined (note that --ifconfig implies --link-mtu %d)", LINK_MTU_DEFAULT);
1435
1444
1436
#ifdef ENABLE_OCC
1445
#ifdef ENABLE_OCC
1437
  if (options->proto != PROTO_UDPv4 && options->mtu_test)
1446
  if (!proto_is_udp(options->proto) && options->mtu_test)
1438
    msg (M_USAGE, "--mtu-test only makes sense with --proto udp");
1447
    msg (M_USAGE, "--mtu-test only makes sense with --proto udp");
1439
#endif
1448
#endif
1440
1449
Lines 1479-1485 #endif Link Here
1479
	  const char *remote = l->array[i].hostname;
1488
	  const char *remote = l->array[i].hostname;
1480
	  const int remote_port = l->array[i].port;
1489
	  const int remote_port = l->array[i].port;
1481
1490
1482
	  if (string_defined_equal (options->local, remote)
1491
	  if (proto_is_net(options->proto)
1492
	      && string_defined_equal (options->local, remote)
1483
	      && options->local_port == remote_port)
1493
	      && options->local_port == remote_port)
1484
	    msg (M_USAGE, "--remote and --local addresses are the same");
1494
	    msg (M_USAGE, "--remote and --local addresses are the same");
1485
	
1495
	
Lines 1508-1514 #endif Link Here
1508
  if (!options->remote_list && !options->bind_local)
1518
  if (!options->remote_list && !options->bind_local)
1509
    msg (M_USAGE, "--nobind doesn't make sense unless used with --remote");
1519
    msg (M_USAGE, "--nobind doesn't make sense unless used with --remote");
1510
1520
1511
  if (options->proto == PROTO_TCPv4_CLIENT && !options->local && !options->local_port_defined && !options->bind_defined)
1521
  if ((options->proto == PROTO_TCPv4_CLIENT||options->proto == PROTO_TCPv6_CLIENT) && !options->local && !options->local_port_defined && !options->bind_defined)
1512
    options->bind_local = false;
1522
    options->bind_local = false;
1513
1523
1514
#ifdef ENABLE_SOCKS
1524
#ifdef ENABLE_SOCKS
Lines 1565-1580 #endif Link Here
1565
   */
1575
   */
1566
1576
1567
#ifdef ENABLE_FRAGMENT
1577
#ifdef ENABLE_FRAGMENT
1568
  if (options->proto != PROTO_UDPv4 && options->fragment)
1578
  if (!proto_is_udp(options->proto) && options->fragment)
1569
    msg (M_USAGE, "--fragment can only be used with --proto udp");
1579
    msg (M_USAGE, "--fragment can only be used with --proto udp");
1570
#endif
1580
#endif
1571
1581
1572
#ifdef ENABLE_OCC
1582
#ifdef ENABLE_OCC
1573
  if (options->proto != PROTO_UDPv4 && options->explicit_exit_notification)
1583
  if (!proto_is_udp(options->proto) && options->explicit_exit_notification)
1574
    msg (M_USAGE, "--explicit-exit-notify can only be used with --proto udp");
1584
    msg (M_USAGE, "--explicit-exit-notify can only be used with --proto udp");
1575
#endif
1585
#endif
1576
1586
1577
  if (!options->remote_list && options->proto == PROTO_TCPv4_CLIENT)
1587
  if (!options->remote_list && (options->proto == PROTO_TCPv4_CLIENT||options->proto == PROTO_TCPv6_CLIENT))
1578
    msg (M_USAGE, "--remote MUST be used in TCP Client mode");
1588
    msg (M_USAGE, "--remote MUST be used in TCP Client mode");
1579
1589
1580
#ifdef ENABLE_HTTP_PROXY
1590
#ifdef ENABLE_HTTP_PROXY
Lines 1592-1598 #ifdef ENABLE_SOCKS Link Here
1592
    msg (M_USAGE, "--socks-proxy can not be used in TCP Server mode");
1602
    msg (M_USAGE, "--socks-proxy can not be used in TCP Server mode");
1593
#endif
1603
#endif
1594
1604
1595
  if (options->proto == PROTO_TCPv4_SERVER && remote_list_len (options->remote_list) > 1)
1605
  if ((options->proto == PROTO_TCPv4_SERVER||options->proto == PROTO_TCPv6_SERVER) 
1606
	  && remote_list_len (options->remote_list) > 1)
1596
    msg (M_USAGE, "TCP server mode allows at most one --remote address");
1607
    msg (M_USAGE, "TCP server mode allows at most one --remote address");
1597
1608
1598
#if P2MP_SERVER
1609
#if P2MP_SERVER
Lines 1617-1623 #endif Link Here
1617
	msg (M_USAGE, "--mode server only works with --dev tun or --dev tap");
1628
	msg (M_USAGE, "--mode server only works with --dev tun or --dev tap");
1618
      if (options->pull)
1629
      if (options->pull)
1619
	msg (M_USAGE, "--pull cannot be used with --mode server");
1630
	msg (M_USAGE, "--pull cannot be used with --mode server");
1620
      if (!(options->proto == PROTO_UDPv4 || options->proto == PROTO_TCPv4_SERVER))
1631
      if (!(proto_is_udp(options->proto) || options->proto == PROTO_TCPv4_SERVER || options->proto == PROTO_TCPv6_SERVER))
1621
	msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server");
1632
	msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server");
1622
#if PORT_SHARE
1633
#if PORT_SHARE
1623
      if ((options->port_share_host || options->port_share_port) && options->proto != PROTO_TCPv4_SERVER)
1634
      if ((options->port_share_host || options->port_share_port) && options->proto != PROTO_TCPv4_SERVER)
Lines 1645-1653 #endif Link Here
1645
	msg (M_USAGE, "--inetd cannot be used with --mode server");
1656
	msg (M_USAGE, "--inetd cannot be used with --mode server");
1646
      if (options->ipchange)
1657
      if (options->ipchange)
1647
	msg (M_USAGE, "--ipchange cannot be used with --mode server (use --client-connect instead)");
1658
	msg (M_USAGE, "--ipchange cannot be used with --mode server (use --client-connect instead)");
1648
      if (!(options->proto == PROTO_UDPv4 || options->proto == PROTO_TCPv4_SERVER))
1659
      if (!(proto_is_dgram(options->proto) || options->proto == PROTO_TCPv4_SERVER || options->proto == PROTO_TCPv6_SERVER ))
1649
	msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server");
1660
	msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server (also udp6/tcp6)");
1650
      if (options->proto != PROTO_UDPv4 && (options->cf_max || options->cf_per))
1661
      if (!proto_is_udp(options->proto) && (options->cf_max || options->cf_per))
1651
	msg (M_USAGE, "--connect-freq only works with --mode server --proto udp.  Try --max-clients instead.");
1662
	msg (M_USAGE, "--connect-freq only works with --mode server --proto udp.  Try --max-clients instead.");
1652
      if (!(dev == DEV_TYPE_TAP || (dev == DEV_TYPE_TUN && options->topology == TOP_SUBNET)) && options->ifconfig_pool_netmask)
1663
      if (!(dev == DEV_TYPE_TAP || (dev == DEV_TYPE_TUN && options->topology == TOP_SUBNET)) && options->ifconfig_pool_netmask)
1653
	msg (M_USAGE, "The third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mode");
1664
	msg (M_USAGE, "The third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mode");
Lines 1724-1730 #ifdef USE_CRYPTO Link Here
1724
  /*
1735
  /*
1725
   * Check consistency of replay options
1736
   * Check consistency of replay options
1726
   */
1737
   */
1727
  if ((options->proto != PROTO_UDPv4)
1738
  if ((!proto_is_udp(options->proto))
1728
      && (options->replay_window != defaults.replay_window
1739
      && (options->replay_window != defaults.replay_window
1729
	  || options->replay_time != defaults.replay_time))
1740
	  || options->replay_time != defaults.replay_time))
1730
    msg (M_USAGE, "--replay-window only makes sense with --proto udp");
1741
    msg (M_USAGE, "--replay-window only makes sense with --proto udp");
Lines 1913-1919 #if P2MP Link Here
1913
   */
1924
   */
1914
  if (options->pull
1925
  if (options->pull
1915
      && options->ping_rec_timeout_action == PING_UNDEF
1926
      && options->ping_rec_timeout_action == PING_UNDEF
1916
      && options->proto == PROTO_UDPv4)
1927
      && proto_is_udp(options->proto))
1917
    {
1928
    {
1918
      options->ping_rec_timeout = PRE_PULL_INITIAL_PING_RESTART;
1929
      options->ping_rec_timeout = PRE_PULL_INITIAL_PING_RESTART;
1919
      options->ping_rec_timeout_action = PING_RESTART;
1930
      options->ping_rec_timeout_action = PING_RESTART;
(-)a/openvpn/ps.c (-3 / +3 lines)
Lines 337-345 sock_addr_set (struct openvpn_sockaddr * Link Here
337
	       const int port)
337
	       const int port)
338
{
338
{
339
  CLEAR (*osaddr);
339
  CLEAR (*osaddr);
340
  osaddr->sa.sin_family = AF_INET;
340
  osaddr->addr.in4.sin_family = AF_INET;
341
  osaddr->sa.sin_addr.s_addr = htonl (addr);
341
  osaddr->addr.in4.sin_addr.s_addr = htonl (addr);
342
  osaddr->sa.sin_port = htons (port);
342
  osaddr->addr.in4.sin_port = htons (port);
343
}
343
}
344
344
345
static inline void
345
static inline void
(-)a/openvpn/socket.c (-236 / +651 lines)
Lines 42-51 #include "manage.h" Link Here
42
#include "memdbg.h"
42
#include "memdbg.h"
43
43
44
const int proto_overhead[] = { /* indexed by PROTO_x */
44
const int proto_overhead[] = { /* indexed by PROTO_x */
45
  IPv4_UDP_HEADER_SIZE,
45
  0,
46
  IPv4_UDP_HEADER_SIZE, /* IPv4 */
46
  IPv4_TCP_HEADER_SIZE,
47
  IPv4_TCP_HEADER_SIZE,
47
  IPv4_TCP_HEADER_SIZE,
48
  IPv4_TCP_HEADER_SIZE,
48
  IPv4_TCP_HEADER_SIZE
49
#ifdef USE_PF_INET6
50
  IPv6_UDP_HEADER_SIZE, /* IPv6 */
51
  IPv6_TCP_HEADER_SIZE,
52
  IPv6_TCP_HEADER_SIZE,
53
  IPv6_TCP_HEADER_SIZE,
54
#endif
49
};
55
};
50
56
51
/*
57
/*
Lines 260-279 update_remote (const char* host, Link Here
260
	       struct openvpn_sockaddr *addr,
266
	       struct openvpn_sockaddr *addr,
261
	       bool *changed)
267
	       bool *changed)
262
{
268
{
263
  if (host && addr)
269
  switch(addr->addr.sa.sa_family) {
264
    {
270
    case AF_INET:
265
      const in_addr_t new_addr = getaddr (
271
      if (host && addr)
266
					  GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE,
272
      {
267
					  host,
273
	const in_addr_t new_addr = getaddr (
268
					  1,
274
	    GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE,
269
					  NULL,
275
	    host,
270
					  NULL);
276
	    1,
271
      if (new_addr && addr->sa.sin_addr.s_addr != new_addr)
277
	    NULL,
278
	    NULL);
279
	if (new_addr && addr->addr.in4.sin_addr.s_addr != new_addr)
272
	{
280
	{
273
	  addr->sa.sin_addr.s_addr = new_addr;
281
	  addr->addr.in4.sin_addr.s_addr = new_addr;
274
	  *changed = true;
282
	  *changed = true;
275
	}
283
	}
276
    }
284
      }
285
      break;
286
#ifdef USE_PF_INET6
287
    case AF_INET6: /* jjoFIXME: should adapt getaddr() for AF_INET6 */
288
      if (host && addr)
289
      {
290
	struct addrinfo hints , *ai;
291
	int err;
292
	memset(&hints, 0, sizeof hints);
293
	hints.ai_flags=AI_PASSIVE;
294
	hints.ai_family=AF_INET6;
295
	if ((err=getaddrinfo(host, NULL, &hints, &ai))==0)
296
	{
297
	  struct sockaddr_in6 *sin6=(struct sockaddr_in6*)ai->ai_addr;
298
	  if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &addr->addr.in6.sin6_addr))
299
	  {
300
	    int port=addr->addr.in6.sin6_port; /* backup current port for easier copy, restore later */
301
	    addr->addr.in6=*sin6; /* ipv6 requires also eg. sin6_scope_id => easy to full copy*/
302
	    addr->addr.in6.sin6_port=port;
303
	  }
304
	  freeaddrinfo(ai);
305
	}
306
      }
307
      break;
308
#endif
309
    default:
310
    	ASSERT(0);
311
  }
277
}
312
}
278
313
279
static int
314
static int
Lines 531-536 #endif Link Here
531
  return sd;
566
  return sd;
532
}
567
}
533
568
569
#ifdef USE_PF_INET6
570
static socket_descriptor_t
571
create_socket_udp6 (const unsigned int flags)
572
{
573
  socket_descriptor_t sd;
574
575
  if ((sd = socket (PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
576
    msg (M_SOCKERR, "UDP: Cannot create UDP6 socket");
577
#if ENABLE_IP_PKTINFO
578
  else if (flags & SF_USE_IP_PKTINFO)
579
    {
580
      int pad = 1;
581
      setsockopt (sd, IPPROTO_IPV6, IPV6_PKTINFO, (void*)&pad, sizeof(pad));
582
    }
583
#endif
584
  return sd;
585
}
586
587
static socket_descriptor_t
588
create_socket_tcp6 (void)
589
{
590
  socket_descriptor_t sd;
591
592
  if ((sd = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0)
593
    msg (M_SOCKERR, "Cannot create TCP6 socket");
594
595
  /* set SO_REUSEADDR on socket */
596
  {
597
    int on = 1;
598
    if (setsockopt (sd, SOL_SOCKET, SO_REUSEADDR,
599
		    (void *) &on, sizeof (on)) < 0)
600
      msg (M_SOCKERR, "TCP: Cannot setsockopt SO_REUSEADDR on TCP6 socket");
601
  }
602
603
  return sd;
604
}
605
606
#endif
534
static void
607
static void
535
create_socket (struct link_socket *sock)
608
create_socket (struct link_socket *sock)
536
{
609
{
Lines 549-554 #endif Link Here
549
    {
622
    {
550
      sock->sd = create_socket_tcp ();
623
      sock->sd = create_socket_tcp ();
551
    }
624
    }
625
#ifdef USE_PF_INET6
626
  else if (sock->info.proto == PROTO_TCPv6_SERVER
627
	   || sock->info.proto == PROTO_TCPv6_CLIENT)
628
    {
629
      sock->sd = create_socket_tcp6 ();
630
    }
631
  else if (sock->info.proto == PROTO_UDPv6)
632
    {
633
      sock->sd = create_socket_udp6 (sock->sockflags);
634
    }
635
#endif
552
  else
636
  else
553
    {
637
    {
554
      ASSERT (0);
638
      ASSERT (0);
Lines 586-592 socket_do_accept (socket_descriptor_t sd Link Here
586
		  struct link_socket_actual *act,
670
		  struct link_socket_actual *act,
587
		  const bool nowait)
671
		  const bool nowait)
588
{
672
{
589
  socklen_t remote_len = sizeof (act->dest.sa);
673
  /* af_addr_size WILL return 0 in this case if AFs other than AF_INET
674
   * are compiled because act is empty here.
675
   * could use getsockname() to support later remote_len check
676
   */
677
  socklen_t remote_len_af = af_addr_size(act->dest.addr.sa.sa_family);
678
  socklen_t remote_len = sizeof(act->dest.addr);
590
  socket_descriptor_t new_sd = SOCKET_UNDEFINED;
679
  socket_descriptor_t new_sd = SOCKET_UNDEFINED;
591
680
592
  CLEAR (*act);
681
  CLEAR (*act);
Lines 594-600 socket_do_accept (socket_descriptor_t sd Link Here
594
#ifdef HAVE_GETPEERNAME
683
#ifdef HAVE_GETPEERNAME
595
  if (nowait)
684
  if (nowait)
596
    {
685
    {
597
      new_sd = getpeername (sd, (struct sockaddr *) &act->dest.sa, &remote_len);
686
      new_sd = getpeername (sd, &act->dest.addr.sa, &remote_len);
598
687
599
      if (!socket_defined (new_sd))
688
      if (!socket_defined (new_sd))
600
	msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: getpeername() failed");
689
	msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: getpeername() failed");
Lines 607-613 #else Link Here
607
#endif
696
#endif
608
  else
697
  else
609
    {
698
    {
610
      new_sd = accept (sd, (struct sockaddr *) &act->dest.sa, &remote_len);
699
      new_sd = accept (sd, &act->dest.addr.sa, &remote_len);
611
    }
700
    }
612
701
613
#if 0 /* For debugging only, test the effect of accept() failures */
702
#if 0 /* For debugging only, test the effect of accept() failures */
Lines 623-629 #endif Link Here
623
    {
712
    {
624
      msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: accept(%d) failed", sd);
713
      msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: accept(%d) failed", sd);
625
    }
714
    }
626
  else if (remote_len != sizeof (act->dest.sa))
715
  /* only valid if we have remote_len_af!=0 */
716
  else if (remote_len_af && remote_len != remote_len_af)
627
    {
717
    {
628
      msg (D_LINK_ERRORS, "TCP: Received strange incoming connection with unknown address length=%d", remote_len);
718
      msg (D_LINK_ERRORS, "TCP: Received strange incoming connection with unknown address length=%d", remote_len);
629
      openvpn_close_socket (new_sd);
719
      openvpn_close_socket (new_sd);
Lines 724-730 socket_bind (socket_descriptor_t sd, Link Here
724
{
814
{
725
  struct gc_arena gc = gc_new ();
815
  struct gc_arena gc = gc_new ();
726
816
727
  if (bind (sd, (struct sockaddr *) &local->sa, sizeof (local->sa)))
817
  if (bind (sd, &local->addr.sa, af_addr_size(local->addr.sa.sa_family)))
728
    {
818
    {
729
      const int errnum = openvpn_errno_socket ();
819
      const int errnum = openvpn_errno_socket ();
730
      msg (M_FATAL, "%s: Socket bind failed on local address %s: %s",
820
      msg (M_FATAL, "%s: Socket bind failed on local address %s: %s",
Lines 745-751 openvpn_connect (socket_descriptor_t sd, Link Here
745
835
746
#ifdef CONNECT_NONBLOCK
836
#ifdef CONNECT_NONBLOCK
747
  set_nonblock (sd);
837
  set_nonblock (sd);
748
  status = connect (sd, (struct sockaddr *) &remote->sa, sizeof (remote->sa));
838
  status = connect (sd, &remote->addr.sa, af_addr_size(remote->addr.sa.sa_family));
749
  if (status)
839
  if (status)
750
    status = openvpn_errno_socket ();
840
    status = openvpn_errno_socket ();
751
  if (status == EINPROGRESS)
841
  if (status == EINPROGRESS)
Lines 829-901 socket_connect (socket_descriptor_t *sd, Link Here
829
919
830
#ifdef CONNECT_NONBLOCK
920
#ifdef CONNECT_NONBLOCK
831
  msg (M_INFO, "Attempting to establish TCP connection with %s [nonblock]", 
921
  msg (M_INFO, "Attempting to establish TCP connection with %s [nonblock]", 
832
       print_sockaddr (remote, &gc));
922
      print_sockaddr (remote, &gc));
833
#else
923
#else
834
  msg (M_INFO, "Attempting to establish TCP connection with %s", 
924
  msg (M_INFO, "Attempting to establish TCP connection with %s", 
835
       print_sockaddr (remote, &gc));
925
      print_sockaddr (remote, &gc));
836
#endif
926
#endif
837
927
838
  while (true)
928
  while (true)
839
    {
929
  {
840
      int status;
930
    int status;
841
931
842
#ifdef ENABLE_MANAGEMENT
932
#ifdef ENABLE_MANAGEMENT
843
      if (management)
933
    if (management)
844
	management_set_state (management,
934
      management_set_state (management,
845
			      OPENVPN_STATE_TCP_CONNECT,
935
	  OPENVPN_STATE_TCP_CONNECT,
846
			      NULL,
936
	  NULL,
847
			      (in_addr_t)0,
937
	  (in_addr_t)0,
848
			      (in_addr_t)0);
938
	  (in_addr_t)0);
849
#endif
939
#endif
850
940
851
      status = openvpn_connect (*sd, remote, connect_timeout, signal_received);
941
    status = openvpn_connect (*sd, remote, connect_timeout, signal_received);
852
942
853
      get_signal (signal_received);
943
    get_signal (signal_received);
854
      if (*signal_received)
944
    if (*signal_received)
855
	goto done;
945
      goto done;
856
946
857
      if (!status)
947
    if (!status)
858
	break;
948
      break;
859
949
860
      msg (D_LINK_ERRORS,
950
    msg (D_LINK_ERRORS,
861
	   "TCP: connect to %s failed, will try again in %d seconds: %s",
951
	"TCP: connect to %s failed, will try again in %d seconds: %s",
862
	   print_sockaddr (remote, &gc),
952
	print_sockaddr (remote, &gc),
863
	   connect_retry_seconds,
953
	connect_retry_seconds,
864
	   strerror_ts (status, &gc));
954
	strerror_ts (status, &gc));
865
955
866
      openvpn_close_socket (*sd);
956
    openvpn_close_socket (*sd);
867
      *sd = SOCKET_UNDEFINED;
957
    *sd = SOCKET_UNDEFINED;
868
958
869
      if (connect_retry_max > 0 && ++retry >= connect_retry_max)
959
    if (connect_retry_max > 0 && ++retry >= connect_retry_max)
870
	{
960
    {
871
	  *signal_received = SIGUSR1;
961
      *signal_received = SIGUSR1;
872
	  goto done;
962
      goto done;
873
	}
963
    }
874
964
875
      openvpn_sleep (connect_retry_seconds);
965
    openvpn_sleep (connect_retry_seconds);
876
966
877
      get_signal (signal_received);
967
    get_signal (signal_received);
878
      if (*signal_received)
968
    if (*signal_received)
879
	goto done;
969
      goto done;
880
970
881
      if (remote_list)
971
    switch(remote->addr.sa.sa_family) {
972
      case AF_INET:
973
	if (remote_list)
882
	{
974
	{
883
	  remote_list_next (remote_list);
975
	  remote_list_next (remote_list);
884
	  remote_dynamic = remote_list_host (remote_list);
976
	  remote_dynamic = remote_list_host (remote_list);
885
	  remote->sa.sin_port = htons (remote_list_port (remote_list));
977
	  remote->addr.in4.sin_port = htons (remote_list_port (remote_list));
886
	  *remote_changed = true;
978
	  *remote_changed = true;
887
	}
979
	}
888
980
889
      *sd = create_socket_tcp ();
981
	*sd = create_socket_tcp ();
890
      if (bind_local)
982
	if (bind_local)
891
        socket_bind (*sd, local, "TCP Client");
983
	  socket_bind (*sd, local, "TCP Client");
892
      update_remote (remote_dynamic, remote, remote_changed);
984
	update_remote (remote_dynamic, remote, remote_changed);
985
	break;
986
#ifdef USE_PF_INET6
987
      case AF_INET6: 
988
	if (remote_list)
989
	{
990
	  remote_list_next (remote_list);
991
	  remote_dynamic = remote_list_host (remote_list);
992
	  remote->addr.in6.sin6_port = htons (remote_list_port (remote_list));
993
	  *remote_changed = true;
994
	}
995
	*sd = create_socket_tcp6 ();
996
	if (bind_local)
997
	  socket_bind (*sd, local, "TCP6 Client");
998
	update_remote (remote_dynamic, remote, remote_changed);
999
	break;
1000
#endif
1001
      default:
1002
	msg(M_FATAL, "Only TCP is supported for connection oriented, sa_family=%d", 
1003
	    remote->addr.sa.sa_family);
893
    }
1004
    }
1005
  }
894
1006
895
  msg (M_INFO, "TCP connection established with %s", 
1007
  msg (M_INFO, "TCP connection established with %s", 
896
       print_sockaddr (remote, &gc));
1008
      print_sockaddr (remote, &gc));
897
1009
898
 done:
1010
done:
899
  gc_free (&gc);
1011
  gc_free (&gc);
900
}
1012
}
901
1013
Lines 950-966 resolve_bind_local (struct link_socket * Link Here
950
1062
951
  /* resolve local address if undefined */
1063
  /* resolve local address if undefined */
952
  if (!addr_defined (&sock->info.lsa->local))
1064
  if (!addr_defined (&sock->info.lsa->local))
953
    {
1065
  {
954
      sock->info.lsa->local.sa.sin_family = AF_INET;
1066
    /* may return AF_{INET|INET6} guessed from local_host */
955
      sock->info.lsa->local.sa.sin_addr.s_addr =
1067
    switch(addr_guess_family(sock->info.proto, sock->local_host)) {
956
	(sock->local_host ? getaddr (GETADDR_RESOLVE | GETADDR_WARN_ON_SIGNAL | GETADDR_FATAL,
1068
      case AF_INET:
957
				     sock->local_host,
1069
	sock->info.lsa->local.addr.in4.sin_family = AF_INET;
958
				     0,
1070
	sock->info.lsa->local.addr.in4.sin_addr.s_addr =
959
				     NULL,
1071
	  (sock->local_host ? getaddr (GETADDR_RESOLVE | GETADDR_WARN_ON_SIGNAL | GETADDR_FATAL,
960
				     NULL)
1072
				       sock->local_host,
961
	 : htonl (INADDR_ANY));
1073
				       0,
962
      sock->info.lsa->local.sa.sin_port = htons (sock->local_port);
1074
				       NULL,
1075
				       NULL)
1076
	   : htonl (INADDR_ANY));
1077
	sock->info.lsa->local.addr.in4.sin_port = htons (sock->local_port);
1078
	break;
1079
#ifdef USE_PF_INET6
1080
      case AF_INET6:
1081
	{
1082
	  struct addrinfo hints , *ai;
1083
	  int err;
1084
	  memset(&hints, 0, sizeof hints);
1085
	  hints.ai_flags=AI_PASSIVE;
1086
	  hints.ai_family=AF_INET6;
1087
	  /* if no local_host provided, ask for IN6ADDR_ANY ... */
1088
	  if ((err=getaddrinfo(sock->local_host? sock->local_host : "::", 
1089
		  NULL, &hints, &ai))==0) {
1090
	    sock->info.lsa->local.addr.in6 = *((struct sockaddr_in6*)(ai->ai_addr));
1091
	    freeaddrinfo(ai);
1092
	  } else {
1093
	    msg (M_FATAL, "getaddrinfo() failed for local \"%s\": %s",
1094
		sock->local_host,
1095
		gai_strerror(err));
1096
	  }
1097
	  sock->info.lsa->local.addr.in6.sin6_port = htons (sock->local_port);
1098
	}
1099
	break;
1100
#endif
963
    }
1101
    }
1102
  }
964
  
1103
  
965
  /* bind to local address/port */
1104
  /* bind to local address/port */
966
  if (sock->bind_local)
1105
  if (sock->bind_local)
Lines 984-1086 resolve_remote (struct link_socket *sock Link Here
984
  struct gc_arena gc = gc_new ();
1123
  struct gc_arena gc = gc_new ();
985
1124
986
  if (!sock->did_resolve_remote)
1125
  if (!sock->did_resolve_remote)
1126
  {
1127
    /* resolve remote address if undefined */
1128
    if (!addr_defined (&sock->info.lsa->remote))
987
    {
1129
    {
988
      /* resolve remote address if undefined */
1130
      switch(addr_guess_family(sock->info.proto, sock->remote_host)) 
989
      if (!addr_defined (&sock->info.lsa->remote))
1131
      {
990
	{
1132
	case AF_INET:
991
	  sock->info.lsa->remote.sa.sin_family = AF_INET;
1133
	  sock->info.lsa->remote.addr.in4.sin_family = AF_INET;
992
	  sock->info.lsa->remote.sa.sin_addr.s_addr = 0;
1134
	  sock->info.lsa->remote.addr.in4.sin_addr.s_addr = 0;
993
1135
994
	  if (sock->remote_host)
1136
	  if (sock->remote_host)
995
	    {
1137
	  {
996
	      unsigned int flags = GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE;
1138
	    unsigned int flags = GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE;
997
	      int retry = 0;
1139
	    int retry = 0;
998
	      bool status = false;
1140
	    bool status = false;
999
1141
1000
	      if (remote_list_len (sock->remote_list) > 1 && sock->resolve_retry_seconds == RESOLV_RETRY_INFINITE)
1142
	    if (remote_list_len (sock->remote_list) > 1 && sock->resolve_retry_seconds == RESOLV_RETRY_INFINITE)
1001
		{
1143
	    {
1002
		  if (phase == 2)
1144
	      if (phase == 2)
1003
		    flags |= (GETADDR_TRY_ONCE | GETADDR_FATAL);
1145
		flags |= (GETADDR_TRY_ONCE | GETADDR_FATAL);
1004
		  retry = 0;
1146
	      retry = 0;
1005
		}
1147
	    }
1006
	      else if (phase == 1)
1148
	    else if (phase == 1)
1007
		{
1149
	    {
1008
		  if (sock->resolve_retry_seconds)
1150
	      if (sock->resolve_retry_seconds)
1009
		    {
1151
	      {
1010
		      retry = 0;
1152
		retry = 0;
1011
		    }
1153
	      }
1012
		  else
1013
		    {
1014
		      flags |= (GETADDR_FATAL | GETADDR_MENTION_RESOLVE_RETRY);
1015
		      retry = 0;
1016
		    }
1017
		}
1018
	      else if (phase == 2)
1019
		{
1020
		  if (sock->resolve_retry_seconds)
1021
		    {
1022
		      flags |= GETADDR_FATAL;
1023
		      retry = sock->resolve_retry_seconds;
1024
		    }
1025
		  else
1026
		    {
1027
		      ASSERT (0);
1028
		    }
1029
		}
1030
	      else
1154
	      else
1031
		{
1155
	      {
1032
		  ASSERT (0);
1156
		flags |= (GETADDR_FATAL | GETADDR_MENTION_RESOLVE_RETRY);
1033
		}
1157
		retry = 0;
1034
1158
	      }
1035
	      sock->info.lsa->remote.sa.sin_addr.s_addr = getaddr (
1159
	    }
1036
		    flags,
1160
	    else if (phase == 2)
1037
		    sock->remote_host,
1161
	    {
1038
		    retry,
1162
	      if (sock->resolve_retry_seconds)
1039
		    &status,
1163
	      {
1040
		    signal_received);
1164
		flags |= GETADDR_FATAL;
1041
	      
1165
		retry = sock->resolve_retry_seconds;
1042
	      dmsg (D_SOCKET_DEBUG, "RESOLVE_REMOTE flags=0x%04x phase=%d rrs=%d sig=%d status=%d",
1166
	      }
1043
		   flags,
1167
	      else
1044
		   phase,
1168
	      {
1045
		   retry,
1169
		ASSERT (0);
1046
		   signal_received ? *signal_received : -1,
1170
	      }
1047
		   status);
1171
	    }
1172
	    else
1173
	    {
1174
	      ASSERT (0);
1175
	    }
1048
1176
1177
	    sock->info.lsa->remote.addr.in4.sin_addr.s_addr = getaddr (
1178
		flags,
1179
		sock->remote_host,
1180
		retry,
1181
		&status,
1182
		signal_received);
1183
1184
	    dmsg (D_SOCKET_DEBUG, "RESOLVE_REMOTE flags=0x%04x phase=%d rrs=%d sig=%d status=%d",
1185
		flags,
1186
		phase,
1187
		retry,
1188
		signal_received ? *signal_received : -1,
1189
		status);
1190
1191
	    if (signal_received)
1192
	    {
1193
	      if (*signal_received)
1194
		goto done;
1195
	    }
1196
	    if (!status)
1197
	    {
1049
	      if (signal_received)
1198
	      if (signal_received)
1050
		{
1199
		*signal_received = SIGUSR1;
1051
		  if (*signal_received)
1200
	      goto done;
1052
		    goto done;
1053
		}
1054
	      if (!status)
1055
		{
1056
		  if (signal_received)
1057
		    *signal_received = SIGUSR1;
1058
		  goto done;
1059
		}
1060
	    }
1201
	    }
1202
	  }
1061
1203
1062
	  sock->info.lsa->remote.sa.sin_port = htons (sock->remote_port);
1204
	  sock->info.lsa->remote.addr.in4.sin_port = htons (sock->remote_port);
1063
	}
1205
	  break;
1064
  
1065
      /* should we re-use previous active remote address? */
1066
      if (link_socket_actual_defined (&sock->info.lsa->actual))
1067
	{
1068
	  msg (M_INFO, "TCP/UDP: Preserving recently used remote address: %s",
1069
	       print_link_socket_actual (&sock->info.lsa->actual, &gc));
1070
	  if (remote_dynamic)
1071
	    *remote_dynamic = NULL;
1072
	}
1073
      else
1074
	{
1075
	  CLEAR (sock->info.lsa->actual);
1076
	  sock->info.lsa->actual.dest = sock->info.lsa->remote;
1077
	}
1078
1206
1079
      /* remember that we finished */
1207
#ifdef USE_PF_INET6
1080
      sock->did_resolve_remote = true;
1208
	case AF_INET6: /* jjoFIXME: ipv6 signal logic */
1209
	  {
1210
	    struct addrinfo hints , *ai;
1211
	    int err;
1212
	    memset(&hints, 0, sizeof hints);
1213
	    hints.ai_flags=0;
1214
	    hints.ai_family=AF_INET6;
1215
	    if ((err=getaddrinfo(sock->remote_host? sock->remote_host : "::" , NULL, &hints, &ai))==0) {
1216
	      sock->info.lsa->remote.addr.in6 = *((struct sockaddr_in6*)(ai->ai_addr));
1217
	      freeaddrinfo(ai);
1218
	    } else {
1219
	      msg (M_FATAL, "getaddrinfo() failed for remote \"%s\": %s",
1220
		  sock->remote_host,
1221
		  gai_strerror(err));
1222
	    }
1223
	    sock->info.lsa->remote.addr.in6.sin6_port = htons (sock->remote_port);
1224
	  }
1225
	  break;
1226
#endif
1227
      }
1228
    }
1229
    /* should we re-use previous active remote address? */
1230
    if (link_socket_actual_defined (&sock->info.lsa->actual))
1231
    {
1232
      msg (M_INFO, "TCP/UDP: Preserving recently used remote address: %s",
1233
	  print_link_socket_actual (&sock->info.lsa->actual, &gc));
1234
      if (remote_dynamic)
1235
	*remote_dynamic = NULL;
1236
    }
1237
    else
1238
    {
1239
      CLEAR (sock->info.lsa->actual);
1240
      sock->info.lsa->actual.dest = sock->info.lsa->remote;
1081
    }
1241
    }
1082
1242
1083
 done:
1243
    /* remember that we finished */
1244
    sock->did_resolve_remote = true;
1245
  }
1246
1247
done:
1084
  gc_free (&gc);
1248
  gc_free (&gc);
1085
}
1249
}
1086
1250
Lines 1310-1316 link_socket_init_phase2 (struct link_soc Link Here
1310
	goto done;
1474
	goto done;
1311
1475
1312
      /* TCP client/server */
1476
      /* TCP client/server */
1313
      if (sock->info.proto == PROTO_TCPv4_SERVER)
1477
      if (sock->info.proto == PROTO_TCPv4_SERVER
1478
#ifdef USE_PF_INET6
1479
	||sock->info.proto == PROTO_TCPv6_SERVER
1480
#endif
1481
	)
1314
	{
1482
	{
1315
	  switch (sock->mode)
1483
	  switch (sock->mode)
1316
	    {
1484
	    {
Lines 1345-1351 link_socket_init_phase2 (struct link_soc Link Here
1345
	      ASSERT (0);
1513
	      ASSERT (0);
1346
	    }
1514
	    }
1347
	}
1515
	}
1348
      else if (sock->info.proto == PROTO_TCPv4_CLIENT)
1516
      else if (sock->info.proto == PROTO_TCPv4_CLIENT
1517
#ifdef USE_PF_INET6
1518
	||sock->info.proto == PROTO_TCPv6_CLIENT
1519
#endif
1520
	)
1349
	{
1521
	{
1350
1522
1351
#ifdef GENERAL_PROXY_SUPPORT
1523
#ifdef GENERAL_PROXY_SUPPORT
Lines 1430-1437 #ifdef ENABLE_SOCKS Link Here
1430
	  sock->remote_port = sock->proxy_dest_port;
1602
	  sock->remote_port = sock->proxy_dest_port;
1431
	  sock->did_resolve_remote = false;
1603
	  sock->did_resolve_remote = false;
1432
1604
1433
	  sock->info.lsa->actual.dest.sa.sin_addr.s_addr = 0;
1605
	  addr_zero_host(&sock->info.lsa->actual.dest);
1434
	  sock->info.lsa->remote.sa.sin_addr.s_addr = 0;
1606
	  addr_zero_host(&sock->info.lsa->remote);
1435
1607
1436
	  resolve_remote (sock, 1, NULL, signal_received);
1608
	  resolve_remote (sock, 1, NULL, signal_received);
1437
1609
Lines 1446-1452 #endif Link Here
1446
      if (remote_changed)
1618
      if (remote_changed)
1447
	{
1619
	{
1448
	  msg (M_INFO, "TCP/UDP: Dynamic remote address changed during TCP connection establishment");
1620
	  msg (M_INFO, "TCP/UDP: Dynamic remote address changed during TCP connection establishment");
1449
	  sock->info.lsa->remote.sa.sin_addr.s_addr = sock->info.lsa->actual.dest.sa.sin_addr.s_addr;
1621
	  addr_copy_host(&sock->info.lsa->remote, &sock->info.lsa->actual.dest);
1450
	}
1622
	}
1451
    }
1623
    }
1452
1624
Lines 1618-1630 link_socket_bad_incoming_addr (struct bu Link Here
1618
{
1790
{
1619
  struct gc_arena gc = gc_new ();
1791
  struct gc_arena gc = gc_new ();
1620
1792
1621
  msg (D_LINK_ERRORS,
1793
  switch(from_addr->dest.addr.sa.sa_family)
1622
       "TCP/UDP: Incoming packet rejected from %s[%d], expected peer address: %s (allow this incoming source address/port by removing --remote or adding --float)",
1794
  {
1623
       print_link_socket_actual (from_addr, &gc),
1795
    case AF_INET:
1624
       (int)from_addr->dest.sa.sin_family,
1796
#ifdef USE_PF_INET6
1625
       print_sockaddr (&info->lsa->remote, &gc));
1797
    case AF_INET6:
1798
#endif
1799
      msg (D_LINK_ERRORS,
1800
	  "TCP/UDP: Incoming packet rejected from %s[%d], expected peer address: %s (allow this incoming source address/port by removing --remote or adding --float)",
1801
	  print_link_socket_actual (from_addr, &gc),
1802
	  (int)from_addr->dest.addr.sa.sa_family,
1803
	  print_sockaddr (&info->lsa->remote, &gc));
1804
      break;
1805
  }
1626
  buf->len = 0;
1806
  buf->len = 0;
1627
1628
  gc_free (&gc);
1807
  gc_free (&gc);
1629
}
1808
}
1630
1809
Lines 1639-1648 link_socket_current_remote (const struct Link Here
1639
{
1818
{
1640
  const struct link_socket_addr *lsa = info->lsa;
1819
  const struct link_socket_addr *lsa = info->lsa;
1641
1820
1821
/* 
1822
 * This logic supports "redirect-gateway" semantic, which 
1823
 * makes sense only for PF_INET routes over PF_INET endpoints
1824
 *
1825
 * Maybe in the future consider PF_INET6 endpoints also ...
1826
 * by now just ignore it
1827
 *
1828
 */
1829
#if defined ( USE_PF_INET6 )
1830
  if(lsa->actual.dest.addr.sa.sa_family != AF_INET)
1831
	  return 0;
1832
#else
1833
  ASSERT(lsa->actual.dest.addr.sa.sa_family == AF_INET);
1834
#endif
1835
1642
  if (link_socket_actual_defined (&lsa->actual))
1836
  if (link_socket_actual_defined (&lsa->actual))
1643
    return ntohl (lsa->actual.dest.sa.sin_addr.s_addr);
1837
    return ntohl (lsa->actual.dest.addr.in4.sin_addr.s_addr);
1644
  else if (addr_defined (&lsa->remote))
1838
  else if (addr_defined (&lsa->remote))
1645
    return ntohl (lsa->remote.sa.sin_addr.s_addr);
1839
    return ntohl (lsa->remote.addr.in4.sin_addr.s_addr);
1646
  else
1840
  else
1647
    return 0;
1841
    return 0;
1648
}
1842
}
Lines 1869-1895 print_sockaddr_ex (const struct openvpn_ Link Here
1869
		   const unsigned int flags,
2063
		   const unsigned int flags,
1870
		   struct gc_arena *gc)
2064
		   struct gc_arena *gc)
1871
{
2065
{
1872
  if (addr)
2066
  struct buffer out;
1873
    {
2067
  bool addr_is_defined;
1874
      struct buffer out = alloc_buf_gc (64, gc);
2068
  if (!addr) {
1875
      const int port = ntohs (addr->sa.sin_port);
2069
    return "[NULL]";
1876
2070
  }
1877
      mutex_lock_static (L_INET_NTOA);
2071
  addr_is_defined =  addr_defined (addr);
1878
      buf_printf (&out, "%s", (addr_defined (addr) ? inet_ntoa (addr->sa.sin_addr) : "[undef]"));
2072
  switch(addr->addr.sa.sa_family) {
1879
      mutex_unlock_static (L_INET_NTOA);
2073
    case AF_INET:
1880
2074
      {
1881
      if (((flags & PS_SHOW_PORT) || (addr_defined (addr) && (flags & PS_SHOW_PORT_IF_DEFINED)))
2075
	const int port= ntohs (addr->addr.in4.sin_port);
1882
	  && port)
2076
	out = alloc_buf_gc (128, gc);
2077
	buf_puts (&out, "[AF_INET]");
2078
	mutex_lock_static (L_INET_NTOA);
2079
	buf_puts (&out, (addr_is_defined ? inet_ntoa (addr->addr.in4.sin_addr) : "[undef]"));
2080
	mutex_unlock_static (L_INET_NTOA);
2081
2082
	if (((flags & PS_SHOW_PORT) || (addr_is_defined && (flags & PS_SHOW_PORT_IF_DEFINED)))
2083
	    && port)
1883
	{
2084
	{
1884
	  if (separator)
2085
	  if (separator)
1885
	    buf_printf (&out, "%s", separator);
2086
	    buf_printf (&out, "%s", separator);
1886
2087
1887
	  buf_printf (&out, "%d", port);
2088
	  buf_printf (&out, "%d", port);
1888
	}
2089
	}
1889
      return BSTR (&out);
2090
      }
1890
    }
2091
      break;
1891
  else
2092
#ifdef USE_PF_INET6
1892
    return "[NULL]";
2093
    case AF_INET6:
2094
      {
2095
	const int port= ntohs (addr->addr.in6.sin6_port);
2096
	char buf[INET6_ADDRSTRLEN] = "[undef]";
2097
	out = alloc_buf_gc (128, gc);
2098
	buf_puts (&out, "[AF_INET6]");
2099
	if (addr_is_defined)
2100
	{
2101
	  getnameinfo(&addr->addr.sa, sizeof (struct sockaddr_in6),
2102
	      buf, sizeof (buf), NULL, 0, NI_NUMERICHOST);
2103
	  buf_puts (&out, buf);
2104
	}
2105
	if (((flags & PS_SHOW_PORT) || (addr_is_defined && (flags & PS_SHOW_PORT_IF_DEFINED)))
2106
	    && port)
2107
	{
2108
	  if (separator)
2109
	    buf_puts (&out, separator);
2110
2111
	  buf_printf (&out, "%d", port);
2112
	}
2113
      }
2114
      break;
2115
#endif
2116
  }
2117
   return BSTR (&out);
1893
}
2118
}
1894
2119
1895
const char *
2120
const char *
Lines 1909-1920 print_link_socket_actual_ex (const struc Link Here
1909
      struct buffer out = alloc_buf_gc (128, gc);
2134
      struct buffer out = alloc_buf_gc (128, gc);
1910
      buf_printf (&out, "%s", print_sockaddr_ex (&act->dest, separator, flags, gc));
2135
      buf_printf (&out, "%s", print_sockaddr_ex (&act->dest, separator, flags, gc));
1911
#if ENABLE_IP_PKTINFO
2136
#if ENABLE_IP_PKTINFO
1912
      if ((flags & PS_SHOW_PKTINFO) && act->pi.ipi_spec_dst.s_addr)
2137
      if ((flags & PS_SHOW_PKTINFO) && addr_defined_ipi(act))
1913
	{
2138
	{
2139
	  switch(act->dest.addr.sa.sa_family)
2140
	  {
2141
	case AF_INET:
2142
	  {
1914
	  struct openvpn_sockaddr sa;
2143
	  struct openvpn_sockaddr sa;
1915
	  CLEAR (sa);
2144
	  CLEAR (sa);
1916
	  sa.sa.sin_addr = act->pi.ipi_spec_dst;
2145
	  sa.addr.in4.sin_addr = act->pi.in4.ipi_spec_dst;
1917
	  buf_printf (&out, " (via %s)", print_sockaddr_ex (&sa, separator, 0, gc));
2146
	  buf_printf (&out, " (via %s)", print_sockaddr_ex (&sa, separator, 0, gc));
2147
	  }
2148
	  break;
2149
#ifdef USE_PF_INET6
2150
	case AF_INET6:
2151
	  {
2152
	    struct sockaddr_in6 sin6;
2153
	    char buf[INET6_ADDRSTRLEN] = "[undef]";
2154
	    memset(&sin6, 0, sizeof sin6);
2155
	    sin6.sin6_family = AF_INET6;
2156
	    sin6.sin6_addr = act->pi.in6.ipi6_addr;
2157
	    {
2158
	      if (getnameinfo((struct sockaddr *)&sin6, sizeof (struct sockaddr_in6),
2159
		    buf, sizeof (buf), NULL, 0, NI_NUMERICHOST) == 0)
2160
		buf_printf (&out, " (via %s)", buf);
2161
	      else
2162
		buf_printf (&out, " (via [getnameinfo() err])");
2163
	    }
2164
	  }
2165
	  break;
2166
#endif
2167
	  }
2168
1918
	}
2169
	}
1919
#endif
2170
#endif
1920
      return BSTR (&out);
2171
      return BSTR (&out);
Lines 1950-1970 void Link Here
1950
setenv_sockaddr (struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const bool flags)
2201
setenv_sockaddr (struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const bool flags)
1951
{
2202
{
1952
  char name_buf[256];
2203
  char name_buf[256];
2204
  char buf[128];
1953
2205
1954
  if (flags & SA_IP_PORT)
2206
  switch(addr->addr.sa.sa_family) {
1955
    openvpn_snprintf (name_buf, sizeof (name_buf), "%s_ip", name_prefix);
2207
    case AF_INET:
1956
  else
2208
      if (flags & SA_IP_PORT)
1957
    openvpn_snprintf (name_buf, sizeof (name_buf), "%s", name_prefix);
2209
	openvpn_snprintf (name_buf, sizeof (name_buf), "%s_ip", name_prefix);
2210
      else
2211
	openvpn_snprintf (name_buf, sizeof (name_buf), "%s", name_prefix);
1958
2212
1959
  mutex_lock_static (L_INET_NTOA);
2213
      mutex_lock_static (L_INET_NTOA);
1960
  setenv_str (es, name_buf, inet_ntoa (addr->sa.sin_addr));
2214
      setenv_str (es, name_buf, inet_ntoa (addr->addr.in4.sin_addr));
1961
  mutex_unlock_static (L_INET_NTOA);
2215
      mutex_unlock_static (L_INET_NTOA);
2216
2217
      if ((flags & SA_IP_PORT) && addr->addr.in4.sin_port)
2218
      {
2219
	openvpn_snprintf (name_buf, sizeof (name_buf), "%s_port", name_prefix);
2220
	setenv_int (es, name_buf, ntohs (addr->addr.in4.sin_port));
2221
      }
2222
      break;
2223
#ifdef USE_PF_INET6
2224
    case AF_INET6:
2225
      openvpn_snprintf (name_buf, sizeof (name_buf), "%s_ip6", name_prefix);
2226
      getnameinfo(&addr->addr.sa, sizeof (struct sockaddr_in6),
2227
	  buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
2228
      setenv_str (es, name_buf, buf);
1962
2229
1963
  if ((flags & SA_IP_PORT) && addr->sa.sin_port)
1964
    {
1965
      openvpn_snprintf (name_buf, sizeof (name_buf), "%s_port", name_prefix);
2230
      openvpn_snprintf (name_buf, sizeof (name_buf), "%s_port", name_prefix);
1966
      setenv_int (es, name_buf, ntohs (addr->sa.sin_port));
2231
      setenv_int (es, name_buf, ntohs (addr->addr.in6.sin6_port));
1967
    }
2232
      break;
2233
#endif
2234
  }
1968
}
2235
}
1969
2236
1970
void
2237
void
Lines 1974-1980 setenv_in_addr_t (struct env_set *es, co Link Here
1974
    {
2241
    {
1975
      struct openvpn_sockaddr si;
2242
      struct openvpn_sockaddr si;
1976
      CLEAR (si);
2243
      CLEAR (si);
1977
      si.sa.sin_addr.s_addr = htonl (addr);
2244
      si.addr.in4.sin_family = AF_INET;
2245
      si.addr.in4.sin_addr.s_addr = htonl (addr);
1978
      setenv_sockaddr (es, name_prefix, &si, flags);
2246
      setenv_sockaddr (es, name_prefix, &si, flags);
1979
    }
2247
    }
1980
}
2248
}
Lines 1995-2010 setenv_link_socket_actual (struct env_se Link Here
1995
struct proto_names {
2263
struct proto_names {
1996
  const char *short_form;
2264
  const char *short_form;
1997
  const char *display_form;
2265
  const char *display_form;
2266
  bool	is_dgram;
2267
  bool	is_net;
2268
  sa_family_t proto_af;
1998
};
2269
};
1999
2270
2000
/* Indexed by PROTO_x */
2271
/* Indexed by PROTO_x */
2001
static const struct proto_names proto_names[] = {
2272
static const struct proto_names proto_names[PROTO_N] = {
2002
  {"udp",        "UDPv4"},
2273
  {"proto-uninitialized",        "proto-NONE",0,0, AF_UNSPEC},
2003
  {"tcp-server", "TCPv4_SERVER"},
2274
  {"udp",        "UDPv4",1,1, AF_INET},
2004
  {"tcp-client", "TCPv4_CLIENT"},
2275
  {"tcp-server", "TCPv4_SERVER",0,1, AF_INET},
2005
  {"tcp",        "TCPv4"}
2276
  {"tcp-client", "TCPv4_CLIENT",0,1, AF_INET},
2277
  {"tcp",        "TCPv4",0,1, AF_INET},
2278
#ifdef USE_PF_INET6
2279
  {"udp6"       ,"UDPv6",1,1, AF_INET6},
2280
  {"tcp6-server","TCPv6_SERVER",0,1, AF_INET6},
2281
  {"tcp6-client","TCPv6_CLIENT",0,1, AF_INET6},
2282
  {"tcp6"       ,"TCPv6",0,1, AF_INET6},
2283
#endif
2006
};
2284
};
2007
2285
2286
bool
2287
proto_is_net(int proto)
2288
{
2289
  if (proto < 0 || proto >= PROTO_N)
2290
    ASSERT(0);
2291
  return proto_names[proto].is_net;
2292
}
2293
bool
2294
proto_is_dgram(int proto)
2295
{
2296
  if (proto < 0 || proto >= PROTO_N)
2297
    ASSERT(0);
2298
  return proto_names[proto].is_dgram;
2299
}
2300
bool
2301
proto_is_udp(int proto)
2302
{
2303
  if (proto < 0 || proto >= PROTO_N)
2304
    ASSERT(0);
2305
  return proto_names[proto].is_dgram&&proto_names[proto].is_net;
2306
}
2307
bool
2308
proto_is_tcp(int proto)
2309
{
2310
  if (proto < 0 || proto >= PROTO_N)
2311
    ASSERT(0);
2312
  return (!proto_names[proto].is_dgram)&&proto_names[proto].is_net;
2313
}
2314
2315
sa_family_t 
2316
proto_sa_family(int proto)
2317
{
2318
  if (proto < 0 || proto >= PROTO_N)
2319
    ASSERT(0);
2320
  return proto_names[proto].proto_af;
2321
}
2322
2008
int
2323
int
2009
ascii2proto (const char* proto_name)
2324
ascii2proto (const char* proto_name)
2010
{
2325
{
Lines 2044-2049 proto2ascii_all (struct gc_arena *gc) Link Here
2044
  return BSTR (&out);
2359
  return BSTR (&out);
2045
}
2360
}
2046
2361
2362
int
2363
addr_guess_family(int proto, const char *name) 
2364
{
2365
  sa_family_t ret;
2366
  if (proto) {
2367
    return proto_sa_family(proto);	/* already stamped */
2368
  } 
2369
#ifdef USE_PF_UNIX
2370
  else if (name && name[0] == '/') {
2371
    return AF_UNIX;
2372
  }
2373
#endif
2374
#ifdef USE_PF_INET6
2375
  else {
2376
    struct addrinfo hints , *ai;
2377
    int err;
2378
    memset(&hints, 0, sizeof hints);
2379
    hints.ai_flags=AI_NUMERICHOST;
2380
    if ((err=getaddrinfo(name, NULL, &hints, &ai))==0) {
2381
      ret=ai->ai_family;
2382
      freeaddrinfo(ai);
2383
      return ret;
2384
    }
2385
  }
2386
#endif
2387
  return AF_INET;	/* default */
2388
}
2389
const char *
2390
addr_family_name (int af) 
2391
{
2392
  switch (af) {
2393
    case AF_INET: return "AF_INET";
2394
    case AF_INET6: return "AF_INET6";
2395
#ifdef USE_PF_UNIX
2396
    case AF_UNIX: return "AF_UNIX";
2397
#endif
2398
  }
2399
  return "AF_UNSPEC";
2400
}
2401
2047
/*
2402
/*
2048
 * Given a local proto, return local proto
2403
 * Given a local proto, return local proto
2049
 * if !remote, or compatible remote proto
2404
 * if !remote, or compatible remote proto
Lines 2058-2067 proto_remote (int proto, bool remote) Link Here
2058
  ASSERT (proto >= 0 && proto < PROTO_N);
2413
  ASSERT (proto >= 0 && proto < PROTO_N);
2059
  if (remote)
2414
  if (remote)
2060
    {
2415
    {
2061
      if (proto == PROTO_TCPv4_SERVER)
2416
      switch (proto)
2062
	return PROTO_TCPv4_CLIENT;
2417
      {
2063
      if (proto == PROTO_TCPv4_CLIENT)
2418
	case PROTO_TCPv4_SERVER: return PROTO_TCPv4_CLIENT;
2064
	return PROTO_TCPv4_SERVER;
2419
	case PROTO_TCPv4_CLIENT: return PROTO_TCPv4_SERVER;
2420
#ifdef USE_PF_INET6
2421
	case PROTO_TCPv6_SERVER: return PROTO_TCPv6_CLIENT;
2422
	case PROTO_TCPv6_CLIENT: return PROTO_TCPv6_SERVER;
2423
#endif
2424
      }
2065
    }
2425
    }
2066
  return proto;
2426
  return proto;
2067
}
2427
}
Lines 2119-2129 #ifndef WIN32 Link Here
2119
2479
2120
#if ENABLE_IP_PKTINFO
2480
#if ENABLE_IP_PKTINFO
2121
2481
2122
struct openvpn_pktinfo
2482
struct openvpn_in4_pktinfo
2123
{
2483
{
2124
  struct cmsghdr cmsghdr;
2484
  struct cmsghdr cmsghdr;
2125
  struct in_pktinfo in_pktinfo;
2485
  struct in_pktinfo pi;
2126
};
2486
};
2487
#ifdef USE_PF_INET6
2488
struct openvpn_in6_pktinfo
2489
{
2490
  struct cmsghdr cmsghdr;
2491
  struct in6_pktinfo pi6;
2492
};
2493
#endif
2494
2495
union openvpn_pktinfo {
2496
	struct openvpn_in4_pktinfo cmsgpi;
2497
#ifdef USE_PF_INET6
2498
	struct openvpn_in6_pktinfo cmsgpi6;
2499
#endif
2500
};
2501
2127
2502
2128
static socklen_t
2503
static socklen_t
2129
link_socket_read_udp_posix_recvmsg (struct link_socket *sock,
2504
link_socket_read_udp_posix_recvmsg (struct link_socket *sock,
Lines 2132-2146 link_socket_read_udp_posix_recvmsg (stru Link Here
2132
				    struct link_socket_actual *from)
2507
				    struct link_socket_actual *from)
2133
{
2508
{
2134
  struct iovec iov;
2509
  struct iovec iov;
2135
  struct openvpn_pktinfo opi;
2510
  union openvpn_pktinfo opi;
2136
  struct msghdr mesg;
2511
  struct msghdr mesg;
2137
  socklen_t fromlen = sizeof (from->dest.sa);
2512
  socklen_t fromlen = sizeof (from->dest.addr);
2138
2513
2139
  iov.iov_base = BPTR (buf);
2514
  iov.iov_base = BPTR (buf);
2140
  iov.iov_len = maxsize;
2515
  iov.iov_len = maxsize;
2141
  mesg.msg_iov = &iov;
2516
  mesg.msg_iov = &iov;
2142
  mesg.msg_iovlen = 1;
2517
  mesg.msg_iovlen = 1;
2143
  mesg.msg_name = &from->dest.sa;
2518
  mesg.msg_name = &from->dest.addr;
2144
  mesg.msg_namelen = fromlen;
2519
  mesg.msg_namelen = fromlen;
2145
  mesg.msg_control = &opi;
2520
  mesg.msg_control = &opi;
2146
  mesg.msg_controllen = sizeof (opi);
2521
  mesg.msg_controllen = sizeof (opi);
Lines 2157-2165 link_socket_read_udp_posix_recvmsg (stru Link Here
2157
	  && cmsg->cmsg_len >= sizeof (opi))
2532
	  && cmsg->cmsg_len >= sizeof (opi))
2158
	{
2533
	{
2159
	  struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2534
	  struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2160
	  from->pi.ipi_ifindex = pkti->ipi_ifindex;
2535
	  from->pi.in4.ipi_ifindex = pkti->ipi_ifindex;
2161
	  from->pi.ipi_spec_dst = pkti->ipi_spec_dst;
2536
	  from->pi.in4.ipi_spec_dst = pkti->ipi_spec_dst;
2537
	}
2538
#ifdef USE_PF_INET6
2539
      else if (cmsg != NULL
2540
	  && CMSG_NXTHDR (&mesg, cmsg) == NULL
2541
	  && cmsg->cmsg_level == IPPROTO_IPV6 
2542
	  && cmsg->cmsg_type == IPV6_PKTINFO
2543
	  && cmsg->cmsg_len >= sizeof (struct openvpn_in6_pktinfo))
2544
	{
2545
	  struct in6_pktinfo *pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
2546
	  from->pi.in6.ipi6_ifindex = pkti6->ipi6_ifindex;
2547
	  from->pi.in6.ipi6_addr = pkti6->ipi6_addr;
2162
	}
2548
	}
2549
#endif
2163
    }
2550
    }
2164
  return fromlen;
2551
  return fromlen;
2165
}
2552
}
Lines 2171-2188 link_socket_read_udp_posix (struct link_ Link Here
2171
			    int maxsize,
2558
			    int maxsize,
2172
			    struct link_socket_actual *from)
2559
			    struct link_socket_actual *from)
2173
{
2560
{
2174
  socklen_t fromlen = sizeof (from->dest.sa);
2561
  socklen_t fromlen = sizeof (from->dest.addr);
2175
  from->dest.sa.sin_addr.s_addr = 0;
2562
  socklen_t expectedlen = af_addr_size(proto_sa_family(sock->info.proto));
2563
  addr_zero_host(&from->dest);
2176
  ASSERT (buf_safe (buf, maxsize));
2564
  ASSERT (buf_safe (buf, maxsize));
2177
#if ENABLE_IP_PKTINFO
2565
#if ENABLE_IP_PKTINFO
2178
  if (sock->sockflags & SF_USE_IP_PKTINFO)
2566
  /* Both PROTO_UDPv4 and PROTO_UDPv6 */
2567
  if (proto_is_udp(sock->info.proto) && sock->sockflags & SF_USE_IP_PKTINFO)
2179
    fromlen = link_socket_read_udp_posix_recvmsg (sock, buf, maxsize, from);
2568
    fromlen = link_socket_read_udp_posix_recvmsg (sock, buf, maxsize, from);
2180
  else
2569
  else
2181
#endif
2570
#endif
2182
    buf->len = recvfrom (sock->sd, BPTR (buf), maxsize, 0,
2571
    buf->len = recvfrom (sock->sd, BPTR (buf), maxsize, 0,
2183
			 (struct sockaddr *) &from->dest.sa, &fromlen);
2572
			 &from->dest.addr.sa, &fromlen);
2184
  if (fromlen != sizeof (from->dest.sa))
2573
  if (buf->len >= 0 && expectedlen && fromlen != expectedlen)
2185
    bad_address_length (fromlen, sizeof (from->dest.sa));
2574
    bad_address_length (fromlen, expectedlen);
2186
  return buf->len;
2575
  return buf->len;
2187
}
2576
}
2188
2577
Lines 2219-2244 link_socket_write_udp_posix_sendmsg (str Link Here
2219
  struct iovec iov;
2608
  struct iovec iov;
2220
  struct msghdr mesg;
2609
  struct msghdr mesg;
2221
  struct cmsghdr *cmsg;
2610
  struct cmsghdr *cmsg;
2222
  struct in_pktinfo *pkti;
2223
  struct openvpn_pktinfo opi;
2224
2611
2225
  iov.iov_base = BPTR (buf);
2612
  iov.iov_base = BPTR (buf);
2226
  iov.iov_len = BLEN (buf);
2613
  iov.iov_len = BLEN (buf);
2227
  mesg.msg_iov = &iov;
2614
  mesg.msg_iov = &iov;
2228
  mesg.msg_iovlen = 1;
2615
  mesg.msg_iovlen = 1;
2229
  mesg.msg_name = &to->dest.sa;
2616
  switch (sock->info.lsa->remote.addr.sa.sa_family)
2230
  mesg.msg_namelen = sizeof (to->dest.sa);
2617
  {
2231
  mesg.msg_control = &opi;
2618
    case AF_INET: {
2232
  mesg.msg_controllen = sizeof (opi);
2619
	  struct openvpn_in4_pktinfo opi;
2233
  mesg.msg_flags = 0;
2620
	  struct in_pktinfo *pkti;
2234
  cmsg = CMSG_FIRSTHDR (&mesg);
2621
	  mesg.msg_name = &to->dest.addr.sa;
2235
  cmsg->cmsg_len = sizeof (opi);
2622
	  mesg.msg_namelen = sizeof (struct sockaddr_in);
2236
  cmsg->cmsg_level = SOL_IP;
2623
	  mesg.msg_control = &opi;
2237
  cmsg->cmsg_type = IP_PKTINFO;
2624
	  mesg.msg_controllen = sizeof (opi);
2238
  pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2625
	  mesg.msg_flags = 0;
2239
  pkti->ipi_ifindex = to->pi.ipi_ifindex;
2626
	  cmsg = CMSG_FIRSTHDR (&mesg);
2240
  pkti->ipi_spec_dst = to->pi.ipi_spec_dst;
2627
	  cmsg->cmsg_len = sizeof (opi);
2241
  pkti->ipi_addr.s_addr = 0;
2628
	  cmsg->cmsg_level = SOL_IP;
2629
	  cmsg->cmsg_type = IP_PKTINFO;
2630
	  pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2631
	  pkti->ipi_ifindex = to->pi.in4.ipi_ifindex;
2632
	  pkti->ipi_spec_dst = to->pi.in4.ipi_spec_dst;
2633
	  pkti->ipi_addr.s_addr = 0;
2634
	  break;
2635
    }
2636
#ifdef USE_PF_INET6
2637
    case AF_INET6: {
2638
	  struct openvpn_in6_pktinfo opi6;
2639
	  struct in6_pktinfo *pkti6;
2640
	  mesg.msg_name = &to->dest.addr.sa;
2641
	  mesg.msg_namelen = sizeof (struct sockaddr_in6);
2642
	  mesg.msg_control = &opi6;
2643
	  mesg.msg_controllen = sizeof (opi6);
2644
	  mesg.msg_flags = 0;
2645
	  cmsg = CMSG_FIRSTHDR (&mesg);
2646
	  cmsg->cmsg_len = sizeof (opi6);
2647
	  cmsg->cmsg_level = IPPROTO_IPV6;
2648
	  cmsg->cmsg_type = IPV6_PKTINFO;
2649
	  pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
2650
	  pkti6->ipi6_ifindex = to->pi.in6.ipi6_ifindex;
2651
	  pkti6->ipi6_addr = to->pi.in6.ipi6_addr;
2652
	  break;
2653
    }
2654
#endif
2655
    default: ASSERT(0);
2656
  }
2242
  return sendmsg (sock->sd, &mesg, 0);
2657
  return sendmsg (sock->sd, &mesg, 0);
2243
}
2658
}
2244
2659
Lines 2382-2388 socket_send_queue (struct link_socket *s Link Here
2382
	{
2797
	{
2383
	  /* set destination address for UDP writes */
2798
	  /* set destination address for UDP writes */
2384
	  sock->writes.addr_defined = true;
2799
	  sock->writes.addr_defined = true;
2385
	  sock->writes.addr = to->dest.sa;
2800
	  sock->writes.addr = to->dest.addr.in4;
2386
	  sock->writes.addrlen = sizeof (sock->writes.addr);
2801
	  sock->writes.addrlen = sizeof (sock->writes.addr);
2387
2802
2388
	  status = WSASendTo(
2803
	  status = WSASendTo(
Lines 2538-2547 socket_finalize (SOCKET s, Link Here
2538
	{
2953
	{
2539
	  if (io->addrlen != sizeof (io->addr))
2954
	  if (io->addrlen != sizeof (io->addr))
2540
	    bad_address_length (io->addrlen, sizeof (io->addr));
2955
	    bad_address_length (io->addrlen, sizeof (io->addr));
2541
	  from->dest.sa = io->addr;
2956
	  from->dest.addr.sa = io->addr;
2542
	}
2957
	}
2543
      else
2958
      else
2544
	CLEAR (from->dest.sa);
2959
	CLEAR (from->dest.addr.sa);
2545
    }
2960
    }
2546
  
2961
  
2547
  if (buf)
2962
  if (buf)
(-)a/openvpn/socket.h (-31 / +202 lines)
Lines 81-87 #define ntohps(x) ntohs(x) Link Here
81
struct openvpn_sockaddr
81
struct openvpn_sockaddr
82
{
82
{
83
  /*int dummy;*/ /* add offset to force a bug if sa not explicitly dereferenced */
83
  /*int dummy;*/ /* add offset to force a bug if sa not explicitly dereferenced */
84
  struct sockaddr_in sa;
84
  union {
85
  struct sockaddr sa;
86
  struct sockaddr_in in4;
87
#ifdef USE_PF_INET6
88
  struct sockaddr_in6 in6;
89
#endif
90
  } addr;
85
};
91
};
86
92
87
/* actual address of remote, based on source address of received packets */
93
/* actual address of remote, based on source address of received packets */
Lines 90-96 struct link_socket_actual Link Here
90
  int dummy; /* JYFIXME -- add offset to force a bug if dest not explicitly dereferenced */
96
  int dummy; /* JYFIXME -- add offset to force a bug if dest not explicitly dereferenced */
91
  struct openvpn_sockaddr dest;
97
  struct openvpn_sockaddr dest;
92
#if ENABLE_IP_PKTINFO
98
#if ENABLE_IP_PKTINFO
93
  struct in_pktinfo pi;
99
  union {
100
    struct in_pktinfo in4;
101
#ifdef USE_PF_INET6
102
    struct in6_pktinfo in6;
103
#endif
104
  } pi;
94
#endif
105
#endif
95
};
106
};
96
107
Lines 411-416 socket_descriptor_t create_socket_tcp (v Link Here
411
socket_descriptor_t socket_do_accept (socket_descriptor_t sd,
422
socket_descriptor_t socket_do_accept (socket_descriptor_t sd,
412
				      struct link_socket_actual *act,
423
				      struct link_socket_actual *act,
413
				      const bool nowait);
424
				      const bool nowait);
425
/*
426
 * proto related
427
 */
428
bool proto_is_net(int proto);
429
bool proto_is_dgram(int proto);
430
bool proto_is_udp(int proto);
431
bool proto_is_tcp(int proto);
432
414
433
415
/*
434
/*
416
 * DNS resolution
435
 * DNS resolution
Lines 436-458 in_addr_t getaddr (unsigned int flags, Link Here
436
 * Transport protocol naming and other details.
455
 * Transport protocol naming and other details.
437
 */
456
 */
438
457
439
#define PROTO_UDPv4        0
458
#if 0 /* PRE UDPv6/TCPv6 code */
440
#define PROTO_TCPv4_SERVER 1
459
#define PROTO_NONE         0 /* catch for uninitialized */
441
#define PROTO_TCPv4_CLIENT 2
460
#define PROTO_UDPv4        1
442
#define PROTO_TCPv4        3
461
#define PROTO_TCPv4_SERVER 2
443
#define PROTO_N            4
462
#define PROTO_TCPv4_CLIENT 3
463
#define PROTO_TCPv4        4
464
#define PROTO_UDPv6        5
465
#define PROTO_TCPv6_SERVER 6
466
#define PROTO_TCPv6_CLIENT 7
467
#define PROTO_TCPv6        8
468
#define PROTO_N            9
469
#endif /* if 0 */
470
471
/* 
472
 * Use enum's instead of #define to allow for easier
473
 * optional proto support
474
 */
475
enum proto_num {
476
	PROTO_NONE, /* catch for uninitialized */
477
	PROTO_UDPv4,
478
	PROTO_TCPv4_SERVER,
479
	PROTO_TCPv4_CLIENT,
480
	PROTO_TCPv4,
481
	PROTO_UDPv6,
482
	PROTO_TCPv6_SERVER,
483
	PROTO_TCPv6_CLIENT,
484
	PROTO_TCPv6,
485
	PROTO_N
486
};
444
487
445
int ascii2proto (const char* proto_name);
488
int ascii2proto (const char* proto_name);
446
const char *proto2ascii (int proto, bool display_form);
489
const char *proto2ascii (int proto, bool display_form);
447
const char *proto2ascii_all (struct gc_arena *gc);
490
const char *proto2ascii_all (struct gc_arena *gc);
448
int proto_remote (int proto, bool remote);
491
int proto_remote (int proto, bool remote);
492
const char *addr_family_name(int af);
449
493
450
/*
494
/*
451
 * Overhead added to packets by various protocols.
495
 * Overhead added to packets by various protocols.
452
 */
496
 */
453
#define IPv4_UDP_HEADER_SIZE              28
497
#define IPv4_UDP_HEADER_SIZE              28
454
#define IPv4_TCP_HEADER_SIZE              40
498
#define IPv4_TCP_HEADER_SIZE              40
455
#define IPv6_UDP_HEADER_SIZE              40
499
#define IPv6_UDP_HEADER_SIZE              48
500
#define IPv6_TCP_HEADER_SIZE              60
456
501
457
extern const int proto_overhead[];
502
extern const int proto_overhead[];
458
503
Lines 485-491 legal_ipv4_port (int port) Link Here
485
static inline bool
530
static inline bool
486
link_socket_proto_connection_oriented (int proto)
531
link_socket_proto_connection_oriented (int proto)
487
{
532
{
488
  return proto == PROTO_TCPv4_SERVER || proto == PROTO_TCPv4_CLIENT;
533
  return !proto_is_dgram(proto);
489
}
534
}
490
535
491
static inline bool
536
static inline bool
Lines 500-506 link_socket_connection_oriented (const s Link Here
500
static inline bool
545
static inline bool
501
addr_defined (const struct openvpn_sockaddr *addr)
546
addr_defined (const struct openvpn_sockaddr *addr)
502
{
547
{
503
  return addr->sa.sin_addr.s_addr != 0;
548
  if (!addr) return 0;
549
  switch (addr->addr.sa.sa_family) {
550
    case AF_INET: return addr->addr.in4.sin_addr.s_addr != 0;
551
#ifdef USE_PF_INET6
552
    case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr);
553
#endif
554
    default: return 0;
555
  }
556
}
557
static inline bool
558
addr_defined_ipi (const struct link_socket_actual *lsa)
559
{
560
#if ENABLE_IP_PKTINFO
561
  if (!lsa) return 0;
562
  switch (lsa->dest.addr.sa.sa_family) {
563
    case AF_INET: return lsa->pi.in4.ipi_spec_dst.s_addr != 0;
564
#ifdef USE_PF_INET6
565
    case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
566
#endif
567
    default: return 0;
568
  }
569
#else
570
  ASSERT(0);
571
#endif
504
}
572
}
505
573
506
static inline bool
574
static inline bool
Lines 512-531 link_socket_actual_defined (const struct Link Here
512
static inline bool
580
static inline bool
513
addr_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
581
addr_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
514
{
582
{
515
  return a1->sa.sin_addr.s_addr == a2->sa.sin_addr.s_addr;
583
  switch(a1->addr.sa.sa_family) {
584
    case AF_INET:
585
      return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr;
586
#ifdef USE_PF_INET6
587
    case AF_INET6:
588
      return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr);
589
#endif
590
  }
591
  ASSERT(0);
592
  return false;
516
}
593
}
517
594
518
static inline in_addr_t
595
static inline in_addr_t
519
addr_host (const struct openvpn_sockaddr *s)
596
addr_host (const struct openvpn_sockaddr *addr)
520
{
597
{
521
  return ntohl (s->sa.sin_addr.s_addr);
598
  /* 
599
   * "public" addr returned is checked against ifconfig for
600
   * possible clash: non sense for now given
601
   * that we do ifconfig only IPv4
602
   */
603
#if defined(USE_PF_INET6) 
604
  if(addr->addr.sa.sa_family != AF_INET)
605
    return 0;
606
#else 
607
  ASSERT(addr->addr.sa.sa_family == AF_INET);
608
#endif
609
  return ntohl (addr->addr.in4.sin_addr.s_addr);
522
}
610
}
523
611
524
static inline bool
612
static inline bool
525
addr_port_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
613
addr_port_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
526
{
614
{
527
  return a1->sa.sin_addr.s_addr == a2->sa.sin_addr.s_addr
615
  switch(a1->addr.sa.sa_family) {
528
    && a1->sa.sin_port == a2->sa.sin_port;
616
    case AF_INET:
617
      return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr
618
	&& a1->addr.in4.sin_port == a2->addr.in4.sin_port;
619
#ifdef USE_PF_INET6
620
    case AF_INET6:
621
      return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr) 
622
	&& a1->addr.in6.sin6_port == a2->addr.in6.sin6_port;
623
#endif
624
  }
625
  ASSERT(0);
626
  return false;
529
}
627
}
530
628
531
static inline bool
629
static inline bool
Lines 538-543 addr_match_proto (const struct openvpn_s Link Here
538
    : addr_port_match (a1, a2);
636
    : addr_port_match (a1, a2);
539
}
637
}
540
638
639
static inline void
640
addr_zero_host(struct openvpn_sockaddr *addr)
641
{
642
   switch(addr->addr.sa.sa_family) {
643
     case AF_INET:
644
       addr->addr.in4.sin_addr.s_addr = 0;
645
       break;
646
#ifdef USE_PF_INET6
647
     case AF_INET6: 
648
       memset(&addr->addr.in6.sin6_addr, 0, sizeof (struct in6_addr));
649
       break;
650
#endif
651
   }
652
}
653
654
static inline void
655
addr_copy_sa(struct openvpn_sockaddr *dst, const struct openvpn_sockaddr *src)
656
{
657
  dst->addr = src->addr;
658
}
659
660
static inline void
661
addr_copy_host(struct openvpn_sockaddr *dst, const struct openvpn_sockaddr *src)
662
{
663
   switch(src->addr.sa.sa_family) {
664
     case AF_INET:
665
       dst->addr.in4.sin_addr.s_addr = src->addr.in4.sin_addr.s_addr;
666
       break;
667
#ifdef USE_PF_INET6
668
     case AF_INET6: 
669
       dst->addr.in6.sin6_addr = src->addr.in6.sin6_addr;
670
       break;
671
#endif
672
   }
673
}
674
675
static inline bool
676
addr_inet4or6(struct sockaddr *addr)
677
{
678
	return addr->sa_family == AF_INET || addr->sa_family == AF_INET6;
679
}
680
681
int addr_guess_family(int proto, const char *name);
682
static inline int
683
af_addr_size(sa_family_t af)
684
{
685
#if defined(USE_PF_INET6) || defined (USE_PF_UNIX)
686
   switch(af) {
687
     case AF_INET: return sizeof (struct sockaddr_in);
688
#ifdef USE_PF_UNIX
689
     case AF_UNIX: return sizeof (struct sockaddr_un);
690
#endif
691
#ifdef USE_PF_INET6
692
     case AF_INET6: return sizeof (struct sockaddr_in6);
693
#endif
694
     default: 
695
#if 0
696
      /* could be called from socket_do_accept() with empty addr */
697
      msg (M_ERR, "Bad address family: %d\n", addr->sa_family);
698
      ASSERT(0);
699
#endif
700
     	return 0;
701
   }
702
#else /* only AF_INET */
703
   return sizeof(struct sockaddr_in);
704
#endif
705
}
706
541
static inline bool
707
static inline bool
542
link_socket_actual_match (const struct link_socket_actual *a1, const struct link_socket_actual *a2)
708
link_socket_actual_match (const struct link_socket_actual *a1, const struct link_socket_actual *a2)
543
{
709
{
Lines 594-607 link_socket_verify_incoming_addr (struct Link Here
594
{
760
{
595
  if (buf->len > 0)
761
  if (buf->len > 0)
596
    {
762
    {
597
      if (from_addr->dest.sa.sin_family != AF_INET)
763
      switch (from_addr->dest.addr.sa.sa_family) {
598
	return false;
764
#ifdef USE_PF_INET6
599
      if (!link_socket_actual_defined (from_addr))
765
	case AF_INET6:
600
	return false;
766
#endif
601
      if (info->remote_float || !addr_defined (&info->lsa->remote))
767
	case AF_INET:
602
	return true;
768
	  if (!link_socket_actual_defined (from_addr))
603
      if (addr_match_proto (&from_addr->dest, &info->lsa->remote, info->proto))
769
	    return false;
604
	return true;
770
	  if (info->remote_float || !addr_defined (&info->lsa->remote))
771
	    return true;
772
	  if (addr_match_proto (&from_addr->dest, &info->lsa->remote, info->proto))
773
	    return true;
774
      }
605
    }
775
    }
606
  return false;
776
  return false;
607
}
777
}
Lines 707-713 link_socket_read (struct link_socket *so Link Here
707
		  int maxsize,
877
		  int maxsize,
708
		  struct link_socket_actual *from)
878
		  struct link_socket_actual *from)
709
{
879
{
710
  if (sock->info.proto == PROTO_UDPv4)
880
  if (proto_is_udp(sock->info.proto)) /* unified UDPv4 and UDPv6 */
711
    {
881
    {
712
      int res;
882
      int res;
713
883
Lines 718-727 #else Link Here
718
#endif
888
#endif
719
      return res;
889
      return res;
720
    }
890
    }
721
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
891
  else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
722
    {
892
    {
723
      /* from address was returned by accept */
893
      /* from address was returned by accept */
724
      from->dest.sa = sock->info.lsa->actual.dest.sa;
894
      addr_copy_sa(&from->dest, &sock->info.lsa->actual.dest);
725
      return link_socket_read_tcp (sock, buf);
895
      return link_socket_read_tcp (sock, buf);
726
    }
896
    }
727
  else
897
  else
Lines 776-788 #if ENABLE_IP_PKTINFO Link Here
776
					   struct buffer *buf,
946
					   struct buffer *buf,
777
					   struct link_socket_actual *to);
947
					   struct link_socket_actual *to);
778
948
779
  if (sock->sockflags & SF_USE_IP_PKTINFO)
949
  if (proto_is_udp(sock->info.proto) && (sock->sockflags & SF_USE_IP_PKTINFO)
950
	  && addr_defined_ipi(to))
780
    return link_socket_write_udp_posix_sendmsg (sock, buf, to);
951
    return link_socket_write_udp_posix_sendmsg (sock, buf, to);
781
  else
952
  else
782
#endif
953
#endif
783
    return sendto (sock->sd, BPTR (buf), BLEN (buf), 0,
954
    return sendto (sock->sd, BPTR (buf), BLEN (buf), 0,
784
		   (struct sockaddr *) &to->dest.sa,
955
		   (struct sockaddr *) &to->dest.addr.sa,
785
		   (socklen_t) sizeof (to->dest.sa));
956
		   (socklen_t) af_addr_size(to->dest.addr.sa.sa_family));
786
}
957
}
787
958
788
static inline int
959
static inline int
Lines 813-823 link_socket_write (struct link_socket *s Link Here
813
		   struct buffer *buf,
984
		   struct buffer *buf,
814
		   struct link_socket_actual *to)
985
		   struct link_socket_actual *to)
815
{
986
{
816
  if (sock->info.proto == PROTO_UDPv4)
987
  if (proto_is_udp(sock->info.proto)) /* unified UDPv4 and UDPv6 */
817
    {
988
    {
818
      return link_socket_write_udp (sock, buf, to);
989
      return link_socket_write_udp (sock, buf, to);
819
    }
990
    }
820
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
991
  else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
821
    {
992
    {
822
      return link_socket_write_tcp (sock, buf, to);
993
      return link_socket_write_tcp (sock, buf, to);
823
    }
994
    }
(-)a/openvpn/socks.c (-9 / +9 lines)
Lines 175-183 recv_socks_reply (socket_descriptor_t sd Link Here
175
175
176
  if (addr != NULL)
176
  if (addr != NULL)
177
    {
177
    {
178
      addr->sa.sin_family = AF_INET;
178
      addr->addr.in4.sin_family = AF_INET;
179
      addr->sa.sin_addr.s_addr = htonl (INADDR_ANY);
179
      addr->addr.in4.sin_addr.s_addr = htonl (INADDR_ANY);
180
      addr->sa.sin_port = htons (0);
180
      addr->addr.in4.sin_port = htons (0);
181
    }
181
    }
182
182
183
  while (len < 4 + alen + 2)
183
  while (len < 4 + alen + 2)
Lines 264-271 recv_socks_reply (socket_descriptor_t sd Link Here
264
  /* ATYP == 1 (IP V4 address) */
264
  /* ATYP == 1 (IP V4 address) */
265
  if (atyp == '\x01' && addr != NULL)
265
  if (atyp == '\x01' && addr != NULL)
266
    {
266
    {
267
      memcpy (&addr->sa.sin_addr, buf + 4, sizeof (addr->sa.sin_addr));
267
      memcpy (&addr->addr.in4.sin_addr, buf + 4, sizeof (addr->addr.in4.sin_addr));
268
      memcpy (&addr->sa.sin_port, buf + 8, sizeof (addr->sa.sin_port));
268
      memcpy (&addr->addr.in4.sin_port, buf + 8, sizeof (addr->addr.in4.sin_port));
269
    }
269
    }
270
270
271
271
Lines 383-390 socks_process_incoming_udp (struct buffe Link Here
383
  if (atyp != 1)		/* ATYP == 1 (IP V4) */
383
  if (atyp != 1)		/* ATYP == 1 (IP V4) */
384
    goto error;
384
    goto error;
385
385
386
  buf_read (buf, &from->dest.sa.sin_addr, sizeof (from->dest.sa.sin_addr));
386
  buf_read (buf, &from->dest.addr.in4.sin_addr, sizeof (from->dest.addr.in4.sin_addr));
387
  buf_read (buf, &from->dest.sa.sin_port, sizeof (from->dest.sa.sin_port));
387
  buf_read (buf, &from->dest.addr.in4.sin_port, sizeof (from->dest.addr.in4.sin_port));
388
388
389
  return;
389
  return;
390
390
Lines 416-423 socks_process_outgoing_udp (struct buffe Link Here
416
  buf_write_u16 (&head, 0);	/* RSV = 0 */
416
  buf_write_u16 (&head, 0);	/* RSV = 0 */
417
  buf_write_u8 (&head, 0);	/* FRAG = 0 */
417
  buf_write_u8 (&head, 0);	/* FRAG = 0 */
418
  buf_write_u8 (&head, '\x01'); /* ATYP = 1 (IP V4) */
418
  buf_write_u8 (&head, '\x01'); /* ATYP = 1 (IP V4) */
419
  buf_write (&head, &to->dest.sa.sin_addr, sizeof (to->dest.sa.sin_addr));
419
  buf_write (&head, &to->dest.addr.in4.sin_addr, sizeof (to->dest.addr.in4.sin_addr));
420
  buf_write (&head, &to->dest.sa.sin_port, sizeof (to->dest.sa.sin_port));
420
  buf_write (&head, &to->dest.addr.in4.sin_port, sizeof (to->dest.addr.in4.sin_port));
421
421
422
  return 10;
422
  return 10;
423
}
423
}

Return to bug 183457