-
bcopy() question
hi
If I build ICMP echo request header and put it in char array by copying it with bcopy(&icmp_h, buf, sizeof(icmp_h)),then server won't send back echo reply.
I know there are other ways to put icmp header in char array,but I'd like to know why I can't use bcopy().From what I understand,it copies number of bytes and it doesn't care what type those bytes represent(it just stores them in same format to destination).
I don't see any difference between the way the following code stores bytes into buf and how bcopy() stores them,but this seems to do the trick while bcopy() causes server to not send back reply
PHP Code:
char buf[100];
struct icmp *ICMP=(struct icmp *) buf;
ICMP->icmp_type=8;...
thank you
-
How about posting a reasonable sized chunk of code which you tried, rather than a couple of lines of stuff which no-one can fault.
-
Will this do?
PHP Code:
int len; //length of ICMP
struct icmp ICMP;
int id=getpid();
ICMP.icmp_type=8;
ICMP.icmp_code=0;
ICMP.icmp_id=id;
ICMP.icmp_seq=seq++;
ICMP.icmp_cksum=0;
bcopy(&ICMP, buf, sizeof(ICMP));
len=8;
ICMP.icmp_cksum=do_cksum(len);
int n=sendto(sockR, sendbuf, len, 0, (struct sockaddr *) &serv, sizeof(serv));
ICMP always gets send(checked it with tcpdump)
If I use the following code server sends echo reply
PHP Code:
int id=getpid();
struct icmp *ICMP=(struct icmp *) buf;
ICMP->icmp_type=8;
ICMP->icmp_code=0;
ICMP->icmp_id=id;
ICMP->icmp_seq=16;
ICMP->icmp_cksum=0;
len=8;
ICMP->icmp_cksum=do_cksum(len);
int n=sendto(sockR, sendbuf, len, 0, (struct sockaddr *) &serv, sizeof(serv));
-
Are you sure that's the right code? You've got buf in one place and sendbuf in another.
-
It is.Code is writen on linux and I'm using windows when surfing the net,so I had to retype it.Sorry for the typo
-
> ICMP.icmp_cksum=do_cksum(len);
Heh, how about doing this BEFORE you bcopy it to somewhere else.
One more thing, bcopy() is old, use memcpy() or memmove()
Linux has plenty of non-IE browsers, how about using one of those when visiting programming sites.
-
thank you
PS:I use IE cos I use Windows for browsing the net