Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:56:30+00:00 2026-06-18T12:56:30+00:00

I was working on a mini module to have low level network interface access

  • 0

I was working on a mini module to have low level network interface access for my university assignmets (not that this is an actual assignment, just to clarify) in python. The actual assignment was done in C, but after that I decided to get some practice with ctypes and started the lib, targeting Linux and Python 2.7.

I already know that Python 3.3 exposes a lot of the functionality I’ve tried to cover, and that it already has a fcntl.ioctl module since arround version 2.3 or so (it does not really matter the exact version, just that it is present in 2.7), but this task was meant for learning.

The issue I ran into is very simple. I’ve got the following classes defined, based on the layout of the C counterparts( for the sake of clarity, bear with the long snippet ):

class struct_ifmap(Structure):
    _fields_ = [
        ('mem_start', c_ulong),
        ('mem_end', c_ulong),
        ('base_addr', c_ushort),
        ('irq', c_ubyte),
        ('dma', c_ubyte),
        ('port', c_ubyte),]

class union_ifreq_anon(Union):
    _fields_ = [
        ('ifr_addr', struct_sockaddr), 
        ('ifr_dstaddr', struct_sockaddr),
        ('ifr_broadaddr', struct_sockaddr),
        ('ifr_netmask', struct_sockaddr),
        ('ifr_hwaddr', struct_sockaddr),
        ('ifr_flags', c_short),
        ('ifr_ifindex', c_int),
        ('ifr_metric', c_int),
        ('ifr_mtu', c_int),
        ('ifr_map', struct_ifmap),
        ('ifr_slave', c_char),
        ('ifr_newname', c_char),
        ('ifr_data', c_char_p),
        ('raw', c_short * 15),]

class struct_ifreq(Structure):
    __IFNAMSIZ = 16
    _anonymous_ = [
        ('u'),]
    _fields_ = [
        ('ifr_name', c_char * __IFNAMSIZ),
        ('u', union_ifreq_anon),]

And the following function:

def _getfahwdraddr(name):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
        socket.IPPROTO_IP)
    ifr = struct_ifreq()
    ifr.ifr_name = name
    result = _libc.ioctl(sock.fileno(), 0x8927, pointer(ifr))
    if result != 0:
        raise OSError(get_errno())
    return ''.join(['%02x:' % pair for pair in \
ifr.ifr_hwaddr.sa_data[0:6]])[:-1]

The output that this gives is -6c:39:-1b:XX:XX:XX, wich does not correspond with the real MAC, that is 94:39:e5:XX:XX:XX. One clue may be that some elements are correct, but as you can see, the output is just wrong. I can not figure why.

Surprisingly, I found a similar question in SO that aims to do almost the same, but uses packed structures, and python’s own ioctl. It just works: Getting MAC Address. Still, my problem is how to correctly represent and use ctype Structures.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:56:32+00:00Added an answer on June 18, 2026 at 12:56 pm

    You’ve got the right answer, you’re just not formatting it right:

    -6c:39:-1b:XX:XX:XX
    

    That’s displaying signed bytes. The result you want is this:

    94:39:e5:XX:XX:XX
    

    That’s the exact same values as unsigned bytes. Compare:

    >>> hex(0x100-0x6c)
    '0x94'
    >>> hex(0x100-0x1b)
    '0xe5'
    

    You didn’t show how you’re defining struct_sockaddr. You could fix it there, by just using an unsigned type, like c_uint8 * 14 in place of c_char * 14.

    Alternatively, you can cast while formatting:

    return ''.join(['%02x:' % c_uint8(pair).value for pair in
                    ifr.ifr_hwaddr.sa_data[0:6]])[:-1]
    

    Or 30 other ways to skin the same problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a mini windows process explorer in C, I have a handle
Hi there I have this confirmable button directive which I am working on, The
I'm working on a kind of dashboard mini site that has blocks with a
I failed to create a mini-library with some useful functions that I have found
I have installed xCode 4.0.2 on my mac mini, every thing is working fine,
I'm working on developing a mini-language in Python (not really, just a few commands
This time I am working on a help-window mini-application (to include in the other
I've been working on a very simple Jquery script that displays a few mini-dashboards
So i have to come up with some kind of mini script that behaves
I have installed Mini-Profiler on my MVC 4 application and it is working like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.