Thread: process socket buffer from binary protocol

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    Question process socket buffer from binary protocol

    hello board,

    i am new here and in c as well. i try myself on network programming(a simple binary protocol), the simple tasks are working so far but i am stuck at progressing a received buffer.

    on the network dump the payload looks like:

    Code:
    7F00000108AE00
    which is: 127.0.0.1 2222 and a null byte. now i would like to process this buffer and i am stuck with my basic c knowledge.

    what i would like to do is fidling the ip address and the port number out of the buffer.

    i found a way for the ip address:
    Code:
    sprintf(new,"%u.%u.%u.%u",buf[0],buf[1],buf[2],buf[3]);
    but i could not figure a way for the port number. as well i wonder what approaches i could have taken as well - as i like to improve my coding skills.

    thank you for your responses,
    Last edited by fudgecode; 10-23-2010 at 06:24 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First big question is, why aren't you working with the sockets library?

    sendto() and recvfrom() will give you everything you need. Both automatically encode and decode IP and port information for you.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    good question why i am not using sendto, i have to read about this function. but this is not solving the point itself. as the send ip address and port was just an example to make myself clear. it could be any kind of binary data. and i am looking for a good way to processing it.

    just imagine instead of ip and port i have:

    Code:
    0912000103
    here we have 5 byte, and lets say i want to get 4 byte and change them to whatever representation i am looking for.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Seriously take a look at sendto() and recvfrom()...

    sendto() takes a char buffer of data and sends it to the specified IP and Port.
    recvfrom() fills a char buffer with data and records the IP and Port of the sender.

    Since you are sending and receiving buffers that are defined in your programs, you are basically working on data in memory. What you actually do to the data is a matter for your own programming... It can be as simple as transferring "Hello" from a string to a string... or as complex as transmitting dynamic configuration information for other programs... The socket calls don't care what's in the buffer... that's up to you.
    Last edited by CommonTater; 10-23-2010 at 07:11 PM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    you seem not to understand my problem. i do not care how the data is send or recieved. what i do care is to process this information. means i want to navigate through a recieved buffer.
    and pack and unpack recieved values.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... perhaps I got off track because you used an IP:Port in your first example... my bad and I apologize.

    The big trick is that you have to know exactly what you've received.

    In your example you want to grab 4 bytes and play with them... what do those 4 bytes represent? Are they a string, an int, a struct of bytes? Unless you know this, you can't really decode their meaning...

    I apologize if I'm being obtuse, that is not the intent... the point is that without knowledge of what's been sent, how it's formatted and what it represents, you can't decode anything.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    no problem, you just trying to help and i lack of explaining what i am looking for.

    now, lets assume i have those a message from a remote system and i know the protocol, so i know the first 4 bytes is the ip, then 2 bytes port, then 20 bytes message. all this data is now in my recieved buffer. for me is the problem now to process it, this means:
    - taking the first 4 bytes of the ip changing them to human readable and print
    - taking the next 2 bytes of the port, changing to human readable and print
    - taking the message out of the buffer, changing to human readable and print.

    well i guess this is basic c things i am looking for, i would say in other languages like perl or python for this u use functions like pack or unpack.

    thank you for helping

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Might I suggest that if you're new to C, going directly to socket programming is a path fraught with difficulty? Maybe you should start with simple string manipulation first?

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    I believe to solve this you have to use byte manipulating operators << >> | ^ ~ & to extract the ip port and message via binray multiplication (&) of the recieved buffer on a binary mask created earlier and storing the answer in another buffer...
    tutorials on using hoffman codes should show you what i mean..
    Sorry i can't describe this more thouroughly, my knowledge of this is exquisitely theoritic.
    Last edited by tty0; 10-24-2010 at 12:54 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by fudgecode View Post
    no problem, you just trying to help and i lack of explaining what i am looking for.

    now, lets assume i have those a message from a remote system and i know the protocol, so i know the first 4 bytes is the ip, then 2 bytes port, then 20 bytes message. all this data is now in my recieved buffer. for me is the problem now to process it, this means:
    - taking the first 4 bytes of the ip changing them to human readable and print
    - taking the next 2 bytes of the port, changing to human readable and print
    - taking the message out of the buffer, changing to human readable and print.

    well i guess this is basic c things i am looking for, i would say in other languages like perl or python for this u use functions like pack or unpack.

    thank you for helping
    Assuming the IP is in machine order and the message is ascii characters...
    Code:
    // the recv buffer (big as you need)
    unsigned char Buff[100]; 
    
    // code to actually receive the message goes here
    
    // display message
    printf("Message from : %d,%d,%d,%d:%d \n",
                          Buff[0],Buff[1],Buff[2],Buff[3], *((uint16_t*)&Buff[4]) );
    
    printf("Message reads: %s \n \n", (char*)&Buff[6] );
    Now please bear in mind this isn't real world code. It's not likely you'll drop this into a procedure and have it work properly. It's only to give you some idea how to extract data from raw buffers.

    As I pointed out earler the "socket" library has built in functions for transferring and decoding reply addresses, so it's very unlikely you will see them in the actual data buffer. What you would most likely get is just the text in which case you can simply print the buffer. The return address etc. would come from the recvfrom() call that fetched the buffer.
    Last edited by CommonTater; 10-24-2010 at 04:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dump the buffer read from binary file
    By r00t in forum C Programming
    Replies: 23
    Last Post: 11-23-2009, 11:51 PM
  2. transfer binary over socket
    By MK27 in forum Networking/Device Communication
    Replies: 30
    Last Post: 10-09-2008, 09:41 AM
  3. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM