14 - Into to Networking

Class: CSCE-313


Notes:

3rd midterm on Friday 4/24 or Mon 4/27

Try to work out problems under Weekly-HW

What is a network?

image-96.png316

Notes:

The network does the routing

image-97.png485

Notes:

The Internet: a "nuts and bolts" view

Billions of connected computing devices:

Packet switches: forward packets (chunks of data)

Communication links

Networks

image-98.png260

Notes:

"Fun" Internet-connected devices

image-99.png395

Notes:

The functionality of "networking"

Networking does much for applications.

Notes:

Rewritten notes:

Routing

image-100.png355

Packet forwarding

image-101.png354

Notes:

Examples:

┌──(macc㉿kaliLab)-[~]
└─$ ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.64.2  netmask 255.255.255.0  broadcast 192.168.64.255
        inet6 fd15:7568:1b93:9ef1:49a9:ac71:34f7:cb32  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::60cf:81ff:fe55:308d  prefixlen 64  scopeid 0x20<link>
        inet6 fd15:7568:1b93:9ef1:60cf:81ff:fe55:308d  prefixlen 64  scopeid 0x0<global>
        ether 62:cf:81:55:30:8d  txqueuelen 1000  (Ethernet)
        RX packets 129735  bytes 97878313 (93.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 111498  bytes 19126431 (18.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 3450  bytes 13001871 (12.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3450  bytes 13001871 (12.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1500
        inet 10.10.14.2  netmask 255.255.254.0  destination 10.10.14.2
        inet6 fe80::8af5:89d2:6df1:a2d0  prefixlen 64  scopeid 0x20<link>
        inet6 dead:beef:2::1000  prefixlen 64  scopeid 0x0<global>
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 500(UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20  bytes 2821 (2.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Protocol Layering

image-102.png494

Notes:

Rewritten notes:

The Open System Interconnection (OSI) model

image-103.png481

Notes:

The TCP/IP Reference Model

image-104.png378

Notes:

A client-server transaction

Most network applications are based on the client-server model.

image-105.png578

Clients and servers are processes running on hosts. They can be the same or
different hosts.

Notes:

What's a protocol?

A human protocol and a computer network protocol:
image-106.png439

Protocols define the format, order of messages sent and received among network entities, and actions taken on message transmission, receipt

Notes

Example of a client-server protocol:

curl

Example

┌──(macc㉿kaliLab)-[~]
└─$ curl -v https://www.google.com

Here is the request that was send:

* Connected to www.google.com (142.251.151.119) port 443
* using HTTP/2
* [HTTP/2] [1] OPENED stream for https://www.google.com/
* [HTTP/2] [1] [:method: GET]
* [HTTP/2] [1] [:scheme: https]
* [HTTP/2] [1] [:authority: www.google.com]
* [HTTP/2] [1] [:path: /]
* [HTTP/2] [1] [user-agent: curl/8.15.0]
* [HTTP/2] [1] [accept: */*]
> GET / HTTP/2
> Host: www.google.com
> User-Agent: curl/8.15.0
> Accept: */*
>
* Request completely sent off

Response looks like:

< HTTP/2 200
< date: Mon, 06 Apr 2026 19:36:58 GMT
< expires: -1
< cache-control: private, max-age=0
< content-type: text/html; charset=ISO-8859-1
< content-security-policy-report-only:
...

IP datagram

image-107.png444

Notes:

TCP header

image-108.png434

Notes:

A programmer's view of the Internet

Hosts are mapped to a {set} of 32-bit IP addresses

A set of identifiers called Internet domain names are mapped to the set of IP addresses for "human" convenience (Domain Name Server aka DNS)

Notes:

Example:

> nslookup www.cs.tamu.edu
Server:         128.194.254.1
Address:        128.194.254.1#53

www.cs.tamu.edu canonical name = redirect.engr.tamu.edu.
redirect.engr.tamu.edu  canonical name = d3s3b8hbw2bor0.cloudfront.net.
Name:   d3s3b8hbw2bor0.cloudfront.net
Address: 3.169.221.28
Name:   d3s3b8hbw2bor0.cloudfront.net
Address: 3.169.221.94
Name:   d3s3b8hbw2bor0.cloudfront.net
Address: 3.169.221.95
Name:   d3s3b8hbw2bor0.cloudfront.net
Address: 3.169.221.22
> dig +short www.google.com
142.251.152.119
142.251.153.119
142.251.154.119
142.251.155.119
142.251.156.119
142.251.157.119
142.251.150.119
142.251.151.119

IP Addresses

32-bit IP addresses are stored in an IP address struct

/* Internet address structure */
struct in_addr {
    in_addr_t s_addr; /* network byte order (big-endian) */ 
};

Handy network byte-order conversion functions:

Notes:

Byte order for shorts and ints

image-109.png536

#include <stdio.h>
#include <arpa/inet.h>
int main() {
    uint32_t a = 0x01020304;
    char *p = (char *) &a;
    printf("%x %x %x %x\n", p[0], p[1], p[2], p[3]),
    a = htonl(a);
    printf("%x %x %x %x\n", p[0], p[1], p[2], p[3]);
    return 0;
}

Notes:

Result:

Host BO: 4 3 2 1
Network BO: 1 2 3 4
inet_ntop: 1.2.3.4.

Dotted Decimal Notation

By convention, each byte in a 32-bit IP address is represented by its decimal value and separated by a period

Functions for converting between binary IP addresses and dotted decimal strings:

Notes:

Internet domain names

image-110.png521

Notes:

Querying DNS

Domain Information Groper (dig) provides a scriptable command line interface to DNS

Lots of web interfaces (google "domain information groper")

unix> dig +short linux2.cse.tamu.edu
128.194.166.90

unix> dig +short -x 128.194.166.90
linux2.engr.tamu.edu.
linux2.cse.tamu.edu.
linux2.cs.tamu.edu.

unix> dig +short www.google.com
142.250.114.104
142.250.114.99
142.250.114.106
142.250.114.147
142.250.114.105
142.250.114.103

Notes:

Example:

> dig linux2.cse.tamu.edu

; <<>> DiG 9.10.6 <<>> linux2.cse.tamu.edu
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53704
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 3, ADDITIONAL: 5

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1220
;; QUESTION SECTION:
;linux2.cse.tamu.edu.           IN      A

;; ANSWER SECTION:
linux2.cse.tamu.edu.    3600    IN      A       128.194.166.90

;; AUTHORITY SECTION:
cse.tamu.edu.           3600    IN      NS      csce-info-grid.net.tamu.edu.
cse.tamu.edu.           3600    IN      NS      wemr-info-dhcp.net.tamu.edu.
cse.tamu.edu.           3600    IN      NS      csce-info-dhcp.net.tamu.edu.

;; ADDITIONAL SECTION:
wemr-info-dhcp.net.tamu.edu. 3600 IN    A       128.194.211.237
csce-info-grid.net.tamu.edu. 3600 IN    A       165.91.16.132
csce-info-dhcp.net.tamu.edu. 3600 IN    A       128.194.169.147
csce-info-dhcp.net.tamu.edu. 3600 IN    A       165.91.16.135

;; Query time: 12 msec
;; SERVER: 128.194.254.1#53(128.194.254.1)
;; WHEN: Wed Apr 08 14:21:12 CDT 2026
;; MSG SIZE  rcvd: 227

getaddrinfo - name -> ip mapping

image-111.png553

getaddrinfo("www.google.com", NULL, &hints, &addrinfo);

Notes:

Example:

// Get a list of addrinfo records
memset (&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;    // IPv4 only
hints.ai_socktype = SOCK_STREAM;    // Connections only
if ((rc = getaddrinfo(host, NULL, &hints, &lisp)) != 0) {
	fprintf(stderr, "getaddrinfo error")
	ecit(1);
}

// Walk your rc linked list

getaddrinfo

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

struct addrinfo *addrinfo;
struct addrinfo hints;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* IPv4 only */
hints.ai_socktype = SOCK_STREAM; / * Connections only */
getaddrinfo("www.google.com", NULL, &hints, &addrinfo);

Example:

Domain Naming System

Functions for retrieving host entries from DNS:

struct addrinfo{
    int ai_flags;       /* flags for getaddrinfo */
    int ai_family;      /* address type(AF_INET or AF_INET6) */
    int ai_socktype;    /* the socket type */
    int ai_protocol;    /* the type of protocol */
    size_t ai_addrlen;  /* length of ai_addr */
    struct sockaddr *ai_addr; /* pointer to a sockaddr struct */
    char *ai_canonname;       /* the canonical name */
    struct addrinfo *ai_next; /* pointer to the next addrinfo struct */
};

image-112.png

Notes:

sockaddr = socket address

Base:

struct sockaddr {
	sa_family_t sa_family;
	char sa_data[14];
};

Derived:

struct sockaddr_in {
	short sin_family; // IPv4 or IPv6
	unsigned short sin_port; // port number
	struct in_addr sin_addr; // 32 bit IP
	char sin_zero[8];
};

Notes:


HW1 Q1 - Networking

Problem 1

Try all of these questions!


Anatomy of an internet connection

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-49.png568

Notes:

If you look at /etc/services you will see that http is defined as 80/tcp:

cat /etc/services | more
...
http 80/udp www www-http # World Wide Web HTTP 
http 80/tcp www www-http # World Wide Web HTTP
...

Using Ports to Identify Services

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-50.png562

Notes:

Servers

Servers are long-running processes (daemons)

Each server waits for requests to arrive on a well-known port associated with a particular service

A machine that runs a server process is also often referred to as a "server"

Notes:

Server examples

Web server (port 80)

FTP server (20, 21)

Telnet server (23)

Mail server (25)

Notes: See /etc/services for a comprehensive list
of the services available on a UNIX machine

Notes:

Organization of an internet application

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-51.png583

Notes:

Network Programming with Sockets

Might see BSD Sockets: A Quick And Dirty Primer

Notes:

Stream sockets

image-52.png576x297

Notes:

Binding a socket?

Notes:

A Server-Client interaction in TCP - POSIX functions

image-53.png581x402

Notes:

Rewritten notes:

Accepting a new connection

image-54.png491

Notes:

Echo Server: accept illustrates

image-55.png

Notes:

Function: socket

int socket(int family, int type, int protocol)

Notes:

Example: socket

int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address */
struct sockaddr_in their_addr; /* connector addr */
int sin_size;

if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
}

Notes:

Function: bind

int bind(int sockfd, sockaddr* myaddr, int addrlen)

Notes:

Function: listen

Put it in passive mode

int listen(int sockfd, int backlog)

Example:

if (listen(sockfd, BACKLOG) == -1) {
    perror("listen");
    exit(1);
}

Notes:

Function: accept

int accept(int sockfd, sockaddr* cliaddr, int* addrlen);

Notes:

Function: connect

int connect (int sockfd, sockaddr* servaddr, int addrlen)

Notes:

Example server code (tcp-echo-server)

image-56.png

int main (int argc, char *argv[]) [
    int port = atoi(argv[1]);
    
    int server_fd, client_fd, err;
    struct sockaddr_in server, client;
    char buf[BUFFER_SIZE];
    
    server_fd = socket(AF_INET, SOCK_STREAM,0);
    
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    
    int opt_val = 1;
    setsockopt(server fd, SOL SOCKET, SO REUSEADDR, &opt val, sizeof opt val);
    bind(server_fd, (struct sockaddr *) &server, sizeof(server));
    
    listen(server_fd, 128);
    printf("Server is listening on %d\n", port);
    
    while (1) {
        socklen t client len = sizeof(client);
        client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len);
        
        if(client_fd < 0) on_error("Could not establish new connection\n");
        while (1) {
            irf{ read = recv(client_fd, buf, BUFFER_SIZE, 0);
            
            if (!read) break; // done reading
            if (read < 0) on_error("Client read falled\n");
            
            send(client_fd, buf, read, 0);a
        }
    }
    
    return 0;
}

Notes:

Example client code (tcp-client)

image-57.png

int main(int argc, char *argv[]) {
    char *ip = argv[1];
    int port = atoi(argv[2]);
    int sock;
    struct sockaddr_in addr;
    socklen_t addr_size;
    char buf[BUFFER_SIZE];
    int err, n;
    
    spck = socket(AF INET, SOCK STREAM,0);
    if (sock < 0) on_error("Could not create socket.\n");
    
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip);
    
    if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
        on_error("Failed to connect.\n");
    printf("Connected to the server.\n");
    
    while (1) {
        // read & write from sock
    }
    
    close(sock);
    return 0;
}

Notes:

TCP Connection Example

image-58.png524

Notes:

Example: writen

image-59.png

/* Write "n" bytes to a descriptor */
ssize_t writen(int fd, const void *ptr, size_t n) {
    size_t nleft;
    ssize_t nwritten;
    nleft = n;
    while (nleft > 0) {
        if ((nwritten = write(fd, ptr, nleft)) < 0) {
            if (nleft == n)
                return(-1); /* error, return -1 */
            else
                break; /* error, return amount written so far */
        }
        else if(nwritten == 0)
            break;
        nleft -= nwritten;
        ptr += nwritten;
    }
    return(n - nleft); /* return >= 0 */
}

Notes:

Example: readn

image-60.png

/* Read "n" bytes from a descriptor */
ssize_t readn(int fd, void *ptr, size_t n) {
    size_t nleft;
    ssize_t nread;
    nleft = n;
    while (nleft > 0) {
        if ((nread = read(fd, ptr, nleft)) < 0) {
            if (nleft == n)
                return(-1); /* error, return -1 */
            else
                break; /* error, return amt read */
        }
        else if(nread == 0)
                    break; /* EOF */
            nleft -= nread;
            ptr += nread;
    }
    return(n - nleft); /* return >= 0 */
}

Notes:

Server: setsockopt()

The socket can be given some attributes

...
/* Eliminates "Address already in use" error from bind(). */
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
        (const void *) &optval , sizeof optval) == -1)
    return -1;

Handy trick that allows us to rerun the server immediately after we kill it

Strongly suggest you do this for all your servers to simplify debugging

Notes: