![Page 1: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/1.jpg)
Raw Sockets
![Page 2: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/2.jpg)
What are Raw Sockets?
• Allows you to bypass the TCP/UDP layers.
• Send/receive your own packets, with your own headers.
• You need to do all protocol processing at user-level.
![Page 3: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/3.jpg)
Typical Uses• ICMP messages
– ping generates ICMP echo requests and received ICMP echo replies.
• Routing protocols– gated implements OSPF routing protocol.– Uses IP packets with protocol ID 89 – not supported by
kernel.
• Hacking – Generating your own TCP/UDP packets with spoofed
headers
![Page 4: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/4.jpg)
Raw socket creation
• Only root can open a raw socket.
sockfd = socket(AF_INET, SOCK_RAW, proto)
where proto is IPPROTO_RAW, IPPROTO_ICMP etc.
![Page 5: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/5.jpg)
Raw socket output
• As usual – sendto(), sendmsg() etc.
• IP_HDRINCL option – Specifies whether the process or the kernel
builds the IP header.
/* allow process to build IP header */int on=1; setsockopt( sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on))
![Page 6: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/6.jpg)
Raw socket input• Normally using recvfrom()• Conditions for a packet to match raw socket
– If protocol parameter was specified, only packets with that protocol value are delivered.
– If bind() was called on raw socket, only packets destined to bound IP address are delivered.
– If connect() was called, only packets from connected address are delivered.
![Page 7: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/7.jpg)
Which protocol types are delivered?
• TCP and UDP never reach raw sockets– Kernel IP stack handles these– Linux implementation is an exception.
• All ICMP except– ICMP echo request– Timestamp request– Mask request
• All IGMP
• All other protocols that kernel doesn't understand– Such as OSPF
![Page 8: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/8.jpg)
Exercise
Write a simple “Hello” exchange program using raw sockets
that implements your own protocol
on top of IP.
![Page 9: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/9.jpg)
Java
Reference Book -
“Thinking in Java”
by Bruce Eckel
Book is online at
http://www.mindview.net/books/TIJ
![Page 10: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/10.jpg)
Levels of abstraction
Machine
Assembly language
Imperative languages(C, FORTRAN, ALGOL)
Object oriented languages(C++, Java)
Low-levelmachine details
Abstracting the machine model
Abstracting the problem as object interactions
![Page 11: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/11.jpg)
Object
• An object has – State (internal data)– Behavior (methods that operate on data)– Identity (object can be addressed uniquely)
![Page 12: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/12.jpg)
Objects…
• Class defines properties of a set of similar objects.– Think of it as type of the object
• Protection boundaries define what object state is visible to others.
• Inheritance allows a new class to inherit properties of an earlier class without re-defining them.
• Interface defines what requests others can make to a particular object
![Page 13: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/13.jpg)
Parent class
Interface
Child classes
![Page 14: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/14.jpg)
Primitive Data types
• boolean - false• char - ‘\u0000’ (null)• byte - (byte)0• short - (short)0• int - 0• long - 0L• float - 0.0f• double -0.0d
![Page 15: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/15.jpg)
Creating a class
class Circle {
public :Circle(int x, int y, float r) {…}void draw(){…}void erase(){…}void move(){…}
private :int x_;int y_;float radius_;
}
Methods
Fields
![Page 16: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/16.jpg)
Creating an object
Circle c = new Circle(10,20,5);
c.draw();
c.move();
c.erase();
![Page 17: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/17.jpg)
Inheritanceabstract class Shape {
public :
Shape(int x, int y) {
x_ = x;
y_ = y;
}abstract void draw();
abstract void erase();
abstract void move();
protected :
int x_;
int y_;
}
![Page 18: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/18.jpg)
Inheritance…class Circle extends Shape
{
Circle(int x,int y,float r){
super(x,y);
radius_ = r;
}void draw() {…}void erase(){…}void move(){…}
protected :
float radius_;
}
![Page 19: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/19.jpg)
Inheritance…class Square extends Shape
{
Square(int x,int y,float w){
super(x,y);
width_ = r;
}void draw() {…}void erase(){…}void move(){…}
protected :
float width_;
}
![Page 20: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/20.jpg)
void doStuff(Shape s) {
s.erase(); // ... s.draw();
}
Circle c = new Circle(); Square s = new Square(); Line l = new Line();
doStuff(c); doStuff(s); doStuff(l);
![Page 21: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/21.jpg)
Interfaces
interface DrawObject {// automatically public
void draw();
void erase();
void move();
// Compile-time constant:
int SOME_CONST = 5; // static & final
}
![Page 22: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/22.jpg)
class Shape {public :
Shape(int x, int y) {
x_ = x;
y_ = y;
}
protected :
int x_;
int y_;
}
![Page 23: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/23.jpg)
class Circle extends Shape implements DrawObject
{
Circle(int x,int y,float r){
super(x,y);
radius_ = r;
}void draw() {…}void erase(){…}void move(){…}
protected :
float radius_;
}
![Page 24: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/24.jpg)
class Square extends Shape implements DrawObject
{
Square(int x,int y,float w){
super(x,y);
width_ = r;
}void draw() {…}void erase(){…}void move(){…}
protected :
float width_;
}
![Page 25: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/25.jpg)
static members• static fields or methods have one instance across
all object instancesclass X {
static int i = 0;
……
}
x1 = new X();
x2 = new X();
x1.i++;
x2.i++;
System.out.println(“Result = “ + x2.i);
Result = 2
![Page 26: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/26.jpg)
final member/class• final class X {
…}Final classes cannot be extended.
• class X {final int m();
}Final methods cannot be overridden.
• int mthd(final MyClass mc) {…}– Final arguments cannot be modified.
• final int MY_CONST = 10;– Final fields/references are constants
![Page 27: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/27.jpg)
Things you should learn from TIJ
• Packaging your classes - Chapter 5
• Java I/O system - Chapter 11
• Error handling with exceptions - Chapter 10
• Applets - Chapter 14
![Page 28: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/28.jpg)
Using Standard Java Packages
java import java.lang.*;
public class HelloDate {public static void main(String[] args) {
System.out.println("Hello, it's: ");System.out.println(new Date());
} }
![Page 29: Raw Sockets. What are Raw Sockets? Allows you to bypass the TCP/UDP layers. Send/receive your own packets, with your own headers. You need to do all protocol](https://reader036.vdocuments.us/reader036/viewer/2022062518/56649e1b5503460f94b09b34/html5/thumbnails/29.jpg)
Some other packages…
• java.net
• java.rmi
• java.io
• java.applet
• java.awt
• java.math