Exp 7
Exp 7
Technology
Affiliated to Dr. A.P.J. Abdul Kalam Technical University, Lucknow, Uttar Pradesh Approved by
AICTE, New Delhi
Lucknow-Barabanki Highway, Safedabad, Barabanki (U.P.) - 225003 Website:
http://www.svn.ac.in
Experiment-7
Aim- Programming using raw sockets
Raw sockets:
The basic concept of low level sockets is to send a single packet at one time, with all the protocol
headers filled in by the program (instead of the kernel).Unix provides two kinds of sockets that permit
direct access to the network. One is SOCK_PACKET, which receives and sends data on the device link
layer. This means, the NIC specific header is included in the data that will be written or read. For most
networks, this is the Ethernet header. Of course, all subsequent protocol headers will also be included in the
data. The socket type we’ll be using, however, is SOCK_RAW, which includes the IP headers and all
subsequent protocol headers and data.
Physical layer -> Device layer (Ethernet protocol) -> Network layer (IP) ->Transport layer (TCP, UDP,
ICMP) -> Session layer (application specific data
Programming using TCP or UDP implies that only the application protocol header and data are provided by
the application.
The headers of IP , TCP or UDP protocols are automatically created by the O.S, using information
provided by the application ( IP address, port numbers and protocol family) . The O.S. uses default values
to fields such as TTL. It is up to the O.S. to compute the checksum, when present.
Raw sockets programming, on the other hand, allows headers of lower level protocols to be constructed by
the application.
1) Socket Creation
#include <sys/socket.h>
#include <netinet/in.h>
raw_socket = socket(PF_INET, SOCK_RAW, int protocol);
Protocol is a protocol number (RFC 1700) that identifies the protocol transported in the payload of the IP
packet.
Proocol is a filter used to select which packets are received by the socket.
Example:
NOTES:
IPPROTO_RAW permits to send any type of protocol by the same socket. However, this option does
· The socket option IP_HDRINCL determine that the IP header is defined by the application. This
// IP Header: 20 bytes
struct ipheader {
};
struct icmpheader {
};
struct udpheader {
};
struct tcpheader {
unsigned short int th_sport; unsigned short int th_dport; unsigned int th_seq; unsigned int th_ack;