Internet Protocol and Network Sockets
Internet Protocol and Network Sockets
Internet Protocol (IP)
- Definition: The Internet Protocol (IP) is a network-layer protocol that contains addressing information and control information, enabling packets to be routed.
- Documentation: IP is documented in RFC 791 and is the primary network-layer protocol in the Internet protocol suite.
- Relationship with Other Protocols: Along with Transmission Control Protocol (TCP) and User Datagram Protocol (UDP), IP represents the core of Internet protocols.
OSI Model
- Overview: The OSI model consists of 7 layers, which standardize network communication functions in seven distinct layers.
7 Layers of the OSI Model
Application Layer
- Purpose: Provides end-user services.
- Examples: HTTP, FTP, IRC, SSH, DNSPresentation Layer
- Purpose: Translates data between the application layer and the network.
- Examples: SSL, SSH, IMAP, FTP, MPEG, JPEGSession Layer
- Purpose: Manages sessions between end-user applications.
- Functionality: Synchronization and sending data to the port.Transport Layer
- Purpose: Responsible for error detection and recovery, as well as flow control.
- Examples: TCP, UDPNetwork Layer
- Purpose: Responsible for packet forwarding, including routing through intermediate routers.
- Examples: IP, ICMP, IPSec, IGMPData Link Layer
- Purpose: Manages node-to-node data transfer and handles error correction from the physical layer.
- Examples: Ethernet, PPP, Switch, BridgePhysical Layer
- Purpose: Transmits raw bit stream over physical medium.
- Examples: Coax, Fiber, Wireless, Hubs, Repeaters
What’s a Socket?
- Definition: A socket is the BSD method for accomplishing inter-process communication (IPC) using IP.
- Characteristics: It serves as the logical software endpoint of a communication channel.
- Analogy: Imagine a pipeline between two processes (local or remote).
- Importance: Sockets are the building blocks for various network communication protocols such as telnet, FTP, HTTP, etc.
BSD Sockets Support
- Most modern Operating Systems support BSD sockets, with Linux also supporting BSD-style sockets with certain conditions.
Glimpse Under the Hood
- Network IP addressing: A typical computer has one network IP address, which uniquely identifies it on a network (e.g., google.com).
- Communication process: To reach a computer over the network using its IP address is just the first step; the next challenge is contacting a specific service running inside that computer.
Role of Ports in IP
- Definition: IP uses an additional identifier called a port, analogous to a telephone extension.
- Well-Known Ports: Many common services have well-known ports, such as:
- HTTP: 80
- FTP: 21
- Users can test ports in their browser by modifying the URL (e.g.,http://www.google.com:80,http://www.google.com:81).
Important Notes on Binding Ports
- Only one application can bind to a port at a time without specific socket options.
Network Data Packets
- Each network data packet has an associated destination MAC/IP/Port.
- Media Access Control (MAC): Every network card (NIC) has a unique MAC address assigned to it.
- An Operating System uses the port as a routing mechanism to direct incoming network data to the appropriate process.
- The OS maintains a table of active ports and assigns processes to these ports via sockets.
Creating a Socket (BSD)
- To create a socket, use the function socket().
- Parameters:
-AF_INET: Indicates a network socket.
-SOCK_STREAM: Represents a stream socket, typically for TCP connections. - Example:
int lnSock = socket(AF_INET, SOCK_STREAM, 0);
```
## Two Basic Socket Protocols
1. **User Datagram Protocol (UDP)**
- **Characteristics**:
- Connectionless
- No guaranteed delivery or order
- Low overhead, often yielding faster transmission
- Suitable for applications requiring speed (e.g., online gaming)
2. **Transmission Control Protocol (TCP)**
- **Characteristics**:
- Connection-oriented
- Guarantees delivery and order of packets
- More overhead due to error checking and flow control
- Suitable for applications requiring reliability (e.g., web browsing)
## Socket Types
- **Socket Types**: Correspond directly to the available socket protocols.
- **Datagram Socket**: For UDP communications
- **Stream Socket**: For TCP communications
- TCP combined with IP is known as **TCP/IP**; UDP combined with IP is known as **UDP/IP**.
- **Example for socket creation**:
int lnSock = socket(AF_INET, SOCK_STREAM, 0);
int lnSock = socket(AF_INET, SOCK_DGRAM, 0);
```
Reserving a Port (BSD)
- To reserve a port on the local machine, use bind().
- Functionality:
bind()associates a local address (port) with a given socket. - Restriction: Only one socket can bind to a specific port at any given time.
- Example:
bind(lnSock, …, …);
```
## Client/Server Communication Model
- **Definition**: In software network communication, the **Client/Server** model typically exists, although there are exceptions such as sender/receiver or supplier/consumer.
- **Roles**:
- **Clients**: Initiators of communications.
- **Servers**: Exist to provide data/services to clients.
- **Common Architecture**: Usually, multiple clients connect to a single server, although this is not universally true.
- **Practical Example**: In a web browsing scenario, a browser like **Firefox** acts as a client connecting to a web server like **Apache** to retrieve web pages.
## Two-Way Communication via Sockets
- **Definition**: Sockets function as two-way communication channels, allowing both clients and servers to send and receive data over the same socket (illustrated as a phone line).
- **Practical Usage**:
- Example: A client can send and receive data using one socket, while a server can also send and receive data using another socket.
## UDP Clients
- **Connection Characteristics**: With UDP, there is no consistent connection between clients and servers.
- **Client Socket Setup**: For receiving, a UDP client creates a socket and binds a local port. To send data to a remote address, the **sendto()** function is utilized.
- **Destination Address Structure**: This structure holds the IP and port address information.
## Address Structures
- **Purpose**: `sockaddr` structures contain IP and port address information necessary for network communication.
- **Fill-out Example**:
- **SERVER_ADDR**: Should be in dotted format (e.g., `192.168.1.1`).
- **SERVER_PORT**: Should be an integer representing the port number.
## Setup UDP Socket for Sending
c
// get the socket setup information
SocketSetupStruct 1sSocketSetup;
Socket Setup("socketsetup.ini", &lsSocketSetup);
// socket structure to define properties
struct sockaddr_in 1sSAOther;
int InSocketId = 0;
socklen_t InSockStructLen = sizeof(1sSAOther);
char lanRecBuf[BUFLEN] = {0};
char lanMessage[BUFLEN] = {0};
// attempt to create a socket
if ((InSocketId = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
PrintErrorAndExit("socket");
}
// zero out the structure for defining destination
memset((char*)&lsSAOther, 0, sizeof(1sSAOther));
// initialize destination properties
1sSAOther.sin_family = AF_INET; // IPV4
1sSAOther.sin_port = htons(PORT); // convert port to network byte order
1sSAOther.sin_addr.s_addr = inet_addr(1sSocketSetup.lanServerAddress);
## UDP Sending Example
c
//send the Message
if (sendto(InSocketId, lanMessage, strlen(lanMessage), 0, (struct sockaddr*)&lsSAOther, InSockStructLen) == -1) {
PrintErrorAndExit("sendto ()");
}
## Setup UDP Socket for Receiving
c
// get the socket setup information
SocketSetupStruct 1sSocketSetup;
Socket Setup("socketsetup.ini", &lsSocketSetup);
// create a UDP socket
if ((InSocketId = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
PrintErrorAndExit("socket");
}
// zero out the structure
memset((char*)&lsSAME, 0, sizeof(lsSAME));
1sSAME.sin_family = AF_INET; // use IPV4 network addressing
1sSAME.sin_port = htons(1sSocketSetup.InServerPort); // convert to network byte order.
1sSAME.sin_addr.s_addr = inet_addr(1sSocketSetup.lanServerAddress);
// bind socket to our specific port
if (bind(InSocketId, (struct sockaddr*)&lsSAME, sizeof(lsSAME)) == -1) {
PrintErrorAndExit("bind");
}
## UDP Receiving Example
c
// try to receive some data, this is a blocking call
// -1 indicates an error. anything 0 or above is assumed to be the number of bytes received.
if ((InReceiveLen = recvfrom(lnSocketId, lanRecBuf, BUFLEN, 0, (struct sockaddr*)&lsSAOther, &lnSockStructLen)) == -1) {
PrintErrorAndExit("recvfrom () ");
}
```