how to analyse the contents of a give IP packet?

hi you all!
I can write a network program to send and receive some messages. I use
read() and write() functions for extracting of sending messages via a given socket. By doing so, i know only the actions performed at the application layer of the TCP/IP suite. But i want to control the actual contents of each IP packets(i.e data and ip header) sent/received.Lets say I want to write a routing application which analyses the destination address found in the IP header(TCP header) and sends the packet to its destination.
Give me some hint on how to tackle such cases or give me a reference book/site...

thank you

You want to use raw sockets.

Search google for "raw sockets" or "SOCK_RAW" or "PF_PACKET"

If you're programming C in Linux, there are structs that easily let you alter the whole tcp/udp/icmp/igmp/whatever header and inject packets at will. This can also be used for communication over closed ports and other obscure stuff (if you create a listening raw socket, you'll have yourself a "sniffer", and with that you can analyze data packets that arrive even on closed tcp ports)

libnet: The Million Packet March