0% found this document useful (0 votes)
23 views5 pages

Exp 7

Uploaded by

airtelcare08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Exp 7

Uploaded by

airtelcare08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Seth Vishambhar Nath Institute of Engineering &

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.

The (simplified) link layer model looks like this:

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.

Computer Network Lab SVNIET, Barabanki


Page 1
Seth Vishambhar Nath Institute of Engineering &
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

Raw sockets programming, on the other hand, allows headers of lower level protocols to be constructed by
the application.

1) Socket Creation

A RAW SOCKET is created as follows:

#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:

TCP: protocol = 0x06

· UDP: protocol = 0x11

· ICMP: protocol = 0x1

NOTES:

IPPROTO_RAW permits to send any type of protocol by the same socket. However, this option does

not permit to receive the "header" of the IP packets.

Computer Network Lab SVNIET, Barabanki


Page 2
Seth Vishambhar Nath Institute of Engineering &
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

· The socket option IP_HDRINCL determine that the IP header is defined by the application. This

option is enabled only for the IPPROTO_RAW socket.

2) Structure of Important Headers

// IP Header: 20 bytes

struct ipheader {

unsigned char ip_hl:4, ip_v:4;

unsigned char ip_tos;

unsigned short int ip_len;

unsigned short int ip_id;

unsigned short int ip_off;

unsigned char ip_ttl;

unsigned char ip_p;

unsigned short int ip_sum;

unsigned int ip_src;

unsigned int ip_dst;

};

Computer Network Lab SVNIET, Barabanki


Page 3
Seth Vishambhar Nath Institute of Engineering &
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

// ICMP Header: 8 bytes

struct icmpheader {

unsigned char icmp_type;

unsigned char icmp_code;

unsigned short int icmp_cksum;

/* Fields dependent on the ICMP type */

unsigned short int icmp_id;

unsigned short int icmp_seq;

};

// UDP Header: 8 bytes

struct udpheader {

unsigned short int uh_sport;

unsigned short int uh_dport;

unsigned short int uh_len;

unsigned short int uh_check;

Computer Network Lab SVNIET, Barabanki


Page 4
Seth Vishambhar Nath Institute of Engineering &
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

};

// TCP Header: 20 bytes

struct tcpheader {

unsigned short int th_sport; unsigned short int th_dport; unsigned int th_seq; unsigned int th_ack;

unsigned char th_x2:4, th_off:4;

unsigned char th_flags;

unsigned short int th_win;

unsigned short int th_sum; unsigned short int th_urp;

Computer Network Lab SVNIET, Barabanki


Page 5

You might also like