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_h2/8d40dea4f1b2b0598ab5195cdd8cee3a/image-13.jpg)





- Slides: 18

Introduction to Information Security Networking

Transmission Control Protocol (aka TCP) • Most widely used protocol • A TCP Connection is based on 6 crucial parmeters: o o o Source IP Destination IP Source port Destination port Source byte counter Destination byte counter

TCP/IP Continued • In order to establish a tcp connection a 3 -way handshake occurs. • Once the handshake is establish and the connection is up and running tcp ensures: o Delivery of _all_ packets o Delivery of 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.

Network Programming • 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 3490 // 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), '