Sending/Recieving structures in UNIX
Posted on 2005-04-19
I am trying to send and receive structures that contain, char buffers inside of the structure between my kernel code and my user land/process. My problem lies in that I am passing a structure that has some pointers to buffers within this structure and these buffers are always showing empty/zeros when I receive them on the userland side.
I get all the correct values for ints/booleans and etc. that are in my structure but the _MALLOCed buffers, from the kernel extension are empty. I suspect it has to do something with the way I filling out the mbuf because like I said I can receive the struct on the userland side, the pointers seem good to the buffers with in the structure but these buffers are just empty, zeroed out.
Maybe if someone can take a look at the code below they have some suggestions or see something I do not.
The code from the kernel side is:
struct Test_Struct *test;
test = _MALLOC(sizeof(struct Test_Struct), M_TEMP, M_WIATOK|M_ZERO);
if((test->buf1 = _MALLOC(50, M_TEMP, M_WAITOK|M_ZERO)) != 0)
bcopy("Here comes the test data", test->buf1, 20);
test->n1 = 66;
test->n2 = 678;
// now send the struct to user land
struct mbuf *m;
size_t len = sizeof(Test_Struct);
if(len > MCLBYTES)
if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
m->m_flags |= M_EOR;
if(!(m->m_flags & M_EXT))
bcopy(test, mtod(m, void*), len);
m->m_pkthdr.len = m->m_len=len;
ctl_enqueuembuf(ctlref, m, flags); // this is a call equivalent to send(nfd, void*, int), a Mac OS X thing, don't worry about it, it works.
// ctlref is a pointer to previously registered controller, that is hooked to this kernel extension
Below is a sample of the code in userland that receives the data from ctl_enqueuembuf:
struct Test_Struct teststruct;
nBytesRc = recv(nfd, &teststruct, sizeof(teststruct), 0);
if(nBytesRc > 0)
// do what every with teststruct.buf1; and other members of the received struct.
This is where I can look at the teststruct and buf1 is always empty.