For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2004 > Finding if one is connected









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Finding if one is connected
Ashutosh

2004-07-19, 4:00 pm

Hi,

Is is possible to find programmatically if one is 'online', in a portable
way (across Linux, Solaris, ...)?

Suppose the network cable is plugged out or the linux machine is not
connected a network. In such a case, how can one find if one is connected?

Would one have to use ioctl calls to check each network interface?

Thanks in advance.

Regards,
Ashutosh


Pascal Bourguignon

2004-07-19, 4:00 pm

"Ashutosh" <ashusharma1980@yahoo.co.uk> writes:

> Hi,
>
> Is is possible to find programmatically if one is 'online', in a portable
> way (across Linux, Solaris, ...)?
>
> Suppose the network cable is plugged out or the linux machine is not
> connected a network. In such a case, how can one find if one is connected?
>
> Would one have to use ioctl calls to check each network interface?
>
> Thanks in advance.


This question is meaningless.

What you can ask, is whether you can send and receive some packets
with a given interface.

You could try ping, but some filter out ICMP.

So the best is to try to do what you want to do, and see if it works.

If you want to establish a TCP session with IP 216.239.59.147, port
80, just do it! And it's not because you can't connect to
216.239.59.147, that you won't be able to connect to 217.12.3.11, port
80: it perhaps because there's a cut cable in the Altantic or some
solar flares...


That said, if you know that ICMP should work, you could use ping like
I do:

lost_it=1
for host in 195.114.85.131 213.41.78.20 213.41.78.22 \
213.228.0.12 213.228.0.141 213.228.0.14 213.228.0.142 ; do
log "pinging $host."
ping -c 1 -w 20 -q $host > /dev/null 2>&1
lost_it=$?
[ $lost_it -eq 0 ] && break
done
if [ $lost_it -eq 0 ]
then echo connected
else echo not connected
fi


--
__Pascal Bourguignon__ http://www.informatimago.com/

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
Ashutosh

2004-07-19, 4:00 pm

"Pascal Bourguignon" <spam@thalassa.informatimago.com> wrote in message
news:87n01w6jyx.fsf@thalassa.informatimago.com...
> "Ashutosh" <ashusharma1980@yahoo.co.uk> writes:
>
portable[color=darkred]
connected?[color=darkred]
>
> This question is meaningless.
>
> What you can ask, is whether you can send and receive some packets
> with a given interface.
>
> You could try ping, but some filter out ICMP.
>
> So the best is to try to do what you want to do, and see if it works.
>
> If you want to establish a TCP session with IP 216.239.59.147, port
> 80, just do it! And it's not because you can't connect to
> 216.239.59.147, that you won't be able to connect to 217.12.3.11, port
> 80: it perhaps because there's a cut cable in the Altantic or some
> solar flares...
>
>
> That said, if you know that ICMP should work, you could use ping like
> I do:
>
> lost_it=1
> for host in 195.114.85.131 213.41.78.20 213.41.78.22 \
> 213.228.0.12 213.228.0.141 213.228.0.14 213.228.0.142 ; do
> log "pinging $host."
> ping -c 1 -w 20 -q $host > /dev/null 2>&1
> lost_it=$?
> [ $lost_it -eq 0 ] && break
> done
> if [ $lost_it -eq 0 ]
> then echo connected
> else echo not connected
> fi
>
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> There is no worse tyranny than to force a man to pay for what he does not
> want merely because you think it would be good for him. -- Robert Heinlein


Yes, I get the point.

The problem is that when I do a 'connect()', for some IPs, it just hangs,
for some it returns in some time with "No route to host" (perror output).

Is there some way to put a timeout on "connect()". If I do a 'fcntl(sockfd,
F_SETFL, O_NONBLOCK);' on the socket, it always returns with "connect:
Operation now in progress" (perror output)

Regards,
Ashutosh


Jens.Toerring@physik.fu-berlin.de

2004-07-19, 4:00 pm

Ashutosh <ashusharma1980@yahoo.co.uk> wrote:
> The problem is that when I do a 'connect()', for some IPs, it just hangs,
> for some it returns in some time with "No route to host" (perror output).


> Is there some way to put a timeout on "connect()". If I do a 'fcntl(sockfd,
> F_SETFL, O_NONBLOCK);' on the socket, it always returns with "connect:
> Operation now in progress" (perror output)


You can call e.g. sleep() with the maximum time you're prepared to
wait for the server before calling connect(). When you get a SIGALRM
before connect() had success errno will be set to EINTR (just make
sure you handle the SIGALRM signal properly in order not to get
killed by it).
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Ashutosh

2004-07-19, 4:00 pm

----- Original Message -----
From: <Jens.Toerring@physik.fu-berlin.de>
Newsgroups: comp.unix.programmer
Sent: Tuesday, July 20, 2004 12:15 AM
Subject: Re: Finding if one is connected


> Ashutosh <ashusharma1980@yahoo.co.uk> wrote:
hangs,[color=darkred]
output).[color=darkred]
>
'fcntl(sockfd,[color=darkred]
>
> You can call e.g. sleep() with the maximum time you're prepared to
> wait for the server before calling connect(). When you get a SIGALRM
> before connect() had success errno will be set to EINTR (just make
> sure you handle the SIGALRM signal properly in order not to get
> killed by it).
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
> \__________________________ http://www.toerring.de



Thanks Jens.

I just found another solution too:

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define DEST_IP "10.40.25.1"
#define DEST_PORT 23

main ()
{
int sockfd;
int retval;
struct sockaddr_in dest_addr;
fd_set sset;
struct timeval tv;

sockfd = socket (AF_INET, SOCK_STREAM, 0);
fcntl(sockfd, F_SETFL, O_NONBLOCK);

dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons (DEST_PORT);
dest_addr.sin_addr.s_addr = inet_addr (DEST_IP);
memset (&(dest_addr.sin_zero), '\0', 8);

retval = connect (sockfd, (struct sockaddr *) &dest_addr, sizeof (struct
sockaddr));
if(revtal < 0)
{
if(errno == EINPROGRESS)
{
FD_ZERO(&sset);
FD_SET(sockfd, &sset);
tv.tv_sec = 1;
tv.tv_usec = 500000; // Wait 1.5 seconds

retval = select(sockfd+1, NULL, &sset, NULL, &tv);
if(retval > 0)
{
int val;
int lon = sizeof(int);
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)(&val),
&lon);
printf("val = %d\n", val);
if(val != 0)
{
errno = val;
perror("select");
}
}
else
{
errno = ETIMEDOUT;
perror("select");
}
}
}
}


David Schwartz

2004-07-19, 8:57 pm


"Ashutosh" <ashusharma1980@yahoo.co.uk> wrote in message
news:S8RKc.636$yo4.397387@monger.newsread.com...

> Is is possible to find programmatically if one is 'online', in a portable
> way (across Linux, Solaris, ...)?


Yes, define exactly what you mean by "online" and test for that.

> Suppose the network cable is plugged out or the linux machine is not
> connected a network. In such a case, how can one find if one is connected?


Define exactly what you mean by "connected" and test for that.

> Would one have to use ioctl calls to check each network interface?


It depends exactly what you want to test for. You will have to be 100%
precise and define what you mean by terms like "online" and "connected".
Then you can test for conditions that meet your definitions.

DS


Pascal Bourguignon

2004-07-20, 3:57 am


"Ashutosh" <ashusharma1980@yahoo.co.uk> writes:
> [...]
> Thanks Jens.
>
> I just found another solution too:
> [...]
> retval = connect (sockfd, (struct sockaddr *) &dest_addr, sizeof (struct
> sockaddr));
> if(revtal < 0)
> {
> if(errno == EINPROGRESS)
> [...]


Indeed, errno == EINPROGRESS is not an error but the normal return for
an asynchronous connection.


--
__Pascal Bourguignon__ http://www.informatimago.com/

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
Ashutosh

2004-07-20, 3:57 am

"David Schwartz" <davids@webmaster.com> wrote in message
news:cdhm4k$vde$1@nntp.webmaster.com...
>
> "Ashutosh" <ashusharma1980@yahoo.co.uk> wrote in message
> news:S8RKc.636$yo4.397387@monger.newsread.com...
>
portable[color=darkred]
>
> Yes, define exactly what you mean by "online" and test for that.
>
connected?[color=darkred]
>
> Define exactly what you mean by "connected" and test for that.
>
>
> It depends exactly what you want to test for. You will have to be 100%
> precise and define what you mean by terms like "online" and "connected".
> Then you can test for conditions that meet your definitions.
>
> DS
>
>


I get the point.
Rephrasing the question: If one wants to test if any one of the network
interfaces (other than "lo") is up, how can one do it, possibly in a
platform-agnostic way?

Regards,
Ashutosh


David Schwartz

2004-07-20, 3:59 pm


"Ashutosh" <ashusharma1980@yahoo.co.uk> wrote in message
news:G12Lc.702$yo4.416437@monger.newsread.com...

> I get the point.
> Rephrasing the question: If one wants to test if any one of the network
> interfaces (other than "lo") is up, how can one do it, possibly in a
> platform-agnostic way?


Find and exec 'ifconfig -a' and check the resulting output. It's
painful, but it's the closest to portable that you will get. Otherwise, you
can mess with the SIOCGIFADDR/SIOCGIFCONF ioctl's.

DS


Bjorn Reese

2004-07-20, 3:59 pm

On Mon, 19 Jul 2004 19:01:14 +0000, Ashutosh wrote:

> if(errno == EINPROGRESS)


As you asked for a platform agnostic method, you should
be aware that not all platforms return EINPROGRESS, but
rather EWOULDBLOCK or EAGAIN, so I suggest that you test
for all these error numbers.

--
mail1dotstofanetdotdk

Ashutosh

2004-07-21, 3:57 am

"Bjorn Reese" <breese@see.signature> wrote in message
news:pan.2004.07.20.18.34.36.533346@see.signature...
> On Mon, 19 Jul 2004 19:01:14 +0000, Ashutosh wrote:
>
>
> As you asked for a platform agnostic method, you should
> be aware that not all platforms return EINPROGRESS, but
> rather EWOULDBLOCK or EAGAIN, so I suggest that you test
> for all these error numbers.
>
> --
> mail1dotstofanetdotdk
>


Thanks Bjorn, for the suggestion.

Regards,
Ashutosh


Ashutosh

2004-07-21, 3:57 am

"David Schwartz" <davids@webmaster.com> wrote in message
news:cdjnfp$844$1@nntp.webmaster.com...
>
> "Ashutosh" <ashusharma1980@yahoo.co.uk> wrote in message
> news:G12Lc.702$yo4.416437@monger.newsread.com...
>
>
> Find and exec 'ifconfig -a' and check the resulting output. It's
> painful, but it's the closest to portable that you will get. Otherwise,

you
> can mess with the SIOCGIFADDR/SIOCGIFCONF ioctl's.
>
> DS
>
>


Thanks David. I will try the two approaches.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com