1. ./EuGTK/GtkEngine.e to Irv

I find when I run wee on Linux I see an error trying to find ifconfig. Wee looks great. The error I can safely ignore. Anyway, I traced it to this line of code in ./EuGTK/GtkEngine.e:

if system_exec(sprintf("ifconfig |  grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' > %s ",{tmp}),0) = 0 then 
 

Does Irv have a github page for this where one can post bug reports?

new topic     » topic index » view message » categorize

2. Re: ./EuGTK/GtkEngine.e to Irv

On some / most linux installations you need to be root to run ifconfig, or provide a path to ifconfig (afai remember /sbin), or set sudo. I usually just login as root to use ifconfig.

Chris

new topic     » goto parent     » topic index » view message » categorize

3. Re: ./EuGTK/GtkEngine.e to Irv

ChrisB said...

On some / most linux installations you need to be root to run ifconfig, or provide a path to ifconfig (afai remember /sbin), or set sudo. I usually just login as root to use ifconfig.

Chris

Uh, I've never seen a system where one needs to be root to run ifconfig to get the status or view other settings. (Now, changing something, like setting a new ip address, does require root. But that's different.)

new topic     » goto parent     » topic index » view message » categorize

4. Re: ./EuGTK/GtkEngine.e to Irv

I stand corrected, I've just tried it on my Lubuntiu system.

Since I started using Ubuntu, I haven't had the need to log in as root to ifconfig, so this comment stems from my days of using SuSE, and a little brain fugue while trying to figure out why ifconfig wouldn't work as an ordinary user.

Chris

new topic     » goto parent     » topic index » view message » categorize

5. Re: ./EuGTK/GtkEngine.e to Irv

Thinking it over though, it might a case where only the root user/sudo has /sbin in the PATH, while regular users have /sbin excluded from the path (so root can just do ifconfig -a while a regular user has to do /sbin/ifconfig -a).

new topic     » goto parent     » topic index » view message » categorize

6. Re: ./EuGTK/GtkEngine.e to Irv

I was a bit taken aback to see an error about ifconfig when running wee. You may run ifconfig as a normal user. You just cannot do any changes (or damage) by running ifconfig as a normal user. It is used in EuGTK to determine whether you have an internet connection.

As a normal user, it must be called as /sbin/ifconfig rather than just ifconfig. Does Irv run his GTK test programs as root? Anyway you guys kind of missed the point of the original post. It is kind of obvious you shouldn't be editing stuff as root if they are not in /etc.

new topic     » goto parent     » topic index » view message » categorize

7. Re: ./EuGTK/GtkEngine.e to Irv

SDPringle said...

I was a bit taken aback to see an error about ifconfig when running wee. You may run ifconfig as a normal user. You just cannot do any changes (or damage) by running ifconfig as a normal user. It is used in EuGTK to determine whether you have an internet connection.

There's better ways to do that. Use std/socket.e and create a socket to TCP port 80 to www.whitehouse.gov (for example).

SDPringle said...

As a normal user, it must be called as /sbin/ifconfig rather than just ifconfig.

I quibble with the "must" piece. I regularly add /sbin/ to my PATH in /.bash_profile just so I don't have to call it as /sbin/ifconfig ...

SDPringle said...

Does Irv run his GTK test programs as root?

Or he does what I do.

SDPringle said...

Anyway you guys kind of missed the point of the original post. It is kind of obvious you shouldn't be editing stuff as root if they are not in /etc.

Huh? What does /etc have to do with the original post?

new topic     » goto parent     » topic index » view message » categorize

8. Re: ./EuGTK/GtkEngine.e to Irv

ifconfig runs just fine as a regular user on Mint. However, I'll take that out and try the suggestion posted earlier.

new topic     » goto parent     » topic index » view message » categorize

9. Re: ./EuGTK/GtkEngine.e to Irv

OK, what is the correct way to get my ip address if I am connected to a network but NOT to the internet?

For example, I run my Eu webserver to make music available locally. I don't think anyone at whitehouse.gov wants to listen (to music, that is, they're already listening to everything else...).

Edit: a cross-platform solution would be nice.

new topic     » goto parent     » topic index » view message » categorize

10. Re: ./EuGTK/GtkEngine.e to Irv

Output of ifconfig varies in newer versions, also locale dependent in some systems.

$ ifconfig -V 
net-tools 1.60 
ifconfig 1.42 (2001-04-13) 
$ ifconfig | grep 'inet:'  
          Direc. inet:127.0.0.1  Másc:255.0.0.0 
          Direc. inet:192.168.1.139  Difus.:192.168.1.255  Másc:255.255.255.0 
$ LANG=C ifconfig | grep 'inet '  
          inet addr:127.0.0.1  Mask:255.0.0.0 
          inet addr:192.168.1.139  Bcast:192.168.1.255  Mask:255.255.255.0 
newer versions:
$ ifconfig -V 
net-tools 2.10-alpha 
$ LANG=C ifconfig | grep 'inet '  
        inet 127.0.0.1  netmask 255.0.0.0 
        inet 192.168.1.139  netmask 255.255.255.0  broadcast 192.168.1.255 
try this:
LANG=C ifconfig | sed 's/addr://' | grep 'inet ' | grep -v '127.0.0.1' | awk '{ print $2 }' 

new topic     » goto parent     » topic index » view message » categorize

11. Re: ./EuGTK/GtkEngine.e to Irv

Thanks, but the original problem appears to be that some people cannot run ifconfig. Perhaps this varies depending upon the distro in use, or ?

ifconfig probably doesn't work on windows, netstat, perhaps?

new topic     » goto parent     » topic index » view message » categorize

12. Re: ./EuGTK/GtkEngine.e to Irv

irv said...

OK, what is the correct way to get my ip address if I am connected to a network but NOT to the internet?

For example, I run my Eu webserver to make music available locally. I don't think anyone at whitehouse.gov wants to listen (to music, that is, they're already listening to everything else...).

Edit: a cross-platform solution would be nice.

One way is to get the hostname of the computer itself, then get the ip address for that hostname.

The 2nd step is easy to do - std/net/dns.e:host_by_name() will do the trick. However, we don't have the gethostname() function wrapped, even though it is portable: https://msdn.microsoft.com/en-us/library/windows/desktop/ms738527(v=vs.85).aspx and http://linux.die.net/man/2/gethostname

What's the purpose of getting the ip address though? Just checking for a live internet connectino is different from getting the ip to host a public webserver or something, which is different again from hosting a local fileserver that's not visible beyon the local lan, etc etc. Maybe I'm not understanding the scope here.

new topic     » goto parent     » topic index » view message » categorize

13. Re: ./EuGTK/GtkEngine.e to Irv

jimcbrown said...

What's the purpose of getting the ip address though? Just checking for a live internet connectino is different from getting the ip to host a public webserver or something, which is different again from hosting a local fileserver that's not visible beyon the local lan, etc etc. Maybe I'm not understanding the scope here.

My first reason for adding this function was so that I could use Eu to serve selected directories (music, mostly) to other wireless devices (phone, tablet, etc) via a wireless router - which has no internet connection. I needed the ip address so I could then assign a port: 8080, or whatever. See webserver.ex.

The second reason was so that I could avoid starting a browser and presenting the 'cannot connect' web page when someone clicks on a web link in a label or button. See test101.ex. More friendly to just pop up a message explaining that you can't get there from here.

new topic     » goto parent     » topic index » view message » categorize

14. Re: ./EuGTK/GtkEngine.e to Irv

irv said...
jimcbrown said...

What's the purpose of getting the ip address though? Just checking for a live internet connectino is different from getting the ip to host a public webserver or something, which is different again from hosting a local fileserver that's not visible beyon the local lan, etc etc. Maybe I'm not understanding the scope here.

My first reason for adding this function was so that I could use Eu to serve selected directories (music, mostly) to other wireless devices (phone, tablet, etc) via a wireless router - which has no internet connection. I needed the ip address so I could then assign a port: 8080, or whatever. See webserver.ex.

The second reason was so that I could avoid starting a browser and presenting the 'cannot connect' web page when someone clicks on a web link in a label or button. See test101.ex. More friendly to just pop up a message explaining that you can't get there from here.

The scope of the two reasons is different. The first one makes perfect sense and I see no problems.

However, the second one seems to be about internet connectivity. What if you are on a computer that's on a local lan with no internet conectivity - a lan that's isolated? You'd get an ip address so the check would pass, but then the browser would still present the 'cannot connect' web page. (Actually, this still isn't enough - you might be connected to the internet but still unable to access the web site for other reasons, such as Tier 1 de-peering. But maybe at that point, it's acceptable to have the browser be the one reporting the error.)

It seems like you'd actually want two separate checks - one to get the local ip address and one to check remote (i.e. internet) connectivity.

new topic     » goto parent     » topic index » view message » categorize

15. Re: ./EuGTK/GtkEngine.e to Irv

jimcbrown said...

The scope of the two reasons is different. The first one makes perfect sense and I see no problems.

However, the second one seems to be about internet connectivity. What if you are on a computer that's on a local lan with no internet conectivity - a lan that's isolated? You'd get an ip address so the check would pass, but then the browser would still present the 'cannot connect' web page. (Actually, this still isn't enough - you might be connected to the internet but still unable to access the web site for other reasons, such as Tier 1 de-peering. But maybe at that point, it's acceptable to have the browser be the one reporting the error.)

It seems like you'd actually want two separate checks - one to get the local ip address and one to check remote (i.e. internet) connectivity.

That makes sense. For the second, it would be better to let the browser deliver the bad news - such as a web page from the school telling you that you've been banned forever for googling "pistil" when all you wanted was some horticultural info.

new topic     » goto parent     » topic index » view message » categorize

16. Re: ./EuGTK/GtkEngine.e to Irv

Hi

quick quick search, for Linux boxes

ip addr show

ip addr show eth0 (if eth0 is the one you are looking for)

/sbin/ifconfig (as discussed)

hostname -I

These are confirmed as working my Lubuntu box

Any more?

on Windows,

ipconfig ipconfig /all for a more comprehensive view.

Of course you have to parse these

To get your outside word facing ip address, you have to use one of the intenet responders (whatsmyip etc)

Could be a call for a cross platform library addition here?

Chris

new topic     » goto parent     » topic index » view message » categorize

17. Re: ./EuGTK/GtkEngine.e to Irv

Here's one way to do it without using ifconfig. It creates a UDP socket, multicasts a packet to itself, and returns the from address. No error checking.

include std/dll.e 
include std/machine.e 
include std/socket.e 
 
constant  
    IPPROTO_IP = 0, 
    IPPROTO_UDP = 17, 
    IP_ADD_MEMBERSHIP = 35, 
    setsockopt_ = define_c_func(-1, "setsockopt", {C_INT, C_INT, C_INT, C_POINTER, C_INT}, C_INT) 
     
function inet_address() 
    object sock 
    sequence addr = {228,105,19,77}, saddr = sprintf("%d.%d.%d.%d", addr) 
    integer port = 13025 
    atom mreq = allocate(12) 
 
    sock = sockets:create(AF_INET, SOCK_DGRAM, IPPROTO_UDP) 
    sockets:set_option(sock, SOL_SOCKET, SO_REUSEADDR, 1) 
    sockets:bind(sock, saddr, port) 
    poke(mreq, addr & {0,0,0,0,0,0,0,0}) 
    c_func(setsockopt_, {sock[1], IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq, 12}) 
    free(mreq) 
    sockets:send_to(sock, {0}, saddr, port) 
    addr = sockets:receive_from(sock) 
    sockets:close(sock) 
    return addr[1] 
end function 
new topic     » goto parent     » topic index » view message » categorize

18. Re: ./EuGTK/GtkEngine.e to Irv

PeteE said...

Here's one way to do it without using ifconfig. It creates a UDP socket, multicasts a packet to itself, and returns the from address. No error checking.

Brilliant!

PeteE said...
constant  
    setsockopt_ = define_c_func(-1, "setsockopt", {C_INT, C_INT, C_INT, C_POINTER, C_INT}, C_INT) 
     
    sockets:set_option(sock, SOL_SOCKET, SO_REUSEADDR, 1) 
 
    c_func(setsockopt_, {sock[1], IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq, 12}) 

Ouch. Looks like we have a bug in set_option().

new topic     » goto parent     » topic index » view message » categorize

19. Re: ./EuGTK/GtkEngine.e to Irv

jimcbrown said...
PeteE said...
constant  
    setsockopt_ = define_c_func(-1, "setsockopt", {C_INT, C_INT, C_INT, C_POINTER, C_INT}, C_INT) 
     
    sockets:set_option(sock, SOL_SOCKET, SO_REUSEADDR, 1) 
 
    c_func(setsockopt_, {sock[1], IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq, 12}) 

Ouch. Looks like we have a bug in set_option().

I wasn't sure if the missing IP proto options were intentional or a bug. Should I file a ticket?

new topic     » goto parent     » topic index » view message » categorize

20. Re: ./EuGTK/GtkEngine.e to Irv

PeteE said...
jimcbrown said...
PeteE said...
constant  
    setsockopt_ = define_c_func(-1, "setsockopt", {C_INT, C_INT, C_INT, C_POINTER, C_INT}, C_INT) 
     
    sockets:set_option(sock, SOL_SOCKET, SO_REUSEADDR, 1) 
 
    c_func(setsockopt_, {sock[1], IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq, 12}) 

Ouch. Looks like we have a bug in set_option().

I wasn't sure if the missing IP proto options were intentional or a bug. Should I file a ticket?

Yes, please!

new topic     » goto parent     » topic index » view message » categorize

21. Re: ./EuGTK/GtkEngine.e to Irv

Why do you need to set addr to some magic number?

new topic     » goto parent     » topic index » view message » categorize

22. Re: ./EuGTK/GtkEngine.e to Irv

The address is mostly arbitrary, except that in order to be a UDP multicast address, the first octet must be in the range of 224-239.

new topic     » goto parent     » topic index » view message » categorize

23. Re: ./EuGTK/GtkEngine.e to Irv

This is not a problem with this code but when I paste the code in I get the following error:

/home/shawn/development/pkg/wee/EuGTK/GtkEngine.e:8675 in function inet_address()  
variable AF_INET has not been assigned a value  
 
... called from /home/shawn/development/pkg/wee/EuGTK/GtkEngine.e:137  
 
--> See ex.err  

This fails both OpenEuphoria 4.1development and EuphoriaAL 4.1. It is time to make a fix and release EuphoriaAL 4.2.

Update

The code snippet works on 4.0.5. Thank goodness.

Maybe it is better to use ifconfig while this bug is being worked on:

object ip 
object ifconfig_process = pipeio:exec("/sbin/ifconfig", pipeio:create()) 
	if atom(ifconfig_process) then 
		return -1 
	end if 
	sequence ifconfig_data = "" 
	while 1 do 
		sequence chunk = read(ifconfig_process[pipeio:STDOUT],256) 
		if equal(chunk,"") then 
			exit 
		end if 
		ifconfig_data &= chunk 
	end while 
	integer loc = 0 
	sequence address 
	while loc with entry do 
		if begins("inet addr:", ifconfig_data[loc..$]) then 
			loc += length("inet addr:") 
		elsif begins("inet ", ifconfig_data[loc..$]) then 
			loc += length("inet ") 
		end if 
		if not begins("127.0.0.1", ifconfig_data[loc..$]) then 
			address = ifconfig_data[loc..$] 
			integer space_loc = find(' ', address) 
			if space_loc then 
				address = address[1..space_loc-1] 
			end if 
			return address 
		end if 
	entry 
		loc = or_bits(match("inet addr:", ifconfig_data, loc+1), match("inet ", ifconfig_data, loc+1)) 
	end while 
	return -1 
new topic     » goto parent     » topic index » view message » categorize

24. Re: ./EuGTK/GtkEngine.e to Irv

I've experimented with the following to avoid ifconfig: it detects if you are on a network, but cannot detect if there is internet available. I thought it would be easy to just ping google.com, but my 4g connection blocks pings! Anyone else have any ideas?

-------------------------------- 
export function inet_address()  
-------------------------------- 
object ip 
ifdef LINUX then 
	sequence tmp = temp_file(,"MYIP-") 
	if system_exec(sprintf("hostname -I > %s ",{tmp})) = 0 then 
		ip = read_lines(tmp)  
		if equal({""},ip) then  
			if system_exec(sprintf("hostname -i > %s ",{tmp})) = 0 then 
				ip = read_lines(tmp)  
				if equal({""},ip) then return 0  
				else return ip[1] end if 
			end if 
		else return ip[1] 
		end if 
	end if 
end ifdef 
return 0 
end function 
 
--------------------------------- 
export function networked() 
--------------------------------- 
ifdef NET then 
	display("Localhost: [] My IP []",{localhost,inet_address()}) 
end ifdef 
return match("127.0",inet_address()) != 1 
end function 
new topic     » goto parent     » topic index » view message » categorize

25. Re: ./EuGTK/GtkEngine.e to Irv

irv said...

Anyone else have any ideas?

You could try the same method Microsoft uses. blink

Windows 7 Network Awareness: How Windows knows it has an internet connection

-Greg

new topic     » goto parent     » topic index » view message » categorize

26. Re: ./EuGTK/GtkEngine.e to Irv

Here's something that seems to work, but the nslookup takes too long to use it every time you click something. About a 5 second delay. Perhaps there's a faster way, or maybe this could be set up to be called by a timer to check periodically? I'll bet this is what Windows does. Any other ideas?

-------------------------------- 
export function inet_address()  
-------------------------------- 
object ip 
ifdef LINUX then 
	sequence tmp = temp_file(,"MYIP-") 
	if system_exec(sprintf("hostname -I > %s ",{tmp})) = 0 then 
		ip = read_lines(tmp) -- 192.168.0.163 
		if equal({""},ip) then  
			if system_exec(sprintf("hostname -i > %s ",{tmp})) = 0 then 
				ip = read_lines(tmp) -- 127.0.1.1 
				if equal({""},ip) then return 0  
				else return ip[1] end if 
			end if 
		else return ip[1] 
		end if 
	end if 
end ifdef 
return 0 
end function 
 
--------------------------------- 
export function networked() 
--------------------------------- 
return match("127.0",inet_address()) != 1 
end function 
 
--------------------------------- 
export function inet_connected() 
--------------------------------- 
object result 
ifdef LINUX then 
	sequence tmp = temp_file(,"NCSI-") 
	if system_exec(sprintf("nslookup www.msftncsi.com > %s ",{tmp})) = 0 then 
		result = read_lines(tmp) 
		for i  = 1 to length(result) do 
		 if match("www.msftncsi.com",result[i]) = 1 then return TRUE end if 
		end for 
	else 
		display("No internets!") 
	end if 
end ifdef 
return FALSE 
end function 
 

  "Server:\t\t127.0.1.1", 
  "Address:\t127.0.1.1#53", 
  "", 
  "Non-authoritative answer:", 
  "www.msftncsi.com\tcanonical name = www.msftncsi.com.edgesuite.net.", 
  "www.msftncsi.com.edgesuite.net\tcanonical name = a1961.g.akamai.net.", 
  "Name:\ta1961.g.akamai.net", 
  "Address: 23.218.156.105", 
  "Name:\ta1961.g.akamai.net", 
  "Address: 23.218.156.91", 
  "" 

new topic     » goto parent     » topic index » view message » categorize

27. Re: ./EuGTK/GtkEngine.e to Irv

Any reason this won't work?

--------------------------------- 
export function inet_connected() 
--------------------------------- 
return sequence(dns:host_by_name("www.msftncsi.com")) 
end function 
 
new topic     » goto parent     » topic index » view message » categorize

28. Re: ./EuGTK/GtkEngine.e to Irv

irv said...

Any reason this won't work?

--------------------------------- 
export function inet_connected() 
--------------------------------- 
return sequence(dns:host_by_name("www.msftncsi.com")) 
end function 
 

I can't think of one.

new topic     » goto parent     » topic index » view message » categorize

29. Re: ./EuGTK/GtkEngine.e to Irv

The DNS lookup could be done by local resolution, eg by a private intranet server, even the local machine.
So if the lookup was to a name that is absolutely on the outside (on the internet) then it should be safe,
possibly test for private IP's such as 192.168.* and 10.* to make sure its not a local DNS resolution?

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu