hi,
I wrote a simple script that include some netlink structs and had a problem
this is build_nl.py:
from cffi import FFI
ffibuilder = FFI()
header = """
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> /* for sa_family_t */
#include <linux/inet_diag.h>
#include <linux/netlink.h>
"""
struct = """
typedef struct {
struct nlmsghdr nlh;
struct inet_diag_req diag_req;
}nlreq_t;
/* Creates a new netlink request. */
nlreq_t *nlreq_create();
/* Destroys a netlink request */
void nlreq_destroy(nlreq_t *req);
"""
ffibuilder.set_source("_netlink", header + struct + """
nlreq_t *nlreq_create() {
nlreq_t *req = malloc(sizeof(nlreq_t));
// set netlink message header
req->nlh.nlmsg_len = sizeof(nlreq_t);
req->nlh.nlmsg_type = TCPDIAG_GETSOCK;
req->nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP;
req->nlh.nlmsg_seq = 1;
// set payload
req->diag_req.idiag_family = AF_INET;
req->diag_req.idiag_states = (1 << 10); /* TCP_LISTEN */
req->diag_req.id.idiag_cookie[0] = INET_DIAG_NOCOOKIE; /* -1 */
req->diag_req.id.idiag_cookie[1] = INET_DIAG_NOCOOKIE;
return req;
};
void nlreq_destroy(nlreq_t *req) {
if(&req->nlh)
free(&req->nlh);
if(&req->diag_req)
free(&req->diag_req);
free(req);
};
""")
ffibuilder.cdef(struct)
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
and this is nl.py:
from _netlink import ffi, lib
nlreq = ffi.new("nlreq_t *")
print nlreq.nlh
when I run python nl.py I got this error:
ffi.error: nlreq_t: wrong size for field 'nlh' (cdef says -1, but C compiler says 16). fix it or use "...;" in the cdef for nlreq_t to make it flexible
I am not familiar with cffi, could someone explain for "cdef says -1",why sizeof nlsmghr is -1。