Introduction to Information Security Networking Transmission Control Protocol














![• • • • • C-Client main int main(int argc, char *argv[]) { • • • • • C-Client main int main(int argc, char *argv[]) {](https://slidetodoc.com/presentation_image_h/061cb90458fc080a602071c8b0ceeccd/image-15.jpg)



- Slides: 18

Introduction to Information Security Networking

Transmission Control Protocol (aka TCP) • Most widely used protocol • A ‘reliable’ (but not secure!) protocol • A TCP Connection is defined by the 4 -tuple – o o Source IP Destination IP Source port Destination port • Also important – o Source byte counter o Destination byte counter

TCP/IP Continued • In order to establish a TCP connection, a 3 -way handshake occurs – o Client -> Server: SYN o Server -> Client: SYN + ACK o Client -> Server: ACK • Once the handshake is established and the connection is up and running TCP ensures – o Delivery of ALL the data in both directions o Delivery in order o Delivery with no errors

Server Implementation • The server listens on a specific port (aka the destination port) • Once a connection is established, the server can accept more connections while handling the first connection • Establish multi processing of connections can be gained by: o o Fork() – Multi process solution Multi-Threaded solutions Select() – Single process solutions Other OS specific solutions

Client implementation • Usually handles only one connection at a time. (extreme contrary example: u. Torrent) • Source port is randomized by operating system (unless application requires otherwise)

#!usr/bin/python Python client from socket import * PORT = 1948 def main(): sock = socket(AF_INET, SOCK_STREAM) sock. connect(("127. 0. 0. 1", PORT)) res = sock. recv(1000) # 1000 = Maximum number of bytes to # receive, however, it may return with less bytes # than expected print res sock. close() if __name__ == '__main__': main()

Python server example #!usr/bin/python from socket import * PORT = 1948 def main(): sock = socket(AF_INET, SOCK_STREAM) sock. bind(("0. 0", PORT)) sock. listen(10) # set backlog while 1: (client_sock, peer_info) = sock. accept() print "Got incoming connection from %s: %d " %(peer_info) res = client_sock. send("Hello, World!") client_sock. close() if __name__ == '__main__': main()

C programming reference • Most popular guide ever released for network programming is beej’s guide which is a very good starter reference o http: //beej. us/guide/bgnet/output/html/singlepage/bgnet. html

Server implementation in C (taken from beej) - includes /* ** server. c - a stream socket server demo */ #include <stdio. h> #include <stdlib. h> #include <unistd. h> #include <errno. h> #include <string. h> #include <sys/types. h> #include <sys/socket. h> #include <netinet/in. h> #include <arpa/inet. h> #include <sys/wait. h> #include <signal. h>

C-Server Cleanup and defines #define MYPORT 1948 // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold //Cleaning up dead child processes: void sigchld_handler(int s) { while(waitpid(-1, NULL, WNOHANG) > 0); }

C-Server main int main(void) { int sockfd, new_fd; // listen on sock_fd, new connection on new_fd struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector’s address information socklen_t sin_size; struct sigaction sa; int yes=1; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } my_addr. sin_family = AF_INET; // host byte order my_addr. sin_port = htons(MYPORT); // short, network byte order my_addr. sin_addr. s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr. sin_zero), '