Reputation: 33
I want to send a array of data to kernel space , ( i have used call back function in my kext) problem is when i use send function i see something weird that i explain in 2 scenario: 1) ... char f[]={'1','2','3','4','5','6'}; send (sock,f,sizeof(f),0); well, when i printf what i receive in kext: 123456
2) ... // i replace f[2] with 0
char f[]={'1','2',0,'4','5','6'}; send (sock,f,sizeof(f),0);
but this time, when i printf what i receive in kext: 120000
it seems that send function make zero every byte after first 0 byte? what is going on? is this a send function bug? i used xcode 4.1 and i my os is lion here is user space part:
int main(int argc, char* const*argv)
{
struct ctl_info ctl_info;
struct sockaddr_ctl sc;
char str[MAX_STRING_LEN];
int sock = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
if (sock < 0)
return -1;
bzero(&ctl_info, sizeof(struct ctl_info));
strcpy(ctl_info.ctl_name, "pana.ifmonitor.nke.foo");
if (ioctl(sock, CTLIOCGINFO, &ctl_info) == -1)
return -1;
bzero(&sc, sizeof(struct sockaddr_ctl));
sc.sc_len = sizeof(struct sockaddr_ctl);
sc.sc_family = AF_SYSTEM;
sc.ss_sysaddr = SYSPROTO_CONTROL;
sc.sc_id = ctl_info.ctl_id;
sc.sc_unit = 0;
if (connect(sock, (struct sockaddr *)&sc, sizeof(struct sockaddr_ctl)))
return -1;
unsigned char data_send[]={'a','l','i','0','1','2','4','l','i',0,'1','2','4','l','i','0','1'};
size_t data_recive;
int j=0;
char data_rcv[8192];
send( sock, data_send, 17*sizeof(char), 10 );
printf("\n");
sleep(1);
close(sock);
return 0;
}
and this is some part of kernel space code that is responsible for getting user space data:
errno_t EPHandleWrite(kern_ctl_ref ctlref, unsigned int unit, void *userdata,mbuf_t m, int flags)
{
printf("\n EPHandleWrite called---------------------- \n");
//char data_rec[50];
//unsigned char *ptr = (unsigned char*)mbuf_data(m);
//char ch;
//mbuf_copydata(m, 0, 50, data_rec);
//strncpy(&ch, ptr, 1 );
size_t data_lenght;
data_lenght = mbuf_pkthdr_len(m);
char data_receive[data_lenght];
strncpy( data_receive, ( char * ) mbuf_data(m) , data_lenght );
printf("data recied %lu\n",data_lenght);
for(int i=0;i<data_lenght;++i)
{
printf("%X ",data_receive[i]);
}
return 0
}
well, it print in console:
61 6C 69 30 31 32 34 6C 69 0 0 0 0 0 0 0 0
and when i change send data to:
{'a','l','i','0','1','2','4','l','i',**'0'**,'1','2','4','l','i','0','1'};
i get correct, in fact i get all 0 after first zero byte in send data
Upvotes: 3
Views: 773
Reputation: 23428
The problem is the strncpy
line - if you look at the documentation for strncpy
, you'll notice that it only copies until it reaches a 0 byte, so it's only suitable for dealing with C strings. If you need to copy arbitrary binary data, use memcpy.
Upvotes: 1