Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nx-libs
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
dimbor
nx-libs
Commits
c6e95651
Commit
c6e95651
authored
Jun 15, 2016
by
Mike Gabriel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
nxcomp/Loop.cpp: Add Unix file socket support for proxy <-> proxy connection.
parent
b23dcd10
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
526 additions
and
141 deletions
+526
-141
ChannelEndPoint.cpp
nxcomp/ChannelEndPoint.cpp
+120
-23
ChannelEndPoint.h
nxcomp/ChannelEndPoint.h
+9
-2
Loop.cpp
nxcomp/Loop.cpp
+397
-116
No files found.
nxcomp/ChannelEndPoint.cpp
View file @
c6e95651
...
@@ -24,6 +24,7 @@
...
@@ -24,6 +24,7 @@
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <sys/stat.h>
#include "ChannelEndPoint.h"
#include "ChannelEndPoint.h"
...
@@ -31,8 +32,21 @@
...
@@ -31,8 +32,21 @@
ChannelEndPoint
::
ChannelEndPoint
(
const
char
*
spec
)
ChannelEndPoint
::
ChannelEndPoint
(
const
char
*
spec
)
:
defaultTCPPort_
(
0
),
defaultTCPInterface_
(
0
),
:
defaultTCPPort_
(
0
),
defaultTCPInterface_
(
0
),
defaultUnixPath_
(
NULL
)
{
defaultUnixPath_
(
NULL
),
spec_
(
NULL
)
{
spec_
=
(
spec
?
strdup
(
spec
)
:
NULL
);
setSpec
(
spec
);
}
ChannelEndPoint
::~
ChannelEndPoint
()
{
char
*
unixPath
=
NULL
;
if
(
getUnixPath
(
&
unixPath
))
{
struct
stat
st
;
lstat
(
unixPath
,
&
st
);
if
(
S_ISSOCK
(
st
.
st_mode
))
unlink
(
unixPath
);
}
}
}
void
void
...
@@ -40,21 +54,92 @@ ChannelEndPoint::setSpec(const char *spec) {
...
@@ -40,21 +54,92 @@ ChannelEndPoint::setSpec(const char *spec) {
if
(
spec_
)
free
(
spec_
);
if
(
spec_
)
free
(
spec_
);
if
(
spec
&&
strlen
(
spec
))
if
(
spec
&&
strlen
(
spec
))
{
spec_
=
strdup
(
spec
);
spec_
=
strdup
(
spec
);
isUnix_
=
getUnixPath
();
isTCP_
=
getTCPHostAndPort
();
}
else
else
{
spec_
=
NULL
;
spec_
=
NULL
;
isUnix_
=
false
;
isTCP_
=
false
;
}
}
}
void
void
ChannelEndPoint
::
setSpec
(
int
port
)
{
ChannelEndPoint
::
setSpec
(
long
port
)
{
if
(
port
>=
0
)
{
if
(
port
>=
0
)
{
char
tmp
[
20
];
char
tmp
[
20
];
sprintf
(
tmp
,
"%d"
,
port
);
sprintf
(
tmp
,
"%
l
d"
,
port
);
setSpec
(
tmp
);
setSpec
(
tmp
);
}
}
else
{
disable
();
}
}
void
ChannelEndPoint
::
setSpec
(
const
char
*
hostName
,
long
port
)
{
int
length
;
if
(
spec_
)
free
(
spec_
);
isUnix_
=
false
;
isTCP_
=
false
;
if
(
hostName
&&
strlen
(
hostName
)
&&
port
>=
1
)
{
length
=
snprintf
(
NULL
,
0
,
"tcp:%s:%ld"
,
hostName
,
port
);
spec_
=
(
char
*
)
calloc
(
length
+
1
,
sizeof
(
char
));
snprintf
(
spec_
,
length
+
1
,
"tcp:%s:%ld"
,
hostName
,
port
);
isTCP_
=
true
;
}
else
setSpec
((
char
*
)
NULL
);
else
setSpec
((
char
*
)
NULL
);
}
}
bool
ChannelEndPoint
::
getSpec
(
char
**
socketUri
)
const
{
if
(
socketUri
)
*
socketUri
=
NULL
;
char
*
unixPath
=
NULL
;
char
*
hostName
=
NULL
;
long
port
=
-
1
;
char
*
newSocketUri
=
NULL
;
int
length
=
-
1
;
if
(
getUnixPath
(
&
unixPath
))
{
length
=
snprintf
(
NULL
,
0
,
"unix:%s"
,
unixPath
);
}
else
if
(
getTCPHostAndPort
(
&
hostName
,
&
port
))
{
length
=
snprintf
(
NULL
,
0
,
"tcp:%s:%ld"
,
hostName
,
port
);
}
if
(
length
>
0
)
{
newSocketUri
=
(
char
*
)
calloc
(
length
+
1
,
sizeof
(
char
));
if
(
isUnixSocket
())
snprintf
(
newSocketUri
,
length
+
1
,
"unix:%s"
,
unixPath
);
else
snprintf
(
newSocketUri
,
length
+
1
,
"tcp:%s:%ld"
,
hostName
,
port
);
if
(
socketUri
)
*
socketUri
=
strdup
(
newSocketUri
);
}
free
(
newSocketUri
);
free
(
unixPath
);
free
(
hostName
);
if
(
*
socketUri
!=
'\0'
)
return
true
;
return
false
;
}
void
void
ChannelEndPoint
::
setDefaultTCPPort
(
long
port
)
{
ChannelEndPoint
::
setDefaultTCPPort
(
long
port
)
{
defaultTCPPort_
=
port
;
defaultTCPPort_
=
port
;
...
@@ -76,10 +161,12 @@ ChannelEndPoint::setDefaultUnixPath(char *path) {
...
@@ -76,10 +161,12 @@ ChannelEndPoint::setDefaultUnixPath(char *path) {
}
}
void
void
ChannelEndPoint
::
disable
()
{
setSpec
(
"0"
);
}
ChannelEndPoint
::
disable
()
{
setSpec
(
"0"
);
}
bool
bool
ChannelEndPoint
::
specIs
Port
(
long
*
port
)
const
{
ChannelEndPoint
::
get
Port
(
long
*
port
)
const
{
if
(
port
)
*
port
=
0
;
if
(
port
)
*
port
=
0
;
long
p
=
-
1
;
long
p
=
-
1
;
if
(
spec_
)
{
if
(
spec_
)
{
...
@@ -101,7 +188,7 @@ ChannelEndPoint::getUnixPath(char **unixPath) const {
...
@@ -101,7 +188,7 @@ ChannelEndPoint::getUnixPath(char **unixPath) const {
long
p
;
long
p
;
char
*
path
=
NULL
;
char
*
path
=
NULL
;
if
(
specIs
Port
(
&
p
))
{
if
(
get
Port
(
&
p
))
{
if
(
p
!=
1
)
return
false
;
if
(
p
!=
1
)
return
false
;
}
}
else
if
(
spec_
&&
(
strncmp
(
"unix:"
,
spec_
,
5
)
==
0
))
{
else
if
(
spec_
&&
(
strncmp
(
"unix:"
,
spec_
,
5
)
==
0
))
{
...
@@ -122,6 +209,11 @@ ChannelEndPoint::getUnixPath(char **unixPath) const {
...
@@ -122,6 +209,11 @@ ChannelEndPoint::getUnixPath(char **unixPath) const {
return
true
;
return
true
;
}
}
bool
ChannelEndPoint
::
isUnixSocket
()
const
{
return
isUnix_
;
}
// FIXME!!!
// FIXME!!!
static
const
char
*
static
const
char
*
getComputerName
()
{
getComputerName
()
{
...
@@ -158,7 +250,7 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const {
...
@@ -158,7 +250,7 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const {
if
(
host
)
*
host
=
NULL
;
if
(
host
)
*
host
=
NULL
;
if
(
port
)
*
port
=
0
;
if
(
port
)
*
port
=
0
;
if
(
specIs
Port
(
&
p
))
{
if
(
get
Port
(
&
p
))
{
h_len
=
0
;
h_len
=
0
;
}
}
else
if
(
spec_
&&
(
strncmp
(
"tcp:"
,
spec_
,
4
)
==
0
))
{
else
if
(
spec_
&&
(
strncmp
(
"tcp:"
,
spec_
,
4
)
==
0
))
{
...
@@ -194,8 +286,8 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const {
...
@@ -194,8 +286,8 @@ ChannelEndPoint::getTCPHostAndPort(char **host, long *port) const {
}
}
bool
bool
ChannelEndPoint
::
enabled
()
const
{
ChannelEndPoint
::
isTCPSocket
()
const
{
return
(
getUnixPath
()
||
getTCPHostAndPort
())
;
return
isTCP_
;
}
}
long
ChannelEndPoint
::
getTCPPort
()
const
{
long
ChannelEndPoint
::
getTCPPort
()
const
{
...
@@ -205,8 +297,15 @@ long ChannelEndPoint::getTCPPort() const {
...
@@ -205,8 +297,15 @@ long ChannelEndPoint::getTCPPort() const {
}
}
bool
bool
ChannelEndPoint
::
enabled
()
const
{
return
(
isUnixSocket
()
||
isTCPSocket
());
}
bool
ChannelEndPoint
::
validateSpec
()
{
ChannelEndPoint
::
validateSpec
()
{
return
(
specIsPort
()
||
getUnixPath
()
||
getTCPHostAndPort
());
isTCP_
=
getTCPHostAndPort
();
isUnix_
=
getUnixPath
();
return
(
getPort
()
||
isUnix_
||
isTCP_
);
}
}
ChannelEndPoint
&
ChannelEndPoint
::
operator
=
(
const
ChannelEndPoint
&
other
)
{
ChannelEndPoint
&
ChannelEndPoint
::
operator
=
(
const
ChannelEndPoint
&
other
)
{
...
@@ -219,26 +318,24 @@ ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) {
...
@@ -219,26 +318,24 @@ ChannelEndPoint &ChannelEndPoint::operator=(const ChannelEndPoint &other) {
old
=
spec_
;
old
=
spec_
;
spec_
=
(
other
.
spec_
?
strdup
(
other
.
spec_
)
:
NULL
);
spec_
=
(
other
.
spec_
?
strdup
(
other
.
spec_
)
:
NULL
);
free
(
old
);
free
(
old
);
isUnix_
=
getUnixPath
();
isTCP_
=
getTCPHostAndPort
();
return
*
this
;
return
*
this
;
}
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
ChannelEndPoint
&
endPoint
)
{
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
ChannelEndPoint
&
endPoint
)
{
if
(
endPoint
.
enabled
())
{
if
(
endPoint
.
enabled
())
{
char
*
unixPath
,
*
host
;
char
*
endPointSpec
=
NULL
;
long
port
;
if
(
endPoint
.
getSpec
(
&
endPointSpec
))
if
(
endPoint
.
getUnixPath
(
&
unixPath
))
{
{
os
<<
"unix:"
<<
unixPath
;
os
<<
endPointSpec
;
free
(
unixPath
);
free
(
endPointSpec
);
}
else
if
(
endPoint
.
getTCPHostAndPort
(
&
host
,
&
port
))
{
os
<<
"tcp:"
<<
host
<<
":"
<<
port
;
free
(
host
);
}
}
else
{
else
os
<<
"(invalid)"
;
os
<<
"(invalid)"
;
}
}
}
else
else
{
{
os
<<
"(disabled)"
;
os
<<
"(disabled)"
;
}
}
return
os
;
return
os
;
...
...
nxcomp/ChannelEndPoint.h
View file @
c6e95651
...
@@ -33,25 +33,32 @@ class ChannelEndPoint
...
@@ -33,25 +33,32 @@ class ChannelEndPoint
int
defaultTCPInterface_
;
// 0=localhost, otherwise IP of public interface.
int
defaultTCPInterface_
;
// 0=localhost, otherwise IP of public interface.
char
*
defaultUnixPath_
;
char
*
defaultUnixPath_
;
char
*
spec_
;
char
*
spec_
;
bool
isUnix_
;
bool
isTCP_
;
bool
specIs
Port
(
long
*
port
=
NULL
)
const
;
bool
get
Port
(
long
*
port
=
NULL
)
const
;
public
:
public
:
ChannelEndPoint
(
const
char
*
spec
=
NULL
);
ChannelEndPoint
(
const
char
*
spec
=
NULL
);
~
ChannelEndPoint
();
ChannelEndPoint
&
operator
=
(
const
ChannelEndPoint
&
other
);
ChannelEndPoint
&
operator
=
(
const
ChannelEndPoint
&
other
);
bool
enabled
()
const
;
bool
enabled
()
const
;
bool
disabled
()
{
return
!
enabled
();
}
bool
disabled
()
{
return
!
enabled
();
}
void
disable
();
void
disable
();
void
setSpec
(
const
char
*
spec
);
void
setSpec
(
const
char
*
spec
);
void
setSpec
(
int
port
);
void
setSpec
(
long
port
);
void
setSpec
(
const
char
*
hostName
,
long
port
);
bool
getSpec
(
char
**
socketUri
)
const
;
void
setDefaultTCPPort
(
long
port
);
void
setDefaultTCPPort
(
long
port
);
void
setDefaultTCPInterface
(
int
publicInterface
);
void
setDefaultTCPInterface
(
int
publicInterface
);
void
setDefaultUnixPath
(
char
*
path
);
void
setDefaultUnixPath
(
char
*
path
);
bool
getUnixPath
(
char
**
path
=
NULL
)
const
;
bool
getUnixPath
(
char
**
path
=
NULL
)
const
;
bool
isUnixSocket
()
const
;
bool
getTCPHostAndPort
(
char
**
hostname
=
NULL
,
long
*
port
=
NULL
)
const
;
bool
getTCPHostAndPort
(
char
**
hostname
=
NULL
,
long
*
port
=
NULL
)
const
;
long
getTCPPort
()
const
;
long
getTCPPort
()
const
;
bool
isTCPSocket
()
const
;
bool
validateSpec
();
bool
validateSpec
();
};
};
...
...
nxcomp/Loop.cpp
View file @
c6e95651
...
@@ -239,7 +239,7 @@ struct sockaddr_un
...
@@ -239,7 +239,7 @@ struct sockaddr_un
// should connect to remote.
// should connect to remote.
//
//
#define WE_INITIATE_CONNECTION (
*connectHost != '\0'
)
#define WE_INITIATE_CONNECTION (
connectSocket.enabled()
)
//
//
// Is true if we must provide our credentials
// Is true if we must provide our credentials
...
@@ -255,7 +255,7 @@ struct sockaddr_un
...
@@ -255,7 +255,7 @@ struct sockaddr_un
//
//
#define WE_LISTEN_FORWARDER (control -> ProxyMode == proxy_server && \
#define WE_LISTEN_FORWARDER (control -> ProxyMode == proxy_server && \
listen
Port != -1
)
listen
Socket.enabled()
)
//
//
// You must define FLUSH in Misc.h if
// You must define FLUSH in Misc.h if
...
@@ -440,7 +440,7 @@ static int SetupDisplaySocket(int &xServerAddrFamily, sockaddr *&xServerAddr,
...
@@ -440,7 +440,7 @@ static int SetupDisplaySocket(int &xServerAddrFamily, sockaddr *&xServerAddr,
static
int
ListenConnection
(
ChannelEndPoint
&
endPoint
,
const
char
*
label
);
static
int
ListenConnection
(
ChannelEndPoint
&
endPoint
,
const
char
*
label
);
static
int
ListenConnectionTCP
(
const
char
*
host
,
long
port
,
const
char
*
label
);
static
int
ListenConnectionTCP
(
const
char
*
host
,
long
port
,
const
char
*
label
);
static
int
ListenConnectionUnix
(
const
char
*
unixP
ath
,
const
char
*
label
);
static
int
ListenConnectionUnix
(
const
char
*
p
ath
,
const
char
*
label
);
static
int
ListenConnectionAny
(
sockaddr
*
addr
,
socklen_t
addrlen
,
const
char
*
label
);
static
int
ListenConnectionAny
(
sockaddr
*
addr
,
socklen_t
addrlen
,
const
char
*
label
);
static
int
AcceptConnection
(
int
fd
,
int
domain
,
const
char
*
label
);
static
int
AcceptConnection
(
int
fd
,
int
domain
,
const
char
*
label
);
...
@@ -448,8 +448,11 @@ static int AcceptConnection(int fd, int domain, const char *label);
...
@@ -448,8 +448,11 @@ static int AcceptConnection(int fd, int domain, const char *label);
// Other convenience functions.
// Other convenience functions.
//
//
static
int
WaitForRemote
(
int
portNum
);
static
int
PrepareProxyConnectionTCP
(
char
**
hostName
,
long
int
*
portNum
,
int
*
timeout
,
int
*
proxyFD
,
int
*
reason
);
static
int
ConnectToRemote
(
const
char
*
const
hostName
,
int
portNum
);
static
int
PrepareProxyConnectionUnix
(
char
**
path
,
int
*
timeout
,
int
*
proxyFD
,
int
*
reason
);
static
int
WaitForRemote
(
ChannelEndPoint
&
socketAddress
);
static
int
ConnectToRemote
(
ChannelEndPoint
&
socketAddress
);
static
int
SendProxyOptions
(
int
fd
);
static
int
SendProxyOptions
(
int
fd
);
static
int
SendProxyCaches
(
int
fd
);
static
int
SendProxyCaches
(
int
fd
);
...
@@ -514,7 +517,7 @@ static int ParsePackOption(const char *opt);
...
@@ -514,7 +517,7 @@ static int ParsePackOption(const char *opt);
// given on the command line.
// given on the command line.
//
//
static
int
ParseHostOption
(
const
char
*
opt
,
char
*
host
,
int
&
port
);
static
int
ParseHostOption
(
const
char
*
opt
,
char
*
host
,
long
&
port
);
//
//
// Translate a font server port specification
// Translate a font server port specification
...
@@ -944,7 +947,6 @@ static char unixSocketName[DEFAULT_STRING_LENGTH] = { 0 };
...
@@ -944,7 +947,6 @@ static char unixSocketName[DEFAULT_STRING_LENGTH] = { 0 };
// Other parameters.
// Other parameters.
//
//
static
char
connectHost
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
static
char
acceptHost
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
static
char
acceptHost
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
static
char
displayHost
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
static
char
displayHost
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
static
char
authCookie
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
static
char
authCookie
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
...
@@ -963,13 +965,19 @@ static sockaddr *xServerAddr = NULL;
...
@@ -963,13 +965,19 @@ static sockaddr *xServerAddr = NULL;
static
unsigned
int
xServerAddrLength
=
0
;
static
unsigned
int
xServerAddrLength
=
0
;
//
//
// The port where the local proxy will await
// The representation of a Unix socket path or
// the peer connection or where the remote
// a bind address, denoting where the local proxy
// proxy will be contacted.
// will await the peer connection.
//
static
ChannelEndPoint
listenSocket
;
//
// The TCP host and port or Unix file socket where
// the remote proxy will be contacted.
//
//
static
int
listenPort
=
-
1
;
static
ChannelEndPoint
connectSocket
;
static
int
connectPort
=
-
1
;
//
//
// Helper channels are disabled by default.
// Helper channels are disabled by default.
...
@@ -3355,46 +3363,75 @@ int InitBeforeNegotiation()
...
@@ -3355,46 +3363,75 @@ int InitBeforeNegotiation()
int
SetupProxyConnection
()
int
SetupProxyConnection
()
{
{
if
(
proxyFD
==
-
1
)
if
(
proxyFD
==
-
1
)
{
{
if
(
WE_INITIATE_CONNECTION
)
{
char
*
socketUri
=
NULL
;
if
(
connectPort
<
0
)
// Let's make sure, the default value for listenSocket is properly set. Doing this
// here, because we have to make sure that we call it after the connectSocket
// declaration is really really complete.
if
(
listenSocket
.
disabled
()
&&
connectSocket
.
disabled
())
{
{
connectPort
=
DEFAULT_NX_PROXY_PORT_OFFSET
+
proxyPort
;
char
listenPortValue
[
20
]
=
{
0
};
sprintf
(
listenPortValue
,
"%ld"
,
(
long
)(
proxyPort
+
DEFAULT_NX_PROXY_PORT_OFFSET
));
SetAndValidateChannelEndPointArg
(
"local"
,
"listen"
,
listenPortValue
,
listenSocket
);
}
}
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Going to connect to "
<<
connectHost
connectSocket
.
getSpec
(
&
socketUri
);
<<
":"
<<
connectPort
<<
".
\n
"
<<
logofs_flush
;
*
logofs
<<
"Loop: connectSocket is "
<<
(
connectSocket
.
enabled
()
?
"enabled"
:
"disabled"
)
<<
". "
<<
"The socket URI is '"
<<
(
socketUri
!=
NULL
?
socketUri
:
"<unset>"
)
<<
"'.
\n
"
<<
logofs_flush
;
listenSocket
.
getSpec
(
&
socketUri
);
*
logofs
<<
"Loop: listenSocket is "
<<
(
listenSocket
.
enabled
()
?
"enabled"
:
"disabled"
)
<<
". "
<<
"The socket URI is '"
<<
(
socketUri
!=
NULL
?
socketUri
:
"<unset>"
)
<<
"'.
\n
"
<<
logofs_flush
;
free
(
socketUri
);
socketUri
=
NULL
;
#endif
#endif
proxyFD
=
ConnectToRemote
(
connectHost
,
connectPort
);
if
(
WE_INITIATE_CONNECTION
)
{
if
(
connectSocket
.
getSpec
(
&
socketUri
))
{
#ifdef TEST
*
logofs
<<
"Loop: Going to connect to '"
<<
socketUri
<<
"'.
\n
"
<<
logofs_flush
;
#endif
free
(
socketUri
);
proxyFD
=
ConnectToRemote
(
connectSocket
);
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Connected to remote proxy on FD#"
*
logofs
<<
"Loop: Connected to remote proxy on FD#"
<<
proxyFD
<<
".
\n
"
<<
logofs_flush
;
<<
proxyFD
<<
".
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Info"
<<
": Connection to remote proxy '"
<<
connectHost
cerr
<<
"Info"
<<
": Connected to remote proxy on FD#"
<<
":"
<<
connectPort
<<
"' established.
\n
"
;
<<
proxyFD
<<
".
\n
"
;
}
}
}
else
else
{
{
if
(
listenPort
<
0
)
if
(
listenSocket
.
isTCPSocket
()
&&
(
listenSocket
.
getTCPPort
()
<
0
))
{
{
listen
Port
=
DEFAULT_NX_PROXY_PORT_OFFSET
+
proxyPort
;
listen
Socket
.
setSpec
(
DEFAULT_NX_PROXY_PORT_OFFSET
+
proxyPort
)
;
}
}
if
(
listenSocket
.
getSpec
(
&
socketUri
))
{
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Going to wait for connection on port "
*
logofs
<<
"Loop: Going to wait for connection at '"
<<
listenPort
<<
"
.
\n
"
<<
logofs_flush
;
<<
socketUri
<<
"'
.
\n
"
<<
logofs_flush
;
#endif
#endif
free
(
socketUri
);
proxyFD
=
WaitForRemote
(
listenPor
t
);
proxyFD
=
WaitForRemote
(
listenSocke
t
);
#ifdef TEST
#ifdef TEST
if
(
WE_LISTEN_FORWARDER
)
if
(
WE_LISTEN_FORWARDER
)
{
{
*
logofs
<<
"Loop: Connected to remote forwarder on FD#"
*
logofs
<<
"Loop: Connected to remote forwarder on FD#"
...
@@ -3405,8 +3442,9 @@ int SetupProxyConnection()
...
@@ -3405,8 +3442,9 @@ int SetupProxyConnection()
*
logofs
<<
"Loop: Connected to remote proxy on FD#"
*
logofs
<<
"Loop: Connected to remote proxy on FD#"
<<
proxyFD
<<
".
\n
"
<<
logofs_flush
;
<<
proxyFD
<<
".
\n
"
<<
logofs_flush
;
}
}
#endif
#endif
}
}
}
}
}
#ifdef TEST
#ifdef TEST
...
@@ -3422,6 +3460,9 @@ int SetupProxyConnection()
...
@@ -3422,6 +3460,9 @@ int SetupProxyConnection()
// to reduce startup time. Option will
// to reduce startup time. Option will
// later be disabled if needed.
// later be disabled if needed.
//
//
// either listenSocket or connectSocket is used here...
if
(
listenSocket
.
isTCPSocket
()
||
connectSocket
.
isTCPSocket
())
SetNoDelay
(
proxyFD
,
1
);
SetNoDelay
(
proxyFD
,
1
);
...
@@ -4404,6 +4445,8 @@ int ListenConnectionAny(sockaddr *addr, socklen_t addrlen, const char *label)
...
@@ -4404,6 +4445,8 @@ int ListenConnectionAny(sockaddr *addr, socklen_t addrlen, const char *label)
goto
SetupSocketError
;
goto
SetupSocketError
;
}
}
if
(
addr
->
sa_family
==
AF_INET
)
if
(
SetReuseAddress
(
newFD
)
<
0
)
if
(
SetReuseAddress
(
newFD
)
<
0
)
{
{
// SetReuseAddress already warns with an error
// SetReuseAddress already warns with an error
...
@@ -4427,12 +4470,12 @@ int ListenConnectionAny(sockaddr *addr, socklen_t addrlen, const char *label)
...
@@ -4427,12 +4470,12 @@ int ListenConnectionAny(sockaddr *addr, socklen_t addrlen, const char *label)
if
(
listen
(
newFD
,
8
)
==
-
1
)
if
(
listen
(
newFD
,
8
)
==
-
1
)
{
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Call to
bind
failed for "
<<
label
*
logofs
<<
"Loop: PANIC! Call to
listen
failed for "
<<
label
<<
". Error is "
<<
EGET
()
<<
". Error is "
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
<<
logofs_flush
;
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Error"
<<
": Call to
bind
failed for "
<<
label
cerr
<<
"Error"
<<
": Call to
listen
failed for "
<<
label
<<
". Error is "
<<
EGET
()
<<
". Error is "
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
;
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
;
...
@@ -5445,7 +5488,6 @@ void CleanupLocal()
...
@@ -5445,7 +5488,6 @@ void CleanupLocal()
*
unixSocketName
=
'\0'
;
*
unixSocketName
=
'\0'
;
*
connectHost
=
'\0'
;
*
acceptHost
=
'\0'
;
*
acceptHost
=
'\0'
;
*
displayHost
=
'\0'
;
*
displayHost
=
'\0'
;
*
authCookie
=
'\0'
;
*
authCookie
=
'\0'
;
...
@@ -5460,8 +5502,8 @@ void CleanupLocal()
...
@@ -5460,8 +5502,8 @@ void CleanupLocal()
xServerAddr
=
NULL
;
xServerAddr
=
NULL
;
listen
Port
=
-
1
;
listen
Socket
.
disable
()
;
connect
Port
=
-
1
;
connect
Socket
.
disable
()
;
cupsPort
.
disable
();
cupsPort
.
disable
();
auxPort
.
disable
();
auxPort
.
disable
();
...
@@ -6592,27 +6634,31 @@ void ResetTimer()
...
@@ -6592,27 +6634,31 @@ void ResetTimer()
}
}
//
//
// Open TCP
socket to listen for remote proxy and
// Open TCP
or UNIX file socket to listen for remote proxy
// block until remote connects. If successful close
//
and
block until remote connects. If successful close
// the listening socket and return FD on which the
// the listening socket and return FD on which the
other
//
other
party is connected.
// party is connected.
//
//
int
WaitForRemote
(
int
portNum
)
int
WaitForRemote
(
ChannelEndPoint
&
socketAddress
)
{
{
char
hostLabel
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
char
hostLabel
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
char
*
socketUri
=
NULL
;
int
retryAccept
=
-
1
;
int
retryAccept
=
-
1
;
int
proxyFD
=
-
1
;
int
proxyFD
=
-
1
;
int
newFD
=
-
1
;
int
newFD
=
-
1
;
int
acceptIPAddr
=
0
;
if
(
socketAddress
.
isTCPSocket
())
{
//
//
// Get IP address of host to be awaited.
// Get IP address of host to be awaited.
//
//
int
acceptIPAddr
=
0
;
if
(
*
acceptHost
!=
'\0'
)
if
(
*
acceptHost
!=
'\0'
)
{
{
acceptIPAddr
=
GetHostAddress
(
acceptHost
);
acceptIPAddr
=
GetHostAddress
(
acceptHost
);
...
@@ -6636,18 +6682,31 @@ int WaitForRemote(int portNum)
...
@@ -6636,18 +6682,31 @@ int WaitForRemote(int portNum)
snprintf
(
hostLabel
,
sizeof
(
hostLabel
),
"'%s'"
,
acceptHost
);
snprintf
(
hostLabel
,
sizeof
(
hostLabel
),
"'%s'"
,
acceptHost
);
}
}
proxyFD
=
ListenConnectionTCP
(((
loopbackBind
||
(
control
->
ProxyMode
==
proxy_server
))
?
"localhost"
:
"*"
),
if
(
loopbackBind
)
portNum
,
"NX"
);
{
long
bindPort
;
if
(
socketAddress
.
getTCPHostAndPort
(
NULL
,
&
bindPort
))
socketAddress
.
setSpec
(
"localhost"
,
bindPort
);
}
}
else
if
(
socketAddress
.
isUnixSocket
())
strcpy
(
hostLabel
,
"this host"
);
else
strcpy
(
hostLabel
,
"unknown origin (something went wrong!!!)"
);
proxyFD
=
ListenConnection
(
socketAddress
,
"NX"
);
socketAddress
.
getSpec
(
&
socketUri
);
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Waiting for connection from "
*
logofs
<<
"Loop: Waiting for connection from "
<<
hostLabel
<<
" on
port '"
<<
portNum
<<
hostLabel
<<
" on
socket '"
<<
socketUri
<<
"'.
\n
"
<<
logofs_flush
;
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Info"
<<
": Waiting for connection from "
cerr
<<
"Info"
<<
": Waiting for connection from "
<<
hostLabel
<<
" on
port '"
<<
portNum
<<
hostLabel
<<
" on
socket '"
<<
socketUri
<<
"'.
\n
"
;
<<
"'.
\n
"
;
free
(
socketUri
);
//
//
// How many times to loop waiting for connections
// How many times to loop waiting for connections
...
@@ -6702,12 +6761,19 @@ int WaitForRemote(int portNum)
...
@@ -6702,12 +6761,19 @@ int WaitForRemote(int portNum)
}
}
else
if
(
result
>
0
&&
FD_ISSET
(
proxyFD
,
&
readSet
))
else
if
(
result
>
0
&&
FD_ISSET
(
proxyFD
,
&
readSet
))
{
{
sockaddr_in
newAddr
;
socklen_t
addrLen
=
sizeof
(
sockaddr_in
);
newFD
=
accept
(
proxyFD
,
(
sockaddr
*
)
&
newAddr
,
&
addrLen
)
;
sockaddr_in
newAddrINET
;
if
(
socketAddress
.
isUnixSocket
())
{
socklen_t
addrLen
=
sizeof
(
sockaddr_un
);
newFD
=
accept
(
proxyFD
,
NULL
,
&
addrLen
);
}
else
if
(
socketAddress
.
isTCPSocket
())
{
socklen_t
addrLen
=
sizeof
(
sockaddr_in
);
newFD
=
accept
(
proxyFD
,
(
sockaddr
*
)
&
newAddrINET
,
&
addrLen
);
}
if
(
newFD
==
-
1
)
if
(
newFD
==
-
1
)
{
{
#ifdef PANIC
#ifdef PANIC
...
@@ -6722,13 +6788,34 @@ int WaitForRemote(int portNum)
...
@@ -6722,13 +6788,34 @@ int WaitForRemote(int portNum)
goto
WaitForRemoteError
;
goto
WaitForRemoteError
;
}
}
char
*
connectedHost
=
inet_ntoa
(
newAddr
.
sin_addr
);
if
(
socketAddress
.
isUnixSocket
())
{
char
*
unixPath
=
NULL
;
socketAddress
.
getUnixPath
(
&
unixPath
);
#ifdef TEST
*
logofs
<<
"Loop: Accepted connection from this host on Unix file socket '"
<<
unixPath
<<
"'.
\n
"
<<
logofs_flush
;
#endif
cerr
<<
"Info"
<<
": Accepted connection from this host on Unix file socket '"
<<
unixPath
<<
"'.
\n
"
;
free
(
unixPath
);
break
;
}
else
if
(
socketAddress
.
isTCPSocket
())
{
char
*
connectedHost
=
inet_ntoa
(
newAddrINET
.
sin_addr
);
if
(
*
acceptHost
==
'\0'
||
(
int
)
newAddr
.
sin_addr
.
s_addr
==
acceptIPAddr
)
if
(
*
acceptHost
==
'\0'
||
(
int
)
newAddrINET
.
sin_addr
.
s_addr
==
acceptIPAddr
)
{
{
#ifdef TEST
#ifdef TEST
unsigned
int
connectedPort
=
ntohs
(
newAddr
.
sin_port
);
unsigned
int
connectedPort
=
ntohs
(
newAddrINET
.
sin_port
);
*
logofs
<<
"Loop: Accepted connection from '"
<<
connectedHost
*
logofs
<<
"Loop: Accepted connection from '"
<<
connectedHost
<<
"' with port '"
<<
connectedPort
<<
"'.
\n
"
<<
"' with port '"
<<
connectedPort
<<
"'.
\n
"
...
@@ -6744,7 +6831,7 @@ int WaitForRemote(int portNum)
...
@@ -6744,7 +6831,7 @@ int WaitForRemote(int portNum)
{
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: WARNING! Refusing connection from '"
<<
connectedHost
*
logofs
<<
"Loop: WARNING! Refusing connection from '"
<<
connectedHost
<<
"' on port '"
<<
portNum
<<
"'.
\n
"
<<
logofs_flush
;
<<
"' on port '"
<<
socketAddress
.
getTCPPort
()
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Warning"
<<
": Refusing connection from '"
cerr
<<
"Warning"
<<
": Refusing connection from '"
...
@@ -6758,11 +6845,25 @@ int WaitForRemote(int portNum)
...
@@ -6758,11 +6845,25 @@ int WaitForRemote(int portNum)
sleep
(
5
);
sleep
(
5
);
close
(
newFD
);
close
(
newFD
);
}
}
}
if
(
--
retryAccept
==
0
)
if
(
--
retryAccept
==
0
)
{
{
if
(
*
acceptHost
==
'\0'
)
if
(
socketAddress
.
isUnixSocket
())
{
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Connection via Unix file socket from this host "
<<
"could not be established.
\n
"
<<
logofs_flush
;
#endif
cerr
<<
"Error"
<<
": Connection via Unix file socket from this host "
<<
"could not be established.
\n
"
;
}
else
if
(
*
acceptHost
==
'\0'
)
{
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Connection with remote host "
*
logofs
<<
"Loop: PANIC! Connection with remote host "
...
@@ -6804,87 +6905,162 @@ WaitForRemoteError:
...
@@ -6804,87 +6905,162 @@ WaitForRemoteError:
HandleCleanup
();
HandleCleanup
();
}
}
//
int
PrepareProxyConnectionTCP
(
char
**
hostName
,
long
int
*
portNum
,
int
*
timeout
,
int
*
proxyFD
,
int
*
reason
)
// Connect to remote proxy. If successful
// return FD of connection, else return -1.
//
int
ConnectToRemote
(
const
char
*
const
hostName
,
int
portNum
)
{
{
int
proxyFD
=
-
1
;
int
remoteIPAddr
=
GetHostAddress
(
hostName
);
if
(
!
proxyFD
)
{
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Implementation error (PrepareProxyConnectionTCP). "
<<
"'proxyFD' must not be a NULL pointer.
\n
"
<<
logofs_flush
;
#endif
cerr
<<
"Error"
<<
": Implementation error (PrepareProxyConnectionTCP). "
<<
"'proxyFD' must not be a NULL pointer.
\n
"
;
return
-
1
;
}
if
(
!
reason
)
{
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Implementation error (PrepareProxyConnectionTCP). "
<<
"'reason' must not be a NULL pointer.
\n
"
<<
logofs_flush
;
#endif
cerr
<<
"Error"
<<
": Implementation error (PrepareProxyConnectionTCP). "
<<
"'reason' must not be a NULL pointer.
\n
"
;
return
-
1
;
}
int
remoteIPAddr
=
GetHostAddress
(
*
hostName
);
if
(
remoteIPAddr
==
0
)
if
(
remoteIPAddr
==
0
)
{
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Unknown remote host '"
*
logofs
<<
"Loop: PANIC! Unknown remote host '"
<<
hostName
<<
"'.
\n
"
<<
logofs_flush
;
<<
*
hostName
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Error"
<<
": Unknown remote host '"
cerr
<<
"Error"
<<
": Unknown remote host '"
<<
hostName
<<
"'.
\n
"
;
<<
*
hostName
<<
"'.
\n
"
;
HandleCleanup
();
HandleCleanup
();
}
}
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Connecting to remote host '"
*
logofs
<<
"Loop: Connecting to remote host '"
<<
hostName
<<
":"
<<
portNum
<<
"'.
\n
"
<<
*
hostName
<<
":"
<<
*
portNum
<<
"'.
\n
"
<<
logofs_flush
;
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Info"
<<
": Connecting to remote host '"
cerr
<<
"Info"
<<
": Connecting to remote host '"
<<
hostName
<<
":"
<<
portNum
<<
"'.
\n
"
<<
*
hostName
<<
":"
<<
*
portNum
<<
"'.
\n
"
<<
logofs_flush
;
<<
logofs_flush
;
//
*
proxyFD
=
-
1
;
// How many times we retry to connect to remote
*
reason
=
-
1
;
// host in case of failure?
//
int
retryConnect
=
control
->
OptionProxyRetryConnect
;
sockaddr_in
addr
;
addr
.
sin_family
=
AF_INET
;
addr
.
sin_port
=
htons
(
*
portNum
);
addr
.
sin_addr
.
s_addr
=
remoteIPAddr
;
*
proxyFD
=
socket
(
AF_INET
,
SOCK_STREAM
,
PF_UNSPEC
);
*
reason
=
EGET
();
if
(
*
proxyFD
==
-
1
)
{
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Call to socket failed. "
<<
"Error is "
<<
*
reason
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
<<
logofs_flush
;
#endif
cerr
<<
"Error"
<<
": Call to socket failed. "
<<
"Error is "
<<
*
reason
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
;
return
-
1
;
}
else
if
(
SetReuseAddress
(
*
proxyFD
)
<
0
)
{
return
-
1
;
}
//
//
// Show an alert after 20 seconds and use the
// Ensure operation is timed out
// same timeout to interrupt the connect. The
// if there is a network problem.
// retry timeout is incremental, starting from
// 100 miliseconds up to 1 second.
//
//
int
alertTimeout
=
20000
;
if
(
timeout
)
int
connectTimeout
=
20000
;
SetTimer
(
*
timeout
);
int
retryTimeout
=
100
;
else
SetTimer
(
20000
);
T_timestamp
lastRetry
=
getNewTimestamp
(
);
int
result
=
connect
(
*
proxyFD
,
(
sockaddr
*
)
&
addr
,
sizeof
(
sockaddr_in
)
);
sockaddr_in
addr
;
*
reason
=
EGET
()
;
addr
.
sin_family
=
AF_INET
;
ResetTimer
();
addr
.
sin_port
=
htons
(
portNum
);
addr
.
sin_addr
.
s_addr
=
remoteIPAddr
;
for
(;;)
return
result
;
}
int
PrepareProxyConnectionUnix
(
char
**
path
,
int
*
timeout
,
int
*
proxyFD
,
int
*
reason
)
{
if
(
!
proxyFD
)
{
{
proxyFD
=
socket
(
AF_INET
,
SOCK_STREAM
,
PF_UNSPEC
);
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Implementation error (PrepareProxyConnectionUnix). "
<<
"proxyFD must not be a NULL pointer.
\n
"
<<
logofs_flush
;
#endif
if
(
proxyFD
==
-
1
)
cerr
<<
"Error"
<<
": Implementation error (PrepareProxyConnectionUnix). "
<<
"proxyFD must not be a NULL pointer.
\n
"
;
return
-
1
;
}
if
(
!
reason
)
{
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Implementation error (PrepareProxyConnectionUnix). "
<<
"'reason' must not be a NULL pointer.
\n
"
<<
logofs_flush
;
#endif
cerr
<<
"Error"
<<
": Implementation error (PrepareProxyConnectionUnix). "
<<
"'reason' must not be a NULL pointer.
\n
"
;
return
-
1
;
}
/* FIXME: Add socket file existence and permission checks */
*
proxyFD
=
-
1
;
*
reason
=
-
1
;
sockaddr_un
addr
;
addr
.
sun_family
=
AF_UNIX
;
strncpy
(
addr
.
sun_path
,
*
path
,
108
-
1
);
*
proxyFD
=
socket
(
AF_UNIX
,
SOCK_STREAM
,
PF_UNSPEC
);
*
reason
=
EGET
();
if
(
*
proxyFD
==
-
1
)
{
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Call to socket failed. "
*
logofs
<<
"Loop: PANIC! Call to socket failed. "
<<
"Error is "
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"Error is "
<<
*
reason
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
<<
logofs_flush
;
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Error"
<<
": Call to socket failed. "
cerr
<<
"Error"
<<
": Call to socket failed. "
<<
"Error is "
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"Error is "
<<
*
reason
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
;
<<
"'.
\n
"
;
goto
ConnectToRemoteError
;
return
-
1
;
}
else
if
(
SetReuseAddress
(
proxyFD
)
<
0
)
{
goto
ConnectToRemoteError
;
}
}
//
//
...
@@ -6892,6 +7068,59 @@ int ConnectToRemote(const char *const hostName, int portNum)
...
@@ -6892,6 +7068,59 @@ int ConnectToRemote(const char *const hostName, int portNum)
// if there is a network problem.
// if there is a network problem.
//
//
if
(
timeout
)
SetTimer
(
*
timeout
);
else
SetTimer
(
20000
);
int
result
=
connect
(
*
proxyFD
,
(
sockaddr
*
)
&
addr
,
sizeof
(
sockaddr_un
));
*
reason
=
EGET
();
ResetTimer
();
return
result
;
}
//
// Connect to remote proxy. If successful
// return FD of connection, else return -1.
//
int
ConnectToRemote
(
ChannelEndPoint
&
socketAddress
)
{
//
// How many times we retry to connect to remote
// host / Unix domain socket in case of failure?
//
int
retryConnect
=
control
->
OptionProxyRetryConnect
;
//
// Show an alert after 20 seconds and use the
// same timeout to interrupt the connect. The
// retry timeout is incremental, starting from
// 100 miliseconds up to 1 second.
//
int
alertTimeout
=
20000
;
int
connectTimeout
=
20000
;
int
retryTimeout
=
100
;
T_timestamp
lastRetry
=
getNewTimestamp
();
int
result
=
-
1
;
int
reason
=
-
1
;
int
proxyFD
=
-
1
;
char
*
hostName
=
NULL
;
long
int
portNum
=
-
1
;
char
*
unixPath
=
NULL
;
for
(;;)
{
#ifdef DEBUG
#ifdef DEBUG
*
logofs
<<
"Loop: Timer set to "
<<
connectTimeout
/
1000
*
logofs
<<
"Loop: Timer set to "
<<
connectTimeout
/
1000
<<
" S "
<<
"with retry set to "
<<
retryConnect
<<
" S "
<<
"with retry set to "
<<
retryConnect
...
@@ -6899,13 +7128,10 @@ int ConnectToRemote(const char *const hostName, int portNum)
...
@@ -6899,13 +7128,10 @@ int ConnectToRemote(const char *const hostName, int portNum)
<<
"'.
\n
"
<<
logofs_flush
;
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
SetTimer
(
connectTimeout
);
if
(
socketAddress
.
getUnixPath
(
&
unixPath
))
result
=
PrepareProxyConnectionUnix
(
&
unixPath
,
&
connectTimeout
,
&
proxyFD
,
&
reason
);
int
result
=
connect
(
proxyFD
,
(
sockaddr
*
)
&
addr
,
sizeof
(
sockaddr_in
));
else
if
(
socketAddress
.
getTCPHostAndPort
(
&
hostName
,
&
portNum
))
result
=
PrepareProxyConnectionTCP
(
&
hostName
,
&
portNum
,
&
connectTimeout
,
&
proxyFD
,
&
reason
);
int
reason
=
EGET
();
ResetTimer
();
if
(
result
<
0
)
if
(
result
<
0
)
{
{
...
@@ -6919,6 +7145,22 @@ int ConnectToRemote(const char *const hostName, int portNum)
...
@@ -6919,6 +7145,22 @@ int ConnectToRemote(const char *const hostName, int portNum)
{
{
ESET
(
reason
);
ESET
(
reason
);
if
(
socketAddress
.
isUnixSocket
())
{
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Connection to Unix file socket '"
<<
unixPath
<<
"' failed. Error is "
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
<<
logofs_flush
;
#endif
cerr
<<
"Error"
<<
": Connection to Unix file socket '"
<<
unixPath
<<
"' failed. Error is "
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
;
}
else
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Connection to '"
<<
hostName
*
logofs
<<
"Loop: PANIC! Connection to '"
<<
hostName
<<
":"
<<
portNum
<<
"' failed. Error is "
<<
":"
<<
portNum
<<
"' failed. Error is "
...
@@ -6929,14 +7171,14 @@ int ConnectToRemote(const char *const hostName, int portNum)
...
@@ -6929,14 +7171,14 @@ int ConnectToRemote(const char *const hostName, int portNum)
cerr
<<
"Error"
<<
": Connection to '"
<<
hostName
cerr
<<
"Error"
<<
": Connection to '"
<<
hostName
<<
":"
<<
portNum
<<
"' failed. Error is "
<<
":"
<<
portNum
<<
"' failed. Error is "
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
;
<<
EGET
()
<<
" '"
<<
ESTR
()
<<
"'.
\n
"
;
}
goto
ConnectToRemoteError
;
goto
ConnectToRemoteError
;
}
}
else
else
{
{
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Sleeping "
<<
retryTimeout
*
logofs
<<
"Loop: Sleeping "
<<
retryTimeout
<<
"
M
s before retrying.
\n
"
<<
"
m
s before retrying.
\n
"
<<
logofs_flush
;
<<
logofs_flush
;
#endif
#endif
...
@@ -7005,10 +7247,20 @@ int ConnectToRemote(const char *const hostName, int portNum)
...
@@ -7005,10 +7247,20 @@ int ConnectToRemote(const char *const hostName, int portNum)
ESET
(
reason
);
ESET
(
reason
);
#ifdef TEST
#ifdef TEST
if
(
unixPath
[
0
]
!=
'\0'
)
{
*
logofs
<<
"Loop: Connection to Unix socket file '"
<<
unixPath
<<
"' failed with error '"
<<
ESTR
()
<<
"'. Retrying.
\n
"
<<
logofs_flush
;
}
else
{
*
logofs
<<
"Loop: Connection to '"
<<
hostName
*
logofs
<<
"Loop: Connection to '"
<<
hostName
<<
":"
<<
portNum
<<
"' failed with error '"
<<
":"
<<
portNum
<<
"' failed with error '"
<<
ESTR
()
<<
"'. Retrying.
\n
"
<<
ESTR
()
<<
"'. Retrying.
\n
"
<<
logofs_flush
;
<<
logofs_flush
;
}
#endif
#endif
}
}
else
else
...
@@ -8234,6 +8486,9 @@ int ParseEnvironmentOptions(const char *env, int force)
...
@@ -8234,6 +8486,9 @@ int ParseEnvironmentOptions(const char *env, int force)
name
=
strtok
(
nextOpts
,
"="
);
name
=
strtok
(
nextOpts
,
"="
);
char
connectHost
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
long
connectPort
=
-
1
;
while
(
name
)
while
(
name
)
{
{
value
=
strtok
(
NULL
,
","
);
value
=
strtok
(
NULL
,
","
);
...
@@ -8254,6 +8509,7 @@ int ParseEnvironmentOptions(const char *env, int force)
...
@@ -8254,6 +8509,7 @@ int ParseEnvironmentOptions(const char *env, int force)
}
}
else
if
(
strcasecmp
(
name
,
"link"
)
==
0
)
else
if
(
strcasecmp
(
name
,
"link"
)
==
0
)
{
{
if
(
control
->
ProxyMode
==
proxy_server
)
if
(
control
->
ProxyMode
==
proxy_server
)
{
{
PrintOptionIgnored
(
"local"
,
name
,
value
);
PrintOptionIgnored
(
"local"
,
name
,
value
);
...
@@ -8315,26 +8571,29 @@ int ParseEnvironmentOptions(const char *env, int force)
...
@@ -8315,26 +8571,29 @@ int ParseEnvironmentOptions(const char *env, int force)
}
}
else
if
(
strcasecmp
(
name
,
"listen"
)
==
0
)
else
if
(
strcasecmp
(
name
,
"listen"
)
==
0
)
{
{
if
(
*
connectHost
!=
'\0'
)
char
*
socketUri
=
NULL
;
if
(
connectSocket
.
getSpec
(
&
socketUri
))
{
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Can't handle 'listen' and 'connect' parameters "
*
logofs
<<
"Loop: PANIC! Can't handle 'listen' and 'connect' parameters "
<<
"at the same time.
\n
"
<<
logofs_flush
;
<<
"at the same time.
\n
"
<<
logofs_flush
;
*
logofs
<<
"Loop: PANIC! Refusing 'listen' parameter with 'connect' being '"
*
logofs
<<
"Loop: PANIC! Refusing 'listen' parameter with 'connect' being '"
<<
connectHost
<<
"'.
\n
"
<<
logofs_flush
;
<<
socketUri
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Error"
<<
": Can't handle 'listen' and 'connect' parameters "
cerr
<<
"Error"
<<
": Can't handle 'listen' and 'connect' parameters "
<<
"at the same time.
\n
"
;
<<
"at the same time.
\n
"
;
cerr
<<
"Error"
<<
": Refusing 'listen' parameter with 'connect' being '"
cerr
<<
"Error"
<<
": Refusing 'listen' parameter with 'connect' being '"
<<
connectHost
<<
"'.
\n
"
;
<<
socketUri
<<
"'.
\n
"
;
free
(
socketUri
);
return
-
1
;
return
-
1
;
}
}
listenPort
=
ValidateArg
(
"local"
,
name
,
value
);
SetAndValidateChannelEndPointArg
(
"local"
,
name
,
value
,
listenSocket
);
}
}
else
if
(
strcasecmp
(
name
,
"loopback"
)
==
0
)
else
if
(
strcasecmp
(
name
,
"loopback"
)
==
0
)
{
{
...
@@ -8342,22 +8601,24 @@ int ParseEnvironmentOptions(const char *env, int force)
...
@@ -8342,22 +8601,24 @@ int ParseEnvironmentOptions(const char *env, int force)
}
}
else
if
(
strcasecmp
(
name
,
"accept"
)
==
0
)
else
if
(
strcasecmp
(
name
,
"accept"
)
==
0
)
{
{
if
(
*
connectHost
!=
'\0'
)
char
*
socketUri
=
NULL
;
if
(
connectSocket
.
getSpec
(
&
socketUri
))
{
{
#ifdef PANIC
#ifdef PANIC
*
logofs
<<
"Loop: PANIC! Can't handle 'accept' and 'connect' parameters "
*
logofs
<<
"Loop: PANIC! Can't handle 'accept' and 'connect' parameters "
<<
"at the same time.
\n
"
<<
logofs_flush
;
<<
"at the same time.
\n
"
<<
logofs_flush
;
*
logofs
<<
"Loop: PANIC! Refusing 'accept' parameter with 'connect' being '"
*
logofs
<<
"Loop: PANIC! Refusing 'accept' parameter with 'connect' being '"
<<
connectHost
<<
"'.
\n
"
<<
logofs_flush
;
<<
socketUri
<<
"'.
\n
"
<<
logofs_flush
;
#endif
#endif
cerr
<<
"Error"
<<
": Can't handle 'accept' and 'connect' parameters "
cerr
<<
"Error"
<<
": Can't handle 'accept' and 'connect' parameters "
<<
"at the same time.
\n
"
;
<<
"at the same time.
\n
"
;
cerr
<<
"Error"
<<
": Refusing 'accept' parameter with 'connect' being '"
cerr
<<
"Error"
<<
": Refusing 'accept' parameter with 'connect' being '"
<<
connectHost
<<
"'.
\n
"
;
<<
socketUri
<<
"'.
\n
"
;
free
(
socketUri
);
return
-
1
;
return
-
1
;
}
}
...
@@ -8383,8 +8644,12 @@ int ParseEnvironmentOptions(const char *env, int force)
...
@@ -8383,8 +8644,12 @@ int ParseEnvironmentOptions(const char *env, int force)
return
-
1
;
return
-
1
;
}
}
if
((
strncmp
(
value
,
"tcp:"
,
4
)
==
0
)
||
(
strncmp
(
value
,
"unix:"
,
5
)
==
0
))
strncpy
(
connectHost
,
value
,
DEFAULT_STRING_LENGTH
-
1
);
SetAndValidateChannelEndPointArg
(
"local"
,
name
,
value
,
connectSocket
);
else
// if the "connect" parameter does not start with "unix:" or "tcp:" assume
// old parameter usage style (providing hostname string only).
strcpy
(
connectHost
,
value
);
}
}
else
if
(
strcasecmp
(
name
,
"port"
)
==
0
)
else
if
(
strcasecmp
(
name
,
"port"
)
==
0
)
{
{
...
@@ -8842,6 +9107,17 @@ int ParseEnvironmentOptions(const char *env, int force)
...
@@ -8842,6 +9107,17 @@ int ParseEnvironmentOptions(const char *env, int force)
}
// End of while (name) ...
}
// End of while (name) ...
// Assemble the connectSocket channel end point if parameter values have been old-school...
if
(
connectSocket
.
disabled
()
&&
(
connectHost
[
0
]
!=
'\0'
)
&&
(
proxyPort
>
0
||
connectPort
>
0
))
{
if
(
connectPort
<
0
)
connectPort
=
proxyPort
+
DEFAULT_NX_PROXY_PORT_OFFSET
;
char
tcpHostAndPort
[
DEFAULT_STRING_LENGTH
]
=
{
0
};
sprintf
(
tcpHostAndPort
,
"tcp:%s:%ld"
,
connectHost
,
connectPort
);
SetAndValidateChannelEndPointArg
(
"local"
,
name
,
tcpHostAndPort
,
connectSocket
);
}
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Completed parsing of string '"
*
logofs
<<
"Loop: Completed parsing of string '"
<<
env
<<
"'.
\n
"
<<
logofs_flush
;
<<
env
<<
"'.
\n
"
<<
logofs_flush
;
...
@@ -9082,15 +9358,20 @@ int ParseCommandLineOptions(int argc, const char **argv)
...
@@ -9082,15 +9358,20 @@ int ParseCommandLineOptions(int argc, const char **argv)
// command line at the connecting side.
// command line at the connecting side.
//
//
if
(
ParseHostOption
(
nextArg
,
connectHost
,
connectPort
)
>
0
)
char
*
cHost
;
long
cPort
;
if
(
connectSocket
.
getTCPHostAndPort
(
&
cHost
,
&
cPort
)
&&
(
ParseHostOption
(
nextArg
,
cHost
,
cPort
)
>
0
))
{
{
//
//
// Assume port is at a proxied display offset.
// Assume port is at a proxied display offset.
//
//
proxyPort
=
connectPort
;
proxyPort
=
cPort
;
cPort
+=
DEFAULT_NX_PROXY_PORT_OFFSET
;
connectSocket
.
setSpec
(
cHost
,
cPort
);
connectPort
+=
DEFAULT_NX_PROXY_PORT_OFFSET
;
}
}
else
if
(
ParseEnvironmentOptions
(
nextArg
,
1
)
<
0
)
else
if
(
ParseEnvironmentOptions
(
nextArg
,
1
)
<
0
)
{
{
...
@@ -13195,7 +13476,7 @@ int ParseBitrateOption(const char *opt)
...
@@ -13195,7 +13476,7 @@ int ParseBitrateOption(const char *opt)
return
1
;
return
1
;
}
}
int
ParseHostOption
(
const
char
*
opt
,
char
*
host
,
int
&
port
)
int
ParseHostOption
(
const
char
*
opt
,
char
*
host
,
long
&
port
)
{
{
#ifdef TEST
#ifdef TEST
*
logofs
<<
"Loop: Trying to parse options string '"
<<
opt
*
logofs
<<
"Loop: Trying to parse options string '"
<<
opt
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment