CSCE 790: Computer Network Security

Download Report

Transcript CSCE 790: Computer Network Security

CSCE 515:
Computer Network Programming
Chin-Tser Huang
[email protected]
University of South Carolina
Something on Midterm Exam



To be held in class on March 3, 2005
(9:30am-10:45am)
No excuse will be accepted
If any substitute exam is really
necessary, it will be MUCH HARDER
than original exam
2/17/2005
2
ICMP Source Quench Error
0
78
type(4)
15 16
code(0)
31
checksum
unused (must be 0)
IP header + first 8 bytes of original datagram data
2/17/2005
3
Address Bindings of UDP Server

Three types of address bindings



localIP.lport and foreignIP.fport
localIP.lport and *.*
*.lport and *.*
2/17/2005
4
Broadcasting and Multicasting




Send message from single source to all
or multiple destinations
Only apply to UDP (why?)
Broadcasting is to send a message to
every host on the cable
Multicasting is to send a message to a
set of hosts that belong to a multicast
group
2/17/2005
5
Broadcasting

Four types of IP broadcast addresses




Limited broadcast: 255.255.255.255
Net-directed broadcast: e.g.
netid.255.255.255 for a class A network ID
Subnet-directed broadcast: all “1” bits for
host ID but need a specific subnet ID
All-subnets-directed broadcast: need to
know subnet mask
2/17/2005
6
Multicasting

Two types of services


Delivery to multiple destinations
Solicitation of servers by clients
2/17/2005
7
Multicast Group Address


Class D
Combination of high-order 4 bits of
“1110” and multicast group ID
Range is 224.0.0.0 – 239.255.255.255
1 1 1 0
2/17/2005
28
multicast group ID
8
Host Group




Set of hosts listening to a particular IP
multicast address
Can span multiple networks
Membership is dynamic; hosts can join and
leave at any time
Permanent host groups




224.0.0.1:
224.0.0.2:
224.0.1.1:
224.0.0.9:
2/17/2005
all systems on this subnet
all routers on this subnet
Network Time Protocol (NTP)
RIP-2
9
Converting Multicast Group
Addresses to Ethernet Addresses


Upper 5 bits of multicast group ID are
ignored in the mapping
Thus mapping is not unique
not used in mapping
0
Class D IP address
7 8
15 16
23 24
31
1110
low-order 23 bits copied to Ethernet address
00000001 00000000 01011110 0
2/17/2005
10
Class MulticastSocket


Extend class DatagramSocket and add
support for IP multicast
Multiple MulticastSockets can listen to
same port on same machine
2/17/2005
11
Class MulticastSocket
Constructors
MulticastSocket()
MulticastSocket(int port)
2/17/2005
12
Class MulticastSocket
Methods
void joinGroup(InetAddress group) throws IOException
void leaveGroup(InetAddress group) throws IOException
void setTimeToLive(int ttl) throws IOException
void setTTL(byte ttl) throws IOException
int getTimeToLive() throws IOException
byte getTTL() throws IOException
void send(DatagramPacket packet, byte ttl) throws IOException
void setInterface(InetAddress address) throws SocketException
InetAddress getInterface() throws SocketException
2/17/2005
13
Class MulticastSocket
Exceptions
IOException
SecurityException
2/17/2005
14
Sending Multicast Packets
// byte[] data
// InetAddress multicastGroup
// int multicastPort
MulticastSocket socket = new MulticastSocket();
DatagramPacket packet = new DatagramPacket
(data, data.length, multicastGroup, multicastPort);
socket.send(packet, (byte) 64);
socket.close();
2/17/2005
15
Receiving Multicast Packets
MulticastSocket socket = new MulticastSocket(multicastPort);
Socket.joinGroup(multicastGroup);
byte buffer[] = new byte[65508];
DatagramPacket packet = new DatagramPacket();
socket.receive(packet);
InetAddress fromAddress = packet.getAddress();
int fromPort = packet.getPort();
int length = packet.getLength();
byte[] data = packet.getData();
// …
socket.leaveGroup(multicastGroup);
socket.close();
2/17/2005
16
A Peer-to-Peer Multicast Chat System



Each client multicasts its message to
other clients
No server is involved; all clients
communicate as peers
Open a chat frame and start a thread
that listens for incoming packets
2/17/2005
17
MulticastChat.java
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek
Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * *
Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights
reserved; see license.txt for details. */
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class MulticastChat implements Runnable, WindowListener, ActionListener {
// public MulticastChat (InetAddress group, int port) …
// public synchronized void start () throws IOException …
// public synchronized void start () throws IOException …
// public void windowOpened (WindowEvent event) …
// public void windowClosing (WindowEvent event) …
// public void actionPerformed (ActionEvent event) …
// public void run () …
// public static void main (String[] args) throws IOException …
}
2/17/2005
18
Constructor MulticastChat
protected InetAddress group;
protected int port;
public MulticastChat (InetAddress group, int port) {
this.group = group;
this.port = port;
initAWT ();
}
// protected void initAWT () …
2/17/2005
19
Method initAWT
protected Frame frame;
protected TextArea output;
protected TextField input;
protected void initAWT () {
frame = new Frame
("MulticastChat [" + group.getHostAddress () + ":" + port + "]");
frame.addWindowListener (this);
output = new TextArea ();
output.setEditable (false);
input = new TextField ();
input.addActionListener (this);
frame.setLayout (new BorderLayout ());
frame.add (output, "Center");
frame.add (input, "South");
frame.pack ();
}
2/17/2005
20
Method start
protected Thread listener;
public synchronized void start () throws IOException {
if (listener == null) {
initNet ();
listener = new Thread (this);
listener.start ();
frame.setVisible (true);
}
}
// protected void initNet () throws IOException …
2/17/2005
21
Method initNet
protected MulticastSocket socket;
protected DatagramPacket outgoing, incoming;
protected void initNet () throws IOException {
socket = new MulticastSocket (port);
socket.setTimeToLive (1);
socket.joinGroup (group);
outgoing = new DatagramPacket (new byte[1], 1, group, port);
incoming = new DatagramPacket (new byte[65508], 65508);
}
2/17/2005
22
Method stop
public synchronized void stop () throws IOException {
frame.setVisible (false);
if (listener != null) {
listener.interrupt ();
listener = null;
try {
socket.leaveGroup (group);
} finally {
socket.close ();
}
}
}
2/17/2005
23
Window-related Methods
public void windowOpened (WindowEvent event) {
input.requestFocus ();
}
public void windowClosing (WindowEvent event) {
try {
stop ();
} catch (IOException ex) {
ex.printStackTrace ();
}
}
public
public
public
public
public
void
void
void
void
void
2/17/2005
windowClosed (WindowEvent event) {}
windowIconified (WindowEvent event) {}
windowDeiconified (WindowEvent event) {}
windowActivated (WindowEvent event) {}
windowDeactivated (WindowEvent event) {}
24
Method actionPerformed
public void actionPerformed (ActionEvent event) {
try {
byte[] utf = event.getActionCommand ().getBytes ("UTF8");
outgoing.setData (utf);
outgoing.setLength (utf.length);
socket.send (outgoing);
input.setText ("");
} catch (IOException ex) {
handleIOException (ex);
}
}
// protected synchronized void handleIOException (IOException ex) …
2/17/2005
25
Method handleIOException
protected synchronized void handleIOException (IOException ex) {
if (listener != null) {
output.append (ex + "\n");
input.setVisible (false);
frame.validate ();
if (listener != Thread.currentThread ())
listener.interrupt ();
listener = null;
try {
socket.leaveGroup (group);
} catch (IOException ignored) {
}
socket.close ();
}
}
2/17/2005
26
Method run
public void run () {
try {
while (!Thread.interrupted ()) {
incoming.setLength (incoming.getData ().length);
socket.receive (incoming);
String message = new String
(incoming.getData (), 0, incoming.getLength (), "UTF8");
output.append (message + "\n");
}
} catch (IOException ex) {
handleIOException (ex);
}
}
2/17/2005
27
Method main
public static void main (String[] args) throws IOException {
if ((args.length != 1) || (args[0].indexOf (":") < 0))
throw new IllegalArgumentException
("Syntax: MulticastChat <group>:<port>");
int idx = args[0].indexOf (":");
InetAddress group = InetAddress.getByName (args[0].substring (0, idx));
int port = Integer.parseInt (args[0].substring (idx + 1));
}
MulticastChat chat = new MulticastChat (group, port);
chat.start ();
2/17/2005
28
Next Class




IGMP
DNS
TCP
Read TI Ch. 13, 14, 17
2/17/2005
29