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
Lifecycle of a Stream Socket
Server is started and waits for connection
requests from clients
Client sends a connection request and server
accepts the request
Client sends more data requests to server,
and server sends data replies to client
Client closes its end of connection, and server
closes its end of connection
1/20/2004
2
Client-Server Communication (TCP)
socket()
int socket(int family, int type, int protocol);
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
TCP Client
socket()
int listen(int sockfd, int backlog);
int socket(int family, int type, int protocol);
bind()
TCP Server
well-known port
listen()
accept()
int accept(int
sockfd,
void *addr,
int *addrlen);
int connect(int
sockfd, struct
sockaddr
*serv_addr,
int addrlen);
connect()
connection establishment
blocks until connection from client
write()
read()
process request
write()
read()
close()
read()
int close(int sockfd);
1/20/2004
int close(int sockfd);
close()
3
Modularized Socket Code:
Connecting to a Server (TCP)
int connect_socket(char *hostname, int port) {
int sock;
IPv4 socket address structure
struct socketaddr_in{
struct sockaddr_in sin;
Hostent
structure
uint8_t
struct hostent{ sin_len; /*length of the structure (16)*/
struct hostent *host;
sa_falimily_t
sin_family/*official
/* AF_INT*/
char
* h_name
name of host*/
sock = socket(AF_INET, SOCK_STREAM,
0);
in_port_t
sin_port
/*
16
bit
or UDP
char ** h_aliases;
/* pointerTCP
ot array
of\ port number*/
if (sock == -1)
struct in_addr sin_addr /*pointers
32 bit Ipv4
address
*/
to alias
name*/
char
sin_zero(8)/*
unused*/
return sock;
int
h_addrtype
/* host
address type*/
}int
h_length
/* length
of address */
family, int type,
int protocol);
host = gethostbyname(hostname);Socket(int
char
**
h_addr_list
/*ptr
to
array
of ptrs
return nonnegative value for OK, -1 for
errorwith \
if (host == NULL) {
IPv4 or IPv6 address*/
close(sock);
}
return -1;
struct
hostent
*gethostbyname(
const char *hostname);
unit16_t
htons(unit16_t
host16bitvaule)
}
/*Return
if OK,
error
*/ to
/*Changenonnull
the portpointer
number
fromNULL
host on
byte
order
memset (&sin, 0, sizeof(sin));
network byte order */
connect(int socketfd, const struct sockaddr * servaddr,
sin.sin_family = AF_INET;
socket_t addrlen)
/*Perform the TCP three way handshaking*/
sin.sin_port = htons(port);
sin.sin_addr.s_addr = *(unsigned long *)host-> h_addr_list[ 0];
if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
close (sock);
return -1;
}
1/20/2004
return sock;
Make the socket
Resolve the host
Setup up the struct
Connect
4
Modularized Socket Code:
Listening on a Port (TCP)
int make_listen_socket(int port) {
struct sockaddr_in sin;
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
Make the socket
return -1;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
Setup up the struct
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
Bind
return -1;
return sock;
bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen);
/* return 0 if OK, -1 on error
}
assigns a local protocol adress to a socket*/
1/20/2004
5
Modularized Socket Code:
Accepting a Client Connection (TCP)
int get_client_socket(int listen_socket) {
struct sockaddr_in sin;
int sock;
int sin_len;
memset(&sin, 0, sizeof(sin));
Setup up the struct
sin_len = sizeof(sin);
sock = accept(listen_socket, (struct sockaddr *) &sin, &sin_len);
return sock;
Accept the client connection
}
accept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen)
/* return nonnegative descriptor if OK, -1 on error
return the next completed connection from the front of the
completed connection queue.
if the queue is empty,
the process is put to sleep(assuming blocking socket)*/
1/20/2004
6
Network Programming in Java
Use classes in package java.net which
provide access to IP addresses, TCP,
UDP, and URL-related mechanisms
Use stream-related classes for I/O
Use threads for multithreading
1/20/2004
7
Class InetAddress
Constructors
No constructors; use getByName(), getByAddress(),
getLocalHost(), getAllByName()
Static methods
InetAddress getLocalHost() throws UnknownHostException
InetAddress getByName(String host) throws
UnknownHostException
InetAddress getByAddress(byte[] addr) throws
UnknownHostException
InetAddress[] getAllByName(String host) throws
UnknownHostException
1/20/2004
8
Class InetAddress
Instance methods
byte[] getAddress()
String getHostName()
String getHostAddress()
boolean isMulticastAddress()
1/20/2004
9
An InetAddress Example
Print out local machine’s address
Stay in a loop accepting host names or
addresses and looking them up
Terminate on EOF
1/20/2004
10
InetExample.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.net.*;
import java.io.*;
public class InetExample {
// public static void main (String args[]) …
}
1/20/2004
11
Method main
public static void main (String args[]) {
printLocalAddress ();
Reader kbd = new FileReader (FileDescriptor.in);
BufferedReader bufferedKbd = new BufferedReader (kbd);
try {
String name;
do {
System.out.print ("Enter a hostname or IP address: ");
System.out.flush ();
name = bufferedKbd.readLine ();
if (name != null)
printRemoteAddress (name);
} while (name != null);
System.out.println ("exit");
} catch (IOException ex) {
System.out.println ("Input error:");
ex.printStackTrace ();
}
}
// static void printLocalAddress () …
// static void printRemoteAddress (String name) …
1/20/2004
12
Method printLocalAddress
static void printLocalAddress () {
try {
InetAddress myself = InetAddress.getLocalHost ();
System.out.println ("My name : " + myself.getHostName ());
System.out.println ("My IP : " + myself.getHostAddress ());
System.out.println ("My class : " + ipClass (myself.getAddress
()));
} catch (UnknownHostException ex) {
System.out.println ("Failed to find myself:");
ex.printStackTrace ();
}
}
// static char ipClass (byte[] ip) …
1/20/2004
13
Method ipClass
static char ipClass (byte[] ip) {
int highByte = 0xff & ip[0];
return (highByte < 128) ? 'A' : (highByte < 192) ? 'B' :
(highByte < 224) ? 'C' : (highByte < 240) ? 'D' : 'E';
}
1/20/2004
14
Method printRemoteAddress
static void printRemoteAddress (String name) {
try {
System.out.println ("Looking up " + name + "...");
InetAddress machine = InetAddress.getByName (name);
System.out.println ("Host name : " + machine.getHostName
());
System.out.println ("Host IP : " + machine.getHostAddress ());
System.out.println ("Host class : " +
ipClass (machine.getAddress ()));
} catch (UnknownHostException ex) {
System.out.println ("Failed to lookup " + name);
}
}
1/20/2004
15
Next Class
Socket programming in Java
Read JNP Ch. 14, 16
Project 1 will be passed out
1/20/2004
16