# Getting essid via ioctl

**URL:** <https://rubytalk.org/t/getting-essid-via-ioctl/55463>\
**Category:** ruby-talk\
**Created:** [19 September 2009 17:50 UTC](https://rubytalk.org/t/getting-essid-via-ioctl/55463 "2009-09-19T17:50:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoph\_Kappel](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@Christoph\_Kappel](https://rubytalk.org/u/Christoph_Kappel)\
**Post date:** [19 September 2009 17:50 UTC](https://rubytalk.org/t/getting-essid-via-ioctl/55463/1 "2009-09-19T17:50:01Z")

</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:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [22 September 2009 23:58 UTC](https://rubytalk.org/t/getting-essid-via-ioctl/55463/2 "2009-09-22T23:58:46Z")

</div>

Christoph Kappel 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  
> {  
> &nbsp;&nbsp;void \_\_user \*pointer; /\* Pointer to the data (in user space) \*/  
> &nbsp;&nbsp;\_\_u16 length; /\* number of fields or size in bytes \*/  
> &nbsp;&nbsp;\_\_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?

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Christoph\_Kappel](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@Christoph\_Kappel](https://rubytalk.org/u/Christoph_Kappel)\
**Post date:** [23 September 2009 09:27 UTC](https://rubytalk.org/t/getting-essid-via-ioctl/55463/3 "2009-09-23T09:27:56Z")

</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 \<[vjoel@path.berkeley.edu](mailto:vjoel@path.berkeley.edu)\> wrote ----
> 
> \> Christoph Kappel 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  
> \>
