gettermaddr()

107 views
Skip to first unread message

Davor Siklic

unread,
May 21, 2013, 7:48:43 PM5/21/13
to Harbour Developers
Hi all

gettermaddr() -> return terminal remote IP address

long time I using this function in clip, but I cannot find something
similar in harbour
is there any solution or maybe possibility to implement in harbour?

there are code from clip implementation (I'm not c programer)

thank you and best regards
Davor


/*****************************/
int
clip_GETTERMADDR(ClipMachine *mp)
{
struct in_addr ina;

if (tcp_term_addr(&ina) == 0)
_clip_retc(mp, inet_ntoa(ina));
else
_clip_retc(mp, "");

return 0;
}
/*****************************/


/* try to detected remote terminal IP address */
static int tcp_term_addr(struct in_addr *ina)
{
char *env_val, *ptr;
int ret = -1;

/* REMOTEHOST is set by telnetd */
if ((env_val = getenv( "REMOTEHOST" )) != NULL &&
tcp_host_addr(env_val, ina) == 0)
ret = 0;

/* SSH_CLIENT is set by sshd but some of
implementation doesn't 'export' it */
if (ret == -1 && (env_val = getenv( "SSH_CLIENT" )) != NULL)
{
if ((ptr = strchr(env_val, ' ')) != NULL)
*ptr = '\0';
if (tcp_host_addr(env_val, ina) == 0)
ret = 0;
}

#ifdef USE_UTMP
/* If we cannot find a proper address in environment we have
to check UTMP entries for our current terminal */
if (ret == -1)
{
struct utmp entry, *ut;
if ((ptr = ttyname( 0 ) ) != NULL)
{
if (strncmp( ptr, "/dev/", 5 ) == 0)
ptr += 5;
strcpy(entry.ut_line, ptr);
setutent();
if ((ut = getutline(&entry)) != NULL &&
ut->ut_addr != 0 && ut->ut_addr != INADDR_NONE &&
(ptr = inet_ntoa(*(struct in_addr*) &ut->ut_addr)) != NULL &&
tcp_host_addr(ptr, ina) == 0)
ret=0;
endutent();
}
}
#endif
if (ret == -1)
ina->s_addr = INADDR_NONE;
return ret;
}

Tamas TEVESZ

unread,
May 22, 2013, 6:50:47 AM5/22/13
to Harbour Developers
On Wed, 22 May 2013, Davor Siklic wrote:

hi,

this is not a 1:1 answer to your question and completely doesn't take
windows hosts into account.

i am yet to see an sshd or even telnetd that does not export
SSH_CONNECTION/REMOTEHOST (not much point in having them unexported,
see).

consequently, looking for the connecting host in the login records,
well, i feel it's a bit icky (nevertheless, it can be done, but i
simply don't have the time to do it properly now).

with that said, here's an *** incredibly lightly *** tested quickie
that you can use for something that somewhat mimicks clip's behaviour
(without the login records stuff).

it's neither nice nor very robust nor can properly handle ipv6 (at
least i don't think it can) and generally it's bleeding from a
thousand wounds, but from a very quick skim, it's not (much more)
worse than clip's implementation, in fact i've been looking at it
while converting it quickly.

PROCEDURE Main()

OutStd( hb_StrFormat( "Remote addr: %s", clip_GetTermAddr() ) + hb_eol() )

RETURN

FUNCTION clip_GetTermAddr()

LOCAL cRemoteAddr

cRemoteAddr = hb_ATokens( GetEnv( "SSH_CLIENT" ), " " )[ 1 ]
IF cRemoteAddr == ""
cRemoteAddr = hb_GetHostByName( GetEnv( "REMOTEHOST" ) )
ENDIF

RETURN cRemoteAddr

#pragma BEGINDUMP

#include "hbapi.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h>
#include <string.h>

#include <stdio.h>

HB_FUNC( HB_GETHOSTBYNAME )
{
char * ipaddr = hb_parcx( 1 );
struct in_addr ipa;
struct hostent * he;
char * res;

fprintf(stderr, "[%s]\n", ipaddr);fflush(stderr);

res = NULL;

if( * ipaddr == '\0' )
{
goto out;
}

if( inet_aton( ipaddr, &ipa ) )
{
res = ipaddr;
goto out;
}

he = gethostbyname( ipaddr );
if( !he ||
he->h_addrtype != AF_INET ||
!he->h_addr_list ||
!he->h_addr_list[ 0 ] )
{
res = NULL;
goto out;
}

memcpy( &ipa, he->h_addr_list[ 0 ], he->h_length );
res = inet_ntoa( ipa );

out:
hb_retc( res ? res : "" );
}

#pragma ENDDUMP

--
[-]

mkdir /nonexistent

Tamas TEVESZ

unread,
May 22, 2013, 6:55:04 AM5/22/13
to Harbour Developers
On Wed, 22 May 2013, Tamas TEVESZ wrote:

> with that said, here's an *** incredibly lightly *** tested quickie

try without the leftover debugprintfs left in.

PROCEDURE Main()

OutStd( hb_StrFormat( "Remote addr: %s", clip_GetTermAddr() ) + hb_eol() )

RETURN

FUNCTION clip_GetTermAddr()

LOCAL cRemoteAddr

cRemoteAddr = hb_ATokens( GetEnv( "SSH_CLIENT" ), " " )[ 1 ]
IF cRemoteAddr == ""
cRemoteAddr = hb_GetHostByName( GetEnv( "REMOTEHOST" ) )
ENDIF

RETURN cRemoteAddr

#pragma BEGINDUMP

#include "hbapi.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h>
#include <string.h>

HB_FUNC( HB_GETHOSTBYNAME )
{
char * ipaddr = hb_parcx( 1 );
struct in_addr ipa;
struct hostent * he;
char * res;

siki

unread,
May 22, 2013, 9:18:24 AM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013 12:50:47 +0200 (CEST), Tamas TEVESZ <i...@extreme.hu>
wrote:
> On Wed, 22 May 2013, Davor Siklic wrote:
>
> hi,
>
> this is not a 1:1 answer to your question and completely doesn't take
> windows hosts into account.
>
> i am yet to see an sshd or even telnetd that does not export
> SSH_CONNECTION/REMOTEHOST (not much point in having them unexported,
> see).
>
> consequently, looking for the connecting host in the login records,
> well, i feel it's a bit icky (nevertheless, it can be done, but i
> simply don't have the time to do it properly now).
>
> with that said, here's an *** incredibly lightly *** tested quickie
> that you can use for something that somewhat mimicks clip's behaviour
> (without the login records stuff).
>
> it's neither nice nor very robust nor can properly handle ipv6 (at
> least i don't think it can) and generally it's bleeding from a
> thousand wounds, but from a very quick skim, it's not (much more)
> worse than clip's implementation, in fact i've been looking at it
> while converting it quickly.
>
Hi Tomas

thank you very much for help
I try the code but problem is that I have system running on LTSP terminal
server
and don't have exported SSH_CONNECTION/REMOTEHOST.

Trying and didn't find where to make that exported.
Clip function get this info from utmp and work as expected.


hbmk2 ttt.prg
hbmk2: Processing environment options: -compiler=gcc
Harbour 3.1.0dev (Rev. 17260)
Copyright (c) 1999-2012, http://harbour-project.org/
Compiling 'ttt.prg'...
Lines 66, Functions/Procedures 2
Generating C source output to '/tmp/hbmk_qniv35.dir/ttt.c'... Done.
ttt.prg: In function 'HB_FUN_HB_GETHOSTBYNAME':
ttt.prg:31: warning: initialization discards qualifiers from pointer
target type

siki@vampire:~/prg/test$ ./ttt
Remote addr:

Would be great to have this done when You have some more time
Thank You very much
Davor
--
Davor Siklic
MSoft
Zelena 409
29442 Lustenice
Czech Republic

Tamas TEVESZ

unread,
May 22, 2013, 9:31:02 AM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013, siki wrote:

hi,

> thank you very much for help
> I try the code but problem is that I have system running on LTSP terminal
> server
> and don't have exported SSH_CONNECTION/REMOTEHOST.

ah, i see then.

what type of software do you use to connect to the ltsp terminal
server (vnc/remote x/rdesktop/etc)?

is that ltsp v4 or ltsp v5?

--
[-]

mkdir /nonexistent

siki

unread,
May 22, 2013, 10:55:42 AM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013 15:31:02 +0200 (CEST), Tamas TEVESZ <i...@extreme.hu>
wrote:
Using thin terminal, booting from network and running remote on ubuntu
server, LTSP v5
I'm running aplication from server but need to know from where (IP, host)
application running
because of redirect printing, bar-code reader's etc...

siki@vampire:~/prg/test$ w
15:19:33 up 6 days, 8:16, 7 users, load average: 0,56, 0,60, 0,53
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
kriz pts/0 tk048:0.0 14:21 27:00 8.89s 0.01s /bin/bash
/usr/
touch pts/1 touch 13:21 1:57m 21.18s 0.01s /bin/bash
/usr/
immuno pts/2 tk047:0.0 Tue15 49:17 27:56 0.01s /bin/bash
/usr/
siki pts/3 tk045:0.0 15:08 1.00s 0.32s 0.02s w
expedice pts/4 tk046:0.0 Tue17 50:12 36:42 0.01s /bin/bash
/usr/
siki pts/8 tk045:0.0 14:28 45:46 0.34s 0.34s bash
siki pts/6 tk045:0.0 14:22 27:43 0.65s 0.26s vim
pac_def.prg

solution is that first found tty and resolve hostname, all that from utmp
I think that this is the way how work in clip

Tamas TEVESZ

unread,
May 22, 2013, 11:04:01 AM5/22/13
to harbou...@googlegroups.com

nevermind, here's a straight copy off clip, adapted for harbour. it's
supposed to work on linux (not so sure about other platforms), but
that should get you going.

PROCEDURE Main()

OutStd( hb_StrFormat( "Remote addr: %s", clip_GetTermAddr() ) + hb_eol() )

RETURN

FUNCTION clip_GetTermAddr()

LOCAL cRemoteAddr

cRemoteAddr = hb_ATokens( GetEnv( "SSH_CLIENT" ), " " )[ 1 ]
IF cRemoteAddr == ""
cRemoteAddr = hb_GetHostByName( GetEnv( "REMOTEHOST" ) )
IF cRemoteAddr == ""
cRemoteAddr = hb_GetHostByName( hb_UTAddr() )
ENDIF
ENDIF

RETURN cRemoteAddr

#pragma BEGINDUMP

#include "hbapi.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h>
#include <string.h>

#include <unistd.h>
#include <utmp.h>

HB_FUNC( HB_GETHOSTBYNAME )
{
char * ipaddr = hb_parcx( 1 );
struct in_addr ipa;
struct hostent * he;
char * res;

res = NULL;

if( * ipaddr == '\0' )
{
goto out;
}

if( inet_aton( ipaddr, &ipa ) )
{
res = ipaddr;
goto out;
}

he = gethostbyname( ipaddr );
if( !he ||
he->h_addrtype != AF_INET ||
!he->h_addr_list ||
!he->h_addr_list[ 0 ] )
{
res = NULL;
goto out;
}

memcpy( &ipa, he->h_addr_list[ 0 ], he->h_length );
res = inet_ntoa( ipa );

out:
hb_retc( res ? res : "" );
}

HB_FUNC( HB_UTADDR )
{
struct utmp entry;
struct utmp * ut;
char * ptr;
char * res;

res = NULL;

ptr = ttyname( 0 );
if( ! ptr )
{
res = NULL;
goto out;
}

if( strncmp( ptr, "/dev/", 5 ) == 0 )
{
ptr += 5;
}

strncpy( entry.ut_line, ptr, UT_LINESIZE );
ptr = NULL;

setutent();
if( ( ut = getutline( &entry ) ) != NULL &&
ut->ut_addr != 0 &&
ut->ut_addr != INADDR_NONE &&
( ptr = inet_ntoa( *( struct in_addr * )&ut->ut_addr ) ) != NULL )
{
res = ptr;
}
endutent();

siki

unread,
May 22, 2013, 11:29:39 AM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013 17:04:01 +0200 (CEST), Tamas TEVESZ <i...@extreme.hu>
wrote:
> nevermind, here's a straight copy off clip, adapted for harbour. it's
> supposed to work on linux (not so sure about other platforms), but
> that should get you going.
>

siki@vampire:~/prg/test$ hbmk2 tttt.prg
hbmk2: Processing environment options: -compiler=gcc
Harbour 3.1.0dev (Rev. 17260)
Copyright (c) 1999-2012, http://harbour-project.org/
Compiling 'tttt.prg'...
Lines 111, Functions/Procedures 2
Generating C source output to '/tmp/hbmk_i66elp.dir/tttt.c'... Done.
tttt.prg: In function 'HB_FUN_HB_GETHOSTBYNAME':
tttt.prg:37: warning: initialization discards qualifiers from pointer
target type
tttt.prg: In function 'HB_FUN_HB_UTADDR':
tttt.prg:99: warning: comparison between signed and unsigned integer
expressions

siki@vampire:~/prg/test$ ./tttt
Remote addr:


something are wrong

using tty show that using tty pts/3
siki@vampire:~/prg/test$ tty

/dev/pts/3


siki@vampire:~/prg/test$ w
17:32:58 up 6 days, 10:30, 7 users, load average: 0,75, 0,74, 0,67
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
kriz pts/0 tk048:0.0 14:21 1:18m 41.08s 0.01s /bin/bash
/usr/
touch pts/1 touch 13:21 4:11m 39.12s 0.01s /bin/bash
/usr/
immuno pts/2 tk047:0.0 Tue15 11:59 32:04 0.01s /bin/bash
/usr/
siki pts/3 tk045:0.0 15:08 1.00s 0.54s 9.59s
gnome-terminal
expedice pts/4 tk046:0.0 Tue17 14:28 41:12 0.01s /bin/bash
/usr/
siki pts/8 tk045:0.0 14:28 2:59m 0.34s 0.34s bash
siki pts/6 tk045:0.0 14:22 2:41m 0.65s 0.26s vim
pac_def.prg


output from w show that I running form host tk045

Tamas TEVESZ

unread,
May 22, 2013, 11:54:10 AM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013, siki wrote:

> siki@vampire:~/prg/test$ ./tttt
> Remote addr:
>
> something are wrong

screen vs not screen

--
[-]

mkdir /nonexistent

siki

unread,
May 22, 2013, 12:03:45 PM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013 17:54:10 +0200 (CEST), Tamas TEVESZ <i...@extreme.hu>
wrote:
>
> screen vs not screen
>
sorry I didn't understand what you mean (my bad English)

a make again test with Your last source and get the same result...
siki@vampire:~/prg/test$ vim tomas.prg
siki@vampire:~/prg/test$ hbmk2 tomas.prg
hbmk2: Processing environment options: -compiler=gcc
Harbour 3.1.0dev (Rev. 17260)
Copyright (c) 1999-2012, http://harbour-project.org/
Compiling 'tomas.prg'...
Lines 111, Functions/Procedures 2
Generating C source output to '/tmp/hbmk_8p7928.dir/tomas.c'... Done.
tomas.prg: In function 'HB_FUN_HB_GETHOSTBYNAME':
tomas.prg:37: warning: initialization discards qualifiers from pointer
target type
tomas.prg: In function 'HB_FUN_HB_UTADDR':
tomas.prg:99: warning: comparison between signed and unsigned integer
expressions
siki@vampire:~/prg/test$ ./tomas
Remote addr:

siki@vampire:~/prg/test$

Tamas TEVESZ

unread,
May 22, 2013, 12:20:41 PM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013, siki wrote:

run it on a terminal that is not managed by screen.

run the clip-compiled equivalent on a terminal that is managed by
screen -- it won't work either.
--
[-]

mkdir /nonexistent

siki

unread,
May 22, 2013, 1:10:36 PM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013 18:20:41 +0200 (CEST), Tamas TEVESZ <i...@extreme.hu>
wrote:
> On Wed, 22 May 2013, siki wrote:
>
> run it on a terminal that is not managed by screen.
>
> run the clip-compiled equivalent on a terminal that is managed by
> screen -- it won't work either.
>
Understand now...

I found a way how to get runnig. In this case DISPLAY is set to
host:display

siki@vampire:~/prg/test$ set | grep DISPLAY
DISPLAY=tk045:0.0

--------------------------------
PROCEDURE Main()

OutStd( hb_StrFormat( "Remote addr: %s", clip_GetTermAddr() ) +
hb_eol() )

RETURN

FUNCTION clip_GetTermAddr()

LOCAL cRemoteAddr

cRemoteAddr := hb_ATokens( GetEnv( "DISPLAY" ), ":" )[ 1 ]
IF cRemoteAddr == ""
cRemoteAddr = hb_ATokens( GetEnv( "SSH_CLIENT" ), " " )[ 1 ]
ENDIF
----------------------------------------------

thank You very much for help
best regards
Davor

Tamas TEVESZ

unread,
May 22, 2013, 2:13:36 PM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013, siki wrote:

> I found a way how to get runnig. In this case DISPLAY is set to
> host:display

yes, that is even better. there's no need for utmp here then.


--
[-]

mkdir /nonexistent

Tamas TEVESZ

unread,
May 22, 2013, 4:12:55 PM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013, Tamas TEVESZ wrote:

> On Wed, 22 May 2013, siki wrote:
>
> > I found a way how to get runnig. In this case DISPLAY is set to
> > host:display
>
> yes, that is even better. there's no need for utmp here then.

oh, i just found hb_socketResolveAddr(), which makes things a lot
easier. i knew i've seem something to this effect in rtl at one point
:)

also note that your way doesn't resolve DISPLAY back to ip addresses
(clip's gettermaddr() always gives you ip addresses).

so the following will cover your use case:

FUNCTION clip_GetTermAddr()

STATIC s_cRemoteAddr := ""
STATIC s_lSeen := .F.

IF s_lSeen
RETURN s_cRemoteAddr
ENDIF

s_cRemoteAddr := hb_socketResolveAddr( hb_ATokens( GetEnv( "DISPLAY" ), ":" )[ 1 ] )
IF s_cRemoteAddr == ""
s_cRemoteAddr = hb_ATokens( GetEnv( "SSH_CLIENT" ), " " )[ 1 ]
IF s_cRemoteAddr == ""
s_cRemoteAddr = hb_socketResolveAddr( GetEnv( "REMOTEHOST" ) )
ENDIF
ENDIF

s_lSeen := .T.

RETURN s_cRemoteAddr

it uses dependable harbour core stuff only, which is a plus, also
caches the result (getenv() is relatively expensive) -- this can bite
you in the butt in certain cases, though. you can put back the
half-assed utmp thing if you want to, but you won't really need it
(the more i think about it, especially in light of DISPLAY as i've
gently been reminded, it's utterly useless anyway, maybe not
"utterly", but to a considerable extent).

here, clip_GetTermAddr() in pure prg. no c programmer needed.
hb_socketResolveAddr() even does ipv6, if used appropriately.

--
[-]

mkdir /nonexistent

siki

unread,
May 22, 2013, 5:10:48 PM5/22/13
to harbou...@googlegroups.com
On Wed, 22 May 2013 22:12:55 +0200 (CEST), Tamas TEVESZ <i...@extreme.hu>
wrote:
> On Wed, 22 May 2013, Tamas TEVESZ wrote:
>
> > On Wed, 22 May 2013, siki wrote:
> >
> > > I found a way how to get runnig. In this case DISPLAY is set to
> > > host:display
> >
> > yes, that is even better. there's no need for utmp here then.
>
no, at the end seem to really no need for utmp :-)

> oh, i just found hb_socketResolveAddr(), which makes things a lot
> easier. i knew i've seem something to this effect in rtl at one point
> :)
>
> also note that your way doesn't resolve DISPLAY back to ip addresses
> (clip's gettermaddr() always gives you ip addresses).
>
> so the following will cover your use case:
>
> FUNCTION clip_GetTermAddr()
>
> STATIC s_cRemoteAddr := ""
> STATIC s_lSeen := .F.
>
> IF s_lSeen
> RETURN s_cRemoteAddr
> ENDIF
>
> s_cRemoteAddr := hb_socketResolveAddr( hb_ATokens( GetEnv( "DISPLAY"
),
> ":" )[ 1 ] )
> IF s_cRemoteAddr == ""
> s_cRemoteAddr = hb_ATokens( GetEnv( "SSH_CLIENT" ), " " )[ 1 ]
> IF s_cRemoteAddr == ""
> s_cRemoteAddr = hb_socketResolveAddr( GetEnv( "REMOTEHOST" ) )
> ENDIF
> ENDIF
>
> s_lSeen := .T.
>
> RETURN s_cRemoteAddr
>
seem great, is elegant, and work like a charm :-)


> it uses dependable harbour core stuff only, which is a plus, also
> caches the result (getenv() is relatively expensive) -- this can bite
> you in the butt in certain cases, though. you can put back the
> half-assed utmp thing if you want to, but you won't really need it
> (the more i think about it, especially in light of DISPLAY as i've
> gently been reminded, it's utterly useless anyway, maybe not
> "utterly", but to a considerable extent).
>
function is used only when starting application so I think that getenv()
didn't make any problem
we always can to put back utmp code if somebody need them. Question is
where to put this function

I vote to make new contrib library cliptohb. I have some more functions to
contribute...


> here, clip_GetTermAddr() in pure prg. no c programmer needed.
> hb_socketResolveAddr() even does ipv6, if used appropriately.


Thank You very much
best regards
Davor

Tamas TEVESZ

unread,
May 23, 2013, 1:20:20 AM5/23/13
to harbou...@googlegroups.com
On Wed, 22 May 2013, siki wrote:

> function is used only when starting application so I think that getenv()
> didn't make any problem
> we always can to put back utmp code if somebody need them. Question is
> where to put this function
>
> I vote to make new contrib library cliptohb. I have some more functions to
> contribute...

"clip" perhaps (following contrib/xhb's naming convention).

good idea. put your things together, i'll help (afk for a few days now
though). it's gonna be a lot of work (at least initially), so be
prepared :) read and heed "how to participate" in readme wrt
formatting and everything.


--
[-]

mkdir /nonexistent

Przemyslaw Czerpak

unread,
May 24, 2013, 10:26:39 AM5/24/13
to harbou...@googlegroups.com
On Thu, 23 May 2013, Tamas TEVESZ wrote:

Hi,
Please only remember that CLIP public release is on pure GPL license
so coping the code directly from CLIP is not good idea though in this
particular case you are talking probably about my code I sent to Uri
over 10 years ago asking him to add it to CLIP code.
BTW using DISPLAY is not good idea because it will show wrong result
for SSH connections with X port forwarding, i.e. try:
ssh -X somehost
and then check DISPLAY, you will see sth like:
localhost:11.0
or:
127.0.0.1:11.0
In most of cases it should be enough to verify if canonical address
points to local host and ignore such results. It's also not perfect
but good enough.

BTW2 the trick with UTMP does not work with MC and other wrappers
which execute process in new pseudo terminal not registered in utmp
database.

best regards,
Przemek

Davor Siklic

unread,
May 24, 2013, 4:13:37 PM5/24/13
to harbou...@googlegroups.com
Dne 24.5.2013 16:26, Przemyslaw Czerpak napsal(a):
> Please only remember that CLIP public release is on pure GPL license
> so coping the code directly from CLIP is not good idea though in this
> particular case you are talking probably about my code I sent to Uri
> over 10 years ago asking him to add it to CLIP code.
Yes, I only use this code as way to temporary solve proble
yes :-)

> BTW using DISPLAY is not good idea because it will show wrong result
> for SSH connections with X port forwarding, i.e. try:
> ssh -X somehost
> and then check DISPLAY, you will see sth like:
> localhost:11.0
> or:
> 127.0.0.1:11.0
> In most of cases it should be enough to verify if canonical address
> points to local host and ignore such results. It's also not perfect
> but good enough.
in case of SSH connnection with port forwarding SSH_CLIENT are set
properly and changing the check order solve the problem:

FUNCTION GetTermAddr()

STATIC s_cRemoteAddr := ""
STATIC s_lSeen := .F.

IF s_lSeen
RETURN s_cRemoteAddr
ENDIF

s_cRemoteAddr = hb_ATokens( GetEnv( "SSH_CLIENT" ), " " )[ 1 ]

IF s_cRemoteAddr == ""
s_cRemoteAddr = hb_socketResolveAddr( GetEnv( "REMOTEHOST" ) )
IF s_cRemoteAddr == ""
s_cRemoteAddr := hb_socketResolveAddr( hb_ATokens(
GetEnv( "DISPLAY" ), ":" )[ 1 ] )
ENDIF
ENDIF

s_lSeen := .T.

RETURN s_cRemoteAddr

Not perfect but mostly work without utmp stuff
>
> BTW2 the trick with UTMP does not work with MC and other wrappers
> which execute process in new pseudo terminal not registered in utmp
> database.
understand...
>
> best regards,
> Przemek
>
there are code You send as candidate no 1. for clip library (only to not
forget :-)

FUNCTION SysCmd( cCmd, cIn, cOut, cErr, aEnv, cDir )
LOCAL nResult
LOCAL cPrevDir

HB_SYMBOL_UNUSED( aEnv )

IF !Empty( cDir )
cPrevDir := hb_cwd( cDir )
ENDIF

nResult := hb_processRun( "/bin/sh -c '" + cCmd + "'", cIn, ;
iif( HB_PIsByRef( 3 ), @cOut, NIL ), ;
iif( HB_PIsByRef( 4 ), @cErr, NIL ) )
IF !Empty( cPrevDir )
hb_cwd( cPrevDir )
ENDIF

RETURN nResult

thank You
best regards
Davor





Reply all
Reply to author
Forward
0 new messages