bcopy() question

This is a discussion on bcopy() question within the C Programming forums, part of the General Programming Boards category; hi If I build ICMP echo request header and put it in char array by copying it with bcopy(&icmp_h, buf, ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    21

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    21
    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(&ICMPbufsizeof(ICMP));

                                                                                                     
       
    len=8;
       
    ICMP.icmp_cksum=do_cksum(len);                                                                                       
                                                                                                     
       
    int n=sendto(sockRsendbuflen0, (struct sockaddr *) &servsizeof(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(sockRsendbuflen0, (struct sockaddr *) &servsizeof(serv)); 
    Last edited by failure_to; 06-03-2004 at 06:31 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Are you sure that's the right code? You've got buf in one place and sendbuf in another.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    21
    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

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    21
    thank you

    PS:I use IE cos I use Windows for browsing the net

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21