[ctypes-users] Strings with null characters
Brought to you by:
theller
|
From: Dominik G. <de...@na...> - 2011-06-26 13:35:04
|
Hi, I am using ctypes while porting parts of glibc to pure Python (i.e. to be able to do ifconfig from within Python). The complete code can be found in my Redmine under http://git.proparity.net/redmine/projects/pyverkko/repository/revisions/master/show/src/pyverkko/ifconfig . In structs.py, I have defined the structs used by glibc to do ioctl(), which works perfectly in most cases. But, this breaks when I have to cope with null bytes. For example, set_ip_address("eth0", "192.168.0.1") breaks, because: I create an isntance of sockaddr, which is a ctypes Structure (or, a PackableStructure, which is a ctypes Structure to which I added a pack() method as described in the FAQ). Now, assigning the binary form of 192.168.0.1 to a member field of sockaddr, breaks because it is expecting a null-termianted string, so the string I assign is only the first two bytes because the third octet, 0, terminates the string. I am not sure if Python does this while assigning, because the field sa_data I am assigning to is declared as a char[14]. How can I take care that *all* bytes of a string are stored in the Structure and recognized when I run pack(), i.e. strings aren't cut off at the first null byte? Regards, Nik |