Forum: Ruby How can I get the local machine's netmask programmatically?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
923f770d7b487a469429a8f77319ab4e?d=identicon&s=25 wolfram (Guest)
on 2007-06-13 22:31
(Received via mailing list)
I'm working on a Rails app that needs to restrict certain privileged
operations/requests to only those requests that originate from within
the local subnet.

How can I find out the machine's netmask programmatically?

Thanks,

Wolf
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jano Svitok (Guest)
on 2007-06-13 23:13
(Received via mailing list)
On 6/13/07, wolfram <wolframarnold@gmail.com> wrote:
> I'm working on a Rails app that needs to restrict certain privileged
> operations/requests to only those requests that originate from within
> the local subnet.
>
> How can I find out the machine's netmask programmatically?

Hi,

what operating system? windows/linux(distribution?)/osx?
how many network adapters are in the machine?

on windows use `ipconfig /all` and parse output or WMI through Win32OLE
on unix `/sbin/ifconfig -a`

If there are more adapters, you have to choose the right one (or allow
all local nets).

J.
923f770d7b487a469429a8f77319ab4e?d=identicon&s=25 wolfram (Guest)
on 2007-06-14 00:45
(Received via mailing list)
> what operating system? windows/linux(distribution?)/osx?
> how many network adapters are in the machine?

Linux.

> on windows use `ipconfig /all` and parse output or WMI through Win32OLE
> on unix `/sbin/ifconfig -a`

I know about the system commands.  Is there a library/API to get these
programmatically without having to parse output from a sytem command?

Thanks,
W.
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2007-06-14 01:30
(Received via mailing list)
On Thu, 2007-06-14 at 07:45 +0900, wolfram wrote:
>
> Thanks,
> W.
>
Create a socket and pull it from the socket structure.
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2007-06-14 03:19
(Received via mailing list)
On Thu, 2007-06-14 at 08:30 +0900, Reid Thompson wrote:
> > programmatically without having to parse output from a sytem command?
> >
> > Thanks,
> > W.
> >
> Create a socket and pull it from the socket structure.
>
see if this works....????

rthompso@shienar ~ $ cat getnetmask.rb
require 'rubygems'
require "inline"

class NetMask
  inline do |builder|
    builder.include '<sys/types.h>'
    builder.include '<sys/socket.h>'
    builder.include '<sys/ioctl.h>'
    builder.include '<netinet/in.h>'
    builder.include '<net/if.h>'
    builder.c "

    char * nmask() {
        int fd;
        struct ifreq ifr;

        fd = socket(AF_INET, SOCK_DGRAM, 0);
        ifr.ifr_addr.sa_family = AF_INET;
        strncpy(ifr.ifr_name, \"eth0\", IFNAMSIZ-1);
        ioctl(fd, SIOCGIFNETMASK, &ifr);
        close(fd);

        return (char *)inet_ntoa(((struct sockaddr_in
*)&ifr.ifr_addr)->sin_addr);
  }"
  end
end

nm = NetMask.new()
nmaa = nm.nmask()
puts nmaa

rthompso@shienar ~ $ ruby getnetmask.rb
255.255.255.0
990bf71a4e84e1145a3131f35656dc18?d=identicon&s=25 List Rb (listx300108u79872)
on 2007-06-14 18:49
(Received via mailing list)
I spent quite a bit of time last night trying to figure this out to no
avail.

Reid, how long did it take you to come up with this? Could anyone
without C experience figured this out?
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2007-06-14 19:22
(Received via mailing list)
On Fri, 2007-06-15 at 01:48 +0900, list.rb@gmail.com wrote:
> > require 'rubygems'
> >
> >         return (char *)inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
>
1) I was previously aware of RubyInline  -- so didn't have to
'find/research' it

2) I knew that getting the info in C code was doable
3) I googled for examples of getting socket info
4) it took me about 15 minutes probably

4a) I think a non-C programmer could have figured it out in time
I basically copied the C RubyInline example ( the factorial one )
and pasted in the socket code.

The gotcha's would probably have been
    a)figuring out to use the builder.include, described in the C++
example, to get the header files included
    b)getting rid of a warning message due to the original C code not
casting to (char *) the return value of inet_ntoa()

5) Ruby's various socket classes probably provide access to the same
info - but I couldn't find out how to get at it ( like you, I spent
waaaay more time trying to find a pure ruby way of getting the info than
coding the example )  i.e. Ruby's Socket class has a getsockname which
returns the struct sockaddr packed into a string -- It may have all the
info needed, but I'm not familiar enough with unpacking a sockaddr
structure to try to figure it out ( or unpacking anything in Ruby for
that matter :) ).
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2007-06-14 19:44
(Received via mailing list)
On Fri, 2007-06-15 at 01:48 +0900, list.rb@gmail.com wrote:
> > > > > how many network adapters are in the machine?
> > > > W.
> >   inline do |builder|
> >
> >
> > nm = NetMask.new()
> > nmaa = nm.nmask()
> > puts nmaa
> >
> > rthompso@shienar ~ $ ruby getnetmask.rb
> > 255.255.255.0
>

http://www.hashcode.eti.br/?p=46 makes an interesting read also
This topic is locked and can not be replied to.