Hi there I've been using Nim's foreign function interface to interface with C libs on linux with much joy for some time. I haven't run into any issues until now. I have a utmp object defined in <utmp.h>. I have a simple app that just prints out currently logged in users - similar output to say what the w command does. type utmpx* = object ut_type*:ut_type # Type of record ut_pid*:cint # PID of login process ut_line*:array[UT_LINESIZE,char] # Device name of tty - "/dev/" ut_id*:array[4,char] # Terminal name suffix or inittab(5) ID ut_user*:array[UT_NAMESIZE,char] # Username ut_host*:array[UT_HOSTSIZE,char] # Hostname for remote login, or kernel version for run-level messages ut_session*:cint # Session ID (getsid(2)) ut_tv*:tv # Time entry was made */ ut_addr_v6*:array[4, uint32] # Internet address of remote host; IPv4 address uses just ut_addr_v6[0] Run
The issue appears when trying to read the ip address. The C equivalent is like so const char *addr_string; char buffer[INET6_ADDRSTRLEN]; if (ut->ut_addr_v6[1] || ut->ut_addr_v6[2] || ut->ut_addr_v6[3]) addr_string = inet_ntop(AF_INET6, &(ut->ut_addr_v6), buffer, sizeof(buffer)); else addr_string = inet_ntop(AF_INET, &(ut->ut_addr_v6), buffer, sizeof(buffer)); printf("ip is %s\n",addr_string); Run The nim version returns the wrong IP addresses - struggling to figure out why the difference? var buff: array[0..255, char] ip:cstring if ut.ut_addr_v6[1] == 0 and ut.ut_addr_v6[2] == 0 and ut.ut_addr_v6[3] == 0: let ip = inet_ntop(AF_INET,ut.ut_addr_v6.addr[0].addr, buff[0].addr, buff.sizeof.int32) else: let ip = inet_ntop(AF_INET6, ut.ut_addr_v6.addr[0].addr, buff[0].addr, buff.sizeof.int32) Run