SlideShare ist ein Scribd-Unternehmen logo
1 von 23
cs423 - cotter 1
What are Raw Sockets?
1.A way to pass information to network
protocols other than TCP or UDP (e.g.
ICMP and IGMP)
2.A way to implement new IPv4 protocols
3.A way to build our own packets (be careful
here)
cs423 - cotter 2
Why Would We Use Them?
• Allows us to access packets sent over protocols
other than TCP / UDP
• Allows us to process IPv4 protocols in user
space
– Control, speed, troubleshooting
• Allow us to implement new IPv4 protocols
• Allows us to control the IP header
– Control option fields (beyond setsockopt() )
– Test / control packet fragmentation
cs423 - cotter 3
Limitations?
• Reliability Loss
• No Ports
• Nonstandard communication
• No Automatic ICMP
• Raw TCP / UDP unlikely
• Requires root / admin
cs423 - cotter 4
OS Involvement in Sockets
User Space Kernel Space
Socket App TCP/IP StackLinux
Socket ( AF_INET,
SOCK_STREAM,
IPPROTO_TCP)
Socket ( AF_INET,
SOCK_RAW,
IPPROTO_ICMP)
Socket ( AF_PACKET,
SOCK_RAW,
htons(ETH_P_IP))
Identify
Socket Type
Identify
Socket Type
Identify
Socket Type
TCP
IP
Ethernet
cs423 - cotter 5
Normal Socket Operation (TCP)
• Create a socket
– s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)
• Bind to a port (optional)
– Identify local IP and port desired and create data structure
– bind (s, (struct sockaddr *) &sin, sizeof(sin))
• Establish a connection to server
– Identify server IP and port
– connect (s, (struct sockaddr *) &sin, sizeof(sin))
• Send / Receive data
– Place data to be send into buffer
– recv (s, buf, strlen(buf), 0);
cs423 - cotter 6
Normal Socket Operation (TCP)
User Space Kernel Space
Socket App ProtocolLinux
socket ( ) Create socket
TCP, IP, Internet
connect( )
Bind to local port:
Connect to remote port
send( ) TCP, IP, InternetPass data thru local
stack to remote port
OK
OK
OK
TCP
cs423 - cotter 7
Raw Sockets Operation (ICMP)
• Create a socket
– s = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP)
• Since there is no port, there is no bind *
• There is no TCP, so no connection *
• Send / Receive data
– Place data to be sent into buffer
– sendto (s, buf, strlen(buf), 0, addr, &len);
* More later
cs423 - cotter 8
Raw Sockets Operation (ICMP)
User Space Kernel Space
Socket App ProtocolLinux
socket ( ) Create socket
sendto( ) IP, InternetPass data thru local
stack to remote host
OK
OK
ICMP
cs423 - cotter 9
Create a Raw Socket
• s = socket (AF_INET, SOCK_RAW, protocol)
– IPPROTO_ICMP, IPPROTO_IP, etc.
• Can create our own IP header if we wish
– const int on = 1;
– setsockopt (s, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on));
• Can “bind”
– Since we have no port, the only effect is to associate a local IP
address with the raw socket. (useful if there are multiple local
IP addrs and we want to use only 1).
• Can “connect”
– Again, since we have no TCP, we have no connection. The
only effect is to associate a remote IP address with this socket.
cs423 - cotter 10
Raw Socket Output
• Normal output performed using sendto or sendmsg.
– Write or send can be used if the socket has been connected
• If IP_HDRINCL not set, starting addr of the data (buf)
specifies the first byte following the IP header that the kernel
will build.
– Size only includes the data above the IP header.
• If IP_HDRINCL is set, the starting addr of the data identifies
the first byte of the IP header.
– Size includes the IP header
– Set IP id field to 0 (tells kernel to set this field)
– Kernel will calculate IP checksum
• Kernel can fragment raw packets exceeding outgoing MTU
cs423 - cotter 11
Raw Socket Input
• Received TCP / UDP NEVER passed to a raw socket.
• Most ICMP packets are passed to a raw socket
– (Some exceptions for Berkeley-derived implementations)
• All IGMP packets are passed to a raw socket
• All IP datagrams with a protocol field that the kernel does
not understand (process) are passed to a raw socket.
• If packet has been fragmented, packet is reassembled
before being passed to raw socket
cs423 - cotter 12
Conditions that include / exclude
passing to specific raw sockets
• If a nonzero protocol is specified when raw
socket is created, datagram protocol must
match
• If raw socket is bound to a specific local
IP, then destination IP must match
• If raw socket is “connected” to a foreign IP
address, then the source IP address must
match
cs423 - cotter 13
Ping – Overview
• This example modified from code by Walton (Ch 18)
• Very simple program that uses ICMP to send a ping to
another machine over the Internet.
• Provides the option to send a defined number of packets
(or will send a default 25).
• We will build an ICMP packet (with a proper header,
including checksum) that will be updated each time we
send a new packet.
• We will display the raw packet that is received back from
our destination host and will interpret some of the data.
– (Output format is different from standard ping)
cs423 - cotter 14
ICMP Packet header
struct icmphdr {
u_int8_t type // ICMP message type (0)
u_int8_t code // ICMP type sub-code (0)
u_int16_t checksum E306, etc.
u_int16_t id // echo datagram id (use pid)
u_int16_t sequence // echo seq # 1, 2, 3, etc.
};
Packet body:
0 1 2 3 4 5 6 7 8 9 : ; < = > ? … B
cs423 - cotter 15
myNuPing.c (overview)
• Global Declarations
– Struct packet { }, some variables
• unsigned short checksum (void *b, int len)
– Calculate checksum for ICMP packet (header and data)
• void display (void *buf, int bytes)
– Format a received packet for display.
• void listener (void)
– Separate process to capture responses to pings
• void ping (struct sockaddr_in *addr)
– Create socket and send out pings 1/sec to specified IP addr
• int main (int count, shar *strings[ ])
– Test for valid instantiation, create addr structure
– Fork a separate process (listener) and use existing process for ping
cs423 - cotter 16
#defines and checksum calc
#define PACKETSIZE 64
struct packet {
struct icmphdr hdr;
char msg[PACKETSIZE-sizeof(struct icmphdr)];
};
int pid=-1;
int loops = 25;
struct protoent *proto=NULL;
unsigned short checksum(void *b, int len) {
unsigned short *buf = b;
unsigned int sum=0;
unsigned short result;
for ( sum = 0; len > 1; len -= 2 )
sum += *buf++;
if ( len == 1 )
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
cs423 - cotter 17
display - present echo info
void display(void *buf, int bytes) {
int i;
struct iphdr *ip = buf;
struct icmphdr *icmp = buf+ip->ihl*4;
printf("----------------n");
for ( i = 0; i < bytes; i++ ) {
if ( !(i & 15) ) printf("n%04X: ", i);
printf("%02X ", ((unsigned char*)buf)[i]);
}
printf("n");
printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s
",
ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol,
ip->ttl, inet_ntoa(ip->saddr));
printf("dst=%sn", inet_ntoa(ip->daddr));
if ( icmp->un.echo.id == pid ) {
printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]n",
icmp->type, icmp->code, ntohs(icmp->checksum),
icmp->un.echo.id, icmp->un.echo.sequence);
}
}
cs423 - cotter 18
Listener - separate process to
listen for and collect messages-
void listener(void) {
int sd, i;
struct sockaddr_in addr;
unsigned char buf[1024];
sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
if ( sd < 0 ) {
perror("socket");
exit(0);
}
for (i = 0; i < loops; i++) {
int bytes, len=sizeof(addr);
bzero(buf, sizeof(buf));
bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *) &addr,
&len);
if ( bytes > 0 )
display(buf, bytes);
else
perror("recvfrom");
}
exit(0);
}
cs423 - cotter 19
ping - Create message and send it
void ping(struct sockaddr_in *addr)
{
const int val=255;
int i, j, sd, cnt=1;
struct packet pckt;
struct sockaddr_in r_addr;
sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
if ( sd < 0 )
{
perror("socket");
return;
}
if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
perror("Set TTL option");
if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
perror("Request nonblocking I/O");
cs423 - cotter 20
ping (cont)
for (j = 0; j < loops; j++) { // send pings 1 per second
int len=sizeof(r_addr);
printf("Msg #%dn", cnt);
if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *)&r_addr, &len) > 0 )
printf("***Got message!***n");
bzero(&pckt, sizeof(pckt));
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = pid;
for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
pckt.msg[i] = i+'0';
pckt.msg[i] = 0;
pckt.hdr.un.echo.sequence = cnt++;
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *) addr, sizeof(*addr)) <= 0)
perror("sendto");
sleep(1);
}
}
cs423 - cotter 21
myNuPing.c – main()int main(int count, char *argv[]) {
struct hostent *hname;
struct sockaddr_in addr;
loops = 0;
if ( count != 3 ) {
printf("usage: %s <addr> <loops> n", argv[0]);
exit(0);
}
if (count == 3) // WE HAVE SPECIFIED A MESSAGE COUNT
loops = atoi(argv[2]);
if ( count > 1 ) {
pid = getpid();
proto = getprotobyname("ICMP");
hname = gethostbyname(argv[1]);
bzero(&addr, sizeof(addr));
addr.sin_family = hname->h_addrtype;
addr.sin_port = 0;
addr.sin_addr.s_addr = *(long*)hname->h_addr;
if ( fork() == 0 )
listener();
else
ping(&addr);
wait(0);
}
else
printf("usage: myping <hostname>n");
return 0;
}
cs423 - cotter 22
“Ping” Output
[root]# ./myNuPing 134.193.12.34 2
Msg #1
----------------
0000: 45 00 00 54 CC 38 40 00 80 01 1F BE 86 12 34 56
0010: 86 12 34 57 00 00 E4 06 DF 07 01 00 30 31 32 33
0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43
0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53
0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63
0050: 64 65 66 00
IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34
ICMP: type[0/0] checksum[58374] id[2015] seq[1]
Msg #2
***Got message!***
----------------
0000: 45 00 00 54 CC 39 40 00 80 01 1F BD 86 12 34 56
0010: 86 12 34 57 00 00 E3 06 DF 07 02 00 30 31 32 33
0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43
0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53
0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63
0050: 64 65 66 00
IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34
ICMP: type[0/0] checksum[58118] id[2015] seq[2]
[root]#
cs423 - cotter 23
Summary
• Raw Sockets allow access to Protocols other
than the standard TCP and UDP
• Performance and capabilities may be OS
dependent.
– Some OSs block the ability to send packets that
originate from raw sockets (although reception may
be permitted).
• Raw sockets remove the burden of the complex
TCP/IP protocol stack, but they also remove the
safeguards and support that those protocols
provide

Weitere ähnliche Inhalte

Was ist angesagt?

data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Network security cryptographic hash function
Network security  cryptographic hash functionNetwork security  cryptographic hash function
Network security cryptographic hash functionMijanur Rahman Milon
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
Network intrusion detection system and analysis
Network intrusion detection system and analysisNetwork intrusion detection system and analysis
Network intrusion detection system and analysisBikrant Gautam
 
Password Cracking using dictionary attacks
Password Cracking using dictionary attacksPassword Cracking using dictionary attacks
Password Cracking using dictionary attackslord
 
Elliptic Curve Cryptography
Elliptic Curve CryptographyElliptic Curve Cryptography
Elliptic Curve CryptographyJorgeVillamarin5
 
3. The Data Encryption Standard (DES) and Alternatives
3. The Data Encryption Standard (DES) and Alternatives3. The Data Encryption Standard (DES) and Alternatives
3. The Data Encryption Standard (DES) and AlternativesSam Bowne
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++Ilio Catallo
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to SwiftGiordano Scalzo
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking ExplainedThomas Graf
 
Session Initiation Protocol
Session Initiation ProtocolSession Initiation Protocol
Session Initiation ProtocolMatt Bynum
 

Was ist angesagt? (20)

Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Network security cryptographic hash function
Network security  cryptographic hash functionNetwork security  cryptographic hash function
Network security cryptographic hash function
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Network intrusion detection system and analysis
Network intrusion detection system and analysisNetwork intrusion detection system and analysis
Network intrusion detection system and analysis
 
Password Cracking using dictionary attacks
Password Cracking using dictionary attacksPassword Cracking using dictionary attacks
Password Cracking using dictionary attacks
 
Unix signals
Unix signalsUnix signals
Unix signals
 
Elliptic Curve Cryptography
Elliptic Curve CryptographyElliptic Curve Cryptography
Elliptic Curve Cryptography
 
3. The Data Encryption Standard (DES) and Alternatives
3. The Data Encryption Standard (DES) and Alternatives3. The Data Encryption Standard (DES) and Alternatives
3. The Data Encryption Standard (DES) and Alternatives
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
Socket programming
Socket programming Socket programming
Socket programming
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
Understanding NMAP
Understanding NMAPUnderstanding NMAP
Understanding NMAP
 
Session Initiation Protocol
Session Initiation ProtocolSession Initiation Protocol
Session Initiation Protocol
 
IP Sec - Basic Concepts
IP Sec - Basic ConceptsIP Sec - Basic Concepts
IP Sec - Basic Concepts
 
Transposition Cipher
Transposition CipherTransposition Cipher
Transposition Cipher
 
hill cipher
hill cipherhill cipher
hill cipher
 

Ähnlich wie Cs423 raw sockets_bw

Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdfsupport58
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptxssuserc4a497
 
Udp socket programming(Florian)
Udp socket programming(Florian)Udp socket programming(Florian)
Udp socket programming(Florian)Flor Ian
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver艾鍗科技
 
Introduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptIntroduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptMajedAboubennah
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoScyllaDB
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecturehugo lu
 
TCP IP
TCP IPTCP IP
TCP IPhivasu
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernelKiran Divekar
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Andriy Berestovskyy
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptsenthilnathans25
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection systemAashiq Ahamed N
 

Ähnlich wie Cs423 raw sockets_bw (20)

Sockets
SocketsSockets
Sockets
 
sockets
socketssockets
sockets
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdf
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptx
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
 
Udp socket programming(Florian)
Udp socket programming(Florian)Udp socket programming(Florian)
Udp socket programming(Florian)
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
Introduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptIntroduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.ppt
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
123
123123
123
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
TCP IP
TCP IPTCP IP
TCP IP
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
Packet filtering using jpcap
Packet filtering using jpcapPacket filtering using jpcap
Packet filtering using jpcap
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection system
 

Kürzlich hochgeladen

Transformative journey for Automotive Components Manufacturers- D&V Business ...
Transformative journey for Automotive Components Manufacturers- D&V Business ...Transformative journey for Automotive Components Manufacturers- D&V Business ...
Transformative journey for Automotive Components Manufacturers- D&V Business ...D&V Business Consulting
 
83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagar
83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagar83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagar
83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagardollysharma2066
 
Innovating Manufacturing with CNC Technology
Innovating Manufacturing with CNC TechnologyInnovating Manufacturing with CNC Technology
Innovating Manufacturing with CNC Technologyquickpartslimitlessm
 
BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024AHOhOops1
 
ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111
ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111
ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111Sapana Sha
 
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptUNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptDineshKumar4165
 
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Hauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhi
Hauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhiHauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhi
Hauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhiHot Call Girls In Sector 58 (Noida)
 
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptxUNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptxDineshKumar4165
 
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGERUNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGERDineshKumar4165
 
(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...
(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...
(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...Hot Call Girls In Sector 58 (Noida)
 
The 10th anniversary, Hyundai World Rally Team's amazing journey
The 10th anniversary, Hyundai World Rally Team's amazing journeyThe 10th anniversary, Hyundai World Rally Team's amazing journey
The 10th anniversary, Hyundai World Rally Team's amazing journeyHyundai Motor Group
 
What Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To AppearWhat Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To AppearJCL Automotive
 
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptxUNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptxDineshKumar4165
 
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHERUNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHERunosafeads
 
John Deere Tractors 5515 Diagnostic Repair Manual
John Deere Tractors 5515 Diagnostic Repair ManualJohn Deere Tractors 5515 Diagnostic Repair Manual
John Deere Tractors 5515 Diagnostic Repair ManualExcavator
 
How To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative ErrorHow To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative ErrorAndres Auto Service
 
VIP Kolkata Call Girl Kasba 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kasba 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kasba 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kasba 👉 8250192130 Available With Roomdivyansh0kumar0
 

Kürzlich hochgeladen (20)

Transformative journey for Automotive Components Manufacturers- D&V Business ...
Transformative journey for Automotive Components Manufacturers- D&V Business ...Transformative journey for Automotive Components Manufacturers- D&V Business ...
Transformative journey for Automotive Components Manufacturers- D&V Business ...
 
83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagar
83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagar83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagar
83778-77756 ( HER.SELF ) Brings Call Girls In Laxmi Nagar
 
Innovating Manufacturing with CNC Technology
Innovating Manufacturing with CNC TechnologyInnovating Manufacturing with CNC Technology
Innovating Manufacturing with CNC Technology
 
BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024
 
ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111
ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111
ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111
 
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptUNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
 
Call Girls In Kirti Nagar 7042364481 Escort Service 24x7 Delhi
Call Girls In Kirti Nagar 7042364481 Escort Service 24x7 DelhiCall Girls In Kirti Nagar 7042364481 Escort Service 24x7 Delhi
Call Girls In Kirti Nagar 7042364481 Escort Service 24x7 Delhi
 
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls East Of Kailash 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Hauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhi
Hauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhiHauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhi
Hauz Khas Call Girls ☎ 7042364481 independent Escorts Service in delhi
 
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptxUNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
 
Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...
Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...
Indian Downtown Call Girls # 00971528903066 # Indian Call Girls In Downtown D...
 
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGERUNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
 
(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...
(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...
(COD) ̄Young Call Girls In Dwarka , New Delhi꧁❤ 7042364481❤꧂ Escorts Service i...
 
The 10th anniversary, Hyundai World Rally Team's amazing journey
The 10th anniversary, Hyundai World Rally Team's amazing journeyThe 10th anniversary, Hyundai World Rally Team's amazing journey
The 10th anniversary, Hyundai World Rally Team's amazing journey
 
What Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To AppearWhat Causes BMW Chassis Stabilization Malfunction Warning To Appear
What Causes BMW Chassis Stabilization Malfunction Warning To Appear
 
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptxUNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
 
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHERUNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
 
John Deere Tractors 5515 Diagnostic Repair Manual
John Deere Tractors 5515 Diagnostic Repair ManualJohn Deere Tractors 5515 Diagnostic Repair Manual
John Deere Tractors 5515 Diagnostic Repair Manual
 
How To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative ErrorHow To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
How To Troubleshoot Mercedes Blind Spot Assist Inoperative Error
 
VIP Kolkata Call Girl Kasba 👉 8250192130 Available With Room
VIP Kolkata Call Girl Kasba 👉 8250192130  Available With RoomVIP Kolkata Call Girl Kasba 👉 8250192130  Available With Room
VIP Kolkata Call Girl Kasba 👉 8250192130 Available With Room
 

Cs423 raw sockets_bw

  • 1. cs423 - cotter 1 What are Raw Sockets? 1.A way to pass information to network protocols other than TCP or UDP (e.g. ICMP and IGMP) 2.A way to implement new IPv4 protocols 3.A way to build our own packets (be careful here)
  • 2. cs423 - cotter 2 Why Would We Use Them? • Allows us to access packets sent over protocols other than TCP / UDP • Allows us to process IPv4 protocols in user space – Control, speed, troubleshooting • Allow us to implement new IPv4 protocols • Allows us to control the IP header – Control option fields (beyond setsockopt() ) – Test / control packet fragmentation
  • 3. cs423 - cotter 3 Limitations? • Reliability Loss • No Ports • Nonstandard communication • No Automatic ICMP • Raw TCP / UDP unlikely • Requires root / admin
  • 4. cs423 - cotter 4 OS Involvement in Sockets User Space Kernel Space Socket App TCP/IP StackLinux Socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP) Socket ( AF_INET, SOCK_RAW, IPPROTO_ICMP) Socket ( AF_PACKET, SOCK_RAW, htons(ETH_P_IP)) Identify Socket Type Identify Socket Type Identify Socket Type TCP IP Ethernet
  • 5. cs423 - cotter 5 Normal Socket Operation (TCP) • Create a socket – s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP) • Bind to a port (optional) – Identify local IP and port desired and create data structure – bind (s, (struct sockaddr *) &sin, sizeof(sin)) • Establish a connection to server – Identify server IP and port – connect (s, (struct sockaddr *) &sin, sizeof(sin)) • Send / Receive data – Place data to be send into buffer – recv (s, buf, strlen(buf), 0);
  • 6. cs423 - cotter 6 Normal Socket Operation (TCP) User Space Kernel Space Socket App ProtocolLinux socket ( ) Create socket TCP, IP, Internet connect( ) Bind to local port: Connect to remote port send( ) TCP, IP, InternetPass data thru local stack to remote port OK OK OK TCP
  • 7. cs423 - cotter 7 Raw Sockets Operation (ICMP) • Create a socket – s = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP) • Since there is no port, there is no bind * • There is no TCP, so no connection * • Send / Receive data – Place data to be sent into buffer – sendto (s, buf, strlen(buf), 0, addr, &len); * More later
  • 8. cs423 - cotter 8 Raw Sockets Operation (ICMP) User Space Kernel Space Socket App ProtocolLinux socket ( ) Create socket sendto( ) IP, InternetPass data thru local stack to remote host OK OK ICMP
  • 9. cs423 - cotter 9 Create a Raw Socket • s = socket (AF_INET, SOCK_RAW, protocol) – IPPROTO_ICMP, IPPROTO_IP, etc. • Can create our own IP header if we wish – const int on = 1; – setsockopt (s, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)); • Can “bind” – Since we have no port, the only effect is to associate a local IP address with the raw socket. (useful if there are multiple local IP addrs and we want to use only 1). • Can “connect” – Again, since we have no TCP, we have no connection. The only effect is to associate a remote IP address with this socket.
  • 10. cs423 - cotter 10 Raw Socket Output • Normal output performed using sendto or sendmsg. – Write or send can be used if the socket has been connected • If IP_HDRINCL not set, starting addr of the data (buf) specifies the first byte following the IP header that the kernel will build. – Size only includes the data above the IP header. • If IP_HDRINCL is set, the starting addr of the data identifies the first byte of the IP header. – Size includes the IP header – Set IP id field to 0 (tells kernel to set this field) – Kernel will calculate IP checksum • Kernel can fragment raw packets exceeding outgoing MTU
  • 11. cs423 - cotter 11 Raw Socket Input • Received TCP / UDP NEVER passed to a raw socket. • Most ICMP packets are passed to a raw socket – (Some exceptions for Berkeley-derived implementations) • All IGMP packets are passed to a raw socket • All IP datagrams with a protocol field that the kernel does not understand (process) are passed to a raw socket. • If packet has been fragmented, packet is reassembled before being passed to raw socket
  • 12. cs423 - cotter 12 Conditions that include / exclude passing to specific raw sockets • If a nonzero protocol is specified when raw socket is created, datagram protocol must match • If raw socket is bound to a specific local IP, then destination IP must match • If raw socket is “connected” to a foreign IP address, then the source IP address must match
  • 13. cs423 - cotter 13 Ping – Overview • This example modified from code by Walton (Ch 18) • Very simple program that uses ICMP to send a ping to another machine over the Internet. • Provides the option to send a defined number of packets (or will send a default 25). • We will build an ICMP packet (with a proper header, including checksum) that will be updated each time we send a new packet. • We will display the raw packet that is received back from our destination host and will interpret some of the data. – (Output format is different from standard ping)
  • 14. cs423 - cotter 14 ICMP Packet header struct icmphdr { u_int8_t type // ICMP message type (0) u_int8_t code // ICMP type sub-code (0) u_int16_t checksum E306, etc. u_int16_t id // echo datagram id (use pid) u_int16_t sequence // echo seq # 1, 2, 3, etc. }; Packet body: 0 1 2 3 4 5 6 7 8 9 : ; < = > ? … B
  • 15. cs423 - cotter 15 myNuPing.c (overview) • Global Declarations – Struct packet { }, some variables • unsigned short checksum (void *b, int len) – Calculate checksum for ICMP packet (header and data) • void display (void *buf, int bytes) – Format a received packet for display. • void listener (void) – Separate process to capture responses to pings • void ping (struct sockaddr_in *addr) – Create socket and send out pings 1/sec to specified IP addr • int main (int count, shar *strings[ ]) – Test for valid instantiation, create addr structure – Fork a separate process (listener) and use existing process for ping
  • 16. cs423 - cotter 16 #defines and checksum calc #define PACKETSIZE 64 struct packet { struct icmphdr hdr; char msg[PACKETSIZE-sizeof(struct icmphdr)]; }; int pid=-1; int loops = 25; struct protoent *proto=NULL; unsigned short checksum(void *b, int len) { unsigned short *buf = b; unsigned int sum=0; unsigned short result; for ( sum = 0; len > 1; len -= 2 ) sum += *buf++; if ( len == 1 ) sum += *(unsigned char*)buf; sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); result = ~sum; return result; }
  • 17. cs423 - cotter 17 display - present echo info void display(void *buf, int bytes) { int i; struct iphdr *ip = buf; struct icmphdr *icmp = buf+ip->ihl*4; printf("----------------n"); for ( i = 0; i < bytes; i++ ) { if ( !(i & 15) ) printf("n%04X: ", i); printf("%02X ", ((unsigned char*)buf)[i]); } printf("n"); printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ", ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol, ip->ttl, inet_ntoa(ip->saddr)); printf("dst=%sn", inet_ntoa(ip->daddr)); if ( icmp->un.echo.id == pid ) { printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]n", icmp->type, icmp->code, ntohs(icmp->checksum), icmp->un.echo.id, icmp->un.echo.sequence); } }
  • 18. cs423 - cotter 18 Listener - separate process to listen for and collect messages- void listener(void) { int sd, i; struct sockaddr_in addr; unsigned char buf[1024]; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); exit(0); } for (i = 0; i < loops; i++) { int bytes, len=sizeof(addr); bzero(buf, sizeof(buf)); bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *) &addr, &len); if ( bytes > 0 ) display(buf, bytes); else perror("recvfrom"); } exit(0); }
  • 19. cs423 - cotter 19 ping - Create message and send it void ping(struct sockaddr_in *addr) { const int val=255; int i, j, sd, cnt=1; struct packet pckt; struct sockaddr_in r_addr; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); return; } if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) perror("Set TTL option"); if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 ) perror("Request nonblocking I/O");
  • 20. cs423 - cotter 20 ping (cont) for (j = 0; j < loops; j++) { // send pings 1 per second int len=sizeof(r_addr); printf("Msg #%dn", cnt); if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *)&r_addr, &len) > 0 ) printf("***Got message!***n"); bzero(&pckt, sizeof(pckt)); pckt.hdr.type = ICMP_ECHO; pckt.hdr.un.echo.id = pid; for ( i = 0; i < sizeof(pckt.msg)-1; i++ ) pckt.msg[i] = i+'0'; pckt.msg[i] = 0; pckt.hdr.un.echo.sequence = cnt++; pckt.hdr.checksum = checksum(&pckt, sizeof(pckt)); if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *) addr, sizeof(*addr)) <= 0) perror("sendto"); sleep(1); } }
  • 21. cs423 - cotter 21 myNuPing.c – main()int main(int count, char *argv[]) { struct hostent *hname; struct sockaddr_in addr; loops = 0; if ( count != 3 ) { printf("usage: %s <addr> <loops> n", argv[0]); exit(0); } if (count == 3) // WE HAVE SPECIFIED A MESSAGE COUNT loops = atoi(argv[2]); if ( count > 1 ) { pid = getpid(); proto = getprotobyname("ICMP"); hname = gethostbyname(argv[1]); bzero(&addr, sizeof(addr)); addr.sin_family = hname->h_addrtype; addr.sin_port = 0; addr.sin_addr.s_addr = *(long*)hname->h_addr; if ( fork() == 0 ) listener(); else ping(&addr); wait(0); } else printf("usage: myping <hostname>n"); return 0; }
  • 22. cs423 - cotter 22 “Ping” Output [root]# ./myNuPing 134.193.12.34 2 Msg #1 ---------------- 0000: 45 00 00 54 CC 38 40 00 80 01 1F BE 86 12 34 56 0010: 86 12 34 57 00 00 E4 06 DF 07 01 00 30 31 32 33 0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 0050: 64 65 66 00 IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34 ICMP: type[0/0] checksum[58374] id[2015] seq[1] Msg #2 ***Got message!*** ---------------- 0000: 45 00 00 54 CC 39 40 00 80 01 1F BD 86 12 34 56 0010: 86 12 34 57 00 00 E3 06 DF 07 02 00 30 31 32 33 0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 0050: 64 65 66 00 IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34 ICMP: type[0/0] checksum[58118] id[2015] seq[2] [root]#
  • 23. cs423 - cotter 23 Summary • Raw Sockets allow access to Protocols other than the standard TCP and UDP • Performance and capabilities may be OS dependent. – Some OSs block the ability to send packets that originate from raw sockets (although reception may be permitted). • Raw sockets remove the burden of the complex TCP/IP protocol stack, but they also remove the safeguards and support that those protocols provide

Hinweis der Redaktion

  1. cs423 - cotter
  2. cs423 - cotter
  3. cs423 - cotter
  4. cs423 - cotter
  5. cs423 - cotter
  6. cs423 - cotter
  7. cs423 - cotter
  8. cs423 - cotter
  9. cs423 - cotter
  10. cs423 - cotter