On Thu, Nov 29, 2012 at 11:05 PM, Mark Kettenis <[email protected]> wrote:
>> #!/usr/bin/perl
>> require "sys/ioctl.ph";
>> $TUNSIFUNIT = _IOC(&IOC_INOUT, ord('t'), 90, 4);
>> open(TUN0, "+</dev/tun0") or die "open";
>> ioctl(TUN0, $TUNSIFUNIT, $unit = pack("i", -1)) or die "ioctl $!";
>> print "Returned: tun".unpack("i", $unit)."\n";
>> close(TUN0);
>
> But you'll need to be a pretty hardcore Perl programmer to be able do this.
>

Thanks, Mark, I take this as a compliment :-) But I don't see myself
as a hardcore Perl programmer.

> My favourite "quick-and-dirty" language is Python, and I consider
> myself a fairly proficient Python programmer.  I've never, ever, done
> ioctls from Python, and while it looks indeed as if it is possible to
> do so, I'm sure it'd take me some effort to get it right.

I'm not a Python programmer at all, but doing ioctls there is not
hard. Same thing: you have to master pack/unpack and call the ioctl
function. But with your experience in Python, I'm sure that you know
how to find API functions in the documentation. Google actually shows
me lots of hints about using ioctl with python.

The only problem with all these scripting languages is, that you have
to get the ioctl number which is usually constructed with the _IO*
macros in the C headers. If you don't have them, you have to figure
out the hex number. And I have to admit that this is complicated and
hard for non-C hackers. Some people just seem to include a pythonized
version of the _IO* macros to construct the ioctl numbers, but this is
most probably platform-specific code.

#!/usr/local/bin/python
import fcntl
import struct
file = open("/dev/tun0")
unit = struct.pack("i", -1)
ret = fcntl.ioctl(file, 0xc004745a, unit)
print struct.unpack("i", ret)

OK, let's find another way without an extra ioctl or keep tun as it is.

Reyk

Reply via email to