# Getting essid via ioctl

**URL:** <https://www.ruby-forum.com/t/getting-essid-via-ioctl/175789>\
**Category:** Ruby\
**Created:** [September 19, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/getting-essid-via-ioctl/175789 "2009-09-19T19:50:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![corck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@corck](https://www.ruby-forum.com/u/corck)\
**Post date:** [September 19, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/getting-essid-via-ioctl/175789/1 "2009-09-19T19:50:43Z")

</div>

Hello.

To avoid relying on the wireless tools I want to get the essid directly  
from the device with ioctl, in C  
this wouldn’t be a problem, but in Ruby it’s quite different.

The problem is following struct from wireless.h that is used as  
input/reply of ioctl:

struct Â iw\_point  
{  
Â void \_\_user _pointer; /_ Pointer to the data Â (in user space) _/  
Â \_\_u16 Â length; Â /_ number of fields or size in bytes _/  
Â \_\_u16 Â flags; Â Â /_ Optional params \*/  
};

The pointer part must be a valid address of a memory area, followed by  
the length in bytes,  
followed by a flag field. I tried with Array#pack and the bit-struct  
gem, but haven’t found a solution yet.

Is there a way to bypass this memory pointer problem?

Wbr

Christoph

---

<div class="post-metadata">

**Author:** ![corck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@corck](https://www.ruby-forum.com/u/corck)\
**Post date:** [September 23, 2009, 1:59am UTC](https://www.ruby-forum.com/t/getting-essid-via-ioctl/175789/2 "2009-09-23T01:59:32Z")

</div>

Christoph K. wrote:

> \_\_u16 length; /\* number of fields or size in bytes _/  
> \_\_u16 flags; /_ Optional params \*/  
> };
> 
> The pointer part must be a valid address of a memory area, followed by the length in bytes,  
> followed by a flag field. I tried with Array#pack and the bit-struct gem, but haven’t found a solution yet.
> 
> Is there a way to bypass this memory pointer problem?

Christoph,

How are you calling ioctl() ? If you’re using FFI, then googling for  
FFI::Pointer should help. If you’re calling it in some other way and are  
trying to access the binary data in ruby, does the P specifier in  
pack/unpack work?

---

<div class="post-metadata">

**Author:** ![corck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@corck](https://www.ruby-forum.com/u/corck)\
**Post date:** [September 23, 2009, 11:28am UTC](https://www.ruby-forum.com/t/getting-essid-via-ioctl/175789/3 "2009-09-23T11:28:42Z")

</div>

Hey Joel,

thanks for both hints - FFI looks interesting but I don’t want to wrap a  
library.  
Funny that I missed the ‘p’ of pack - it’s working now:

require “socket”

# Copied from wireless.h

SIOCGIWESSID = 0x8B1B  
IW\_ESSID\_MAX\_SIZE = 32

iwreq = [ “wlan0”, " " \* IW\_ESSID\_MAX\_SIZE, IW\_ESSID\_MAX\_SIZE, 0  
].pack(“a16pII”)

sock = Socket.new(Socket::AF\_INET, Socket::SOCK\_DGRAM, 0)

sock.ioctl(SIOCGIWESSID, iwreq)

interface, essid, len, flags = iwreq.unpack(“a16pII”)

puts essid

Now I can check if I can fetch the max. link quality via ioctl too.

Wbr, Christoph

---- On Tue, 22 Sep 2009 16:58:46 -0700 Joel VanderWerf  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote ----

> Christoph K. wrote:
> 
> > Hello.
> > 
> > To avoid relying on the wireless tools I want to get the essid  
> > directly from the device with ioctl, in C  
> > this wouldn’t be a problem, but in Ruby it’s quite different.
> > 
> > The problem is following struct from wireless.h that is used as  
> > input/reply of ioctl:
> > 
> > struct iw\_point  
> > {  
> > void \_\_user _pointer; /_ Pointer to the data (in user space) _/  
> > \_\_u16 length; /_ number of fields or size in bytes _/  
> > \_\_u16 flags; /_ Optional params \*/  
> > };
> > 
> > The pointer part must be a valid address of a memory area, followed  
> > by the length in bytes,  
> > followed by a flag field. I tried with Array#pack and the  
> > bit-struct gem, but haven’t found a solution yet.
> > 
> > Is there a way to bypass this memory pointer problem?
> 
> Christoph,
> 
> How are you calling ioctl() ? If you’re using FFI, then googling for  
> FFI::Pointer should help. If you’re calling it in some other way and  
> are  
> trying to access the binary data in ruby, does the P specifier in  
> pack/unpack work?
> 
> –  
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
