Concurrent HTTP Proxy Server. CS425 - Computer Networks Vaibhav Nagar(14785)

Similar documents
Socket Programming. Daniel Zappala. CS 360 Internet Programming Brigham Young University

Antonio Falabella. 3 rd nternational Summer School on INtelligent Signal Processing for FrontIEr Research and Industry, September 2015, Hamburg

CS 3411 Systems Programming

Basics HTTP. requests. Ryan E. Freckleton. PySprings. May 24, 2016

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

Computer Networks ( Classroom Practice Booklet Solutions)

CS 425 / ECE 428 Distributed Systems Fall Indranil Gupta (Indy) Oct. 5, 2017 Lecture 12: Time and Ordering All slides IG

Outline F eria AADL behavior 1/ 78

Monitoring System for ALICE Surface Areas

CSE 123: Computer Networks

Solutions to the second midterm. COSC 4330/6310 Summer 2013

MODBUS Protocol for CS141 network card families

1 Opening URLs. 2 Regular Expressions. 3 Look Back. 4 Graph Theory. 5 Crawler / Spider

Double slit questions

Arup Nanda Starwood Hotels

Overlay Transport Virtualization (OTV) Unicast-Mode Transport Infrastructure Deployment

Inter-Reactive Kotlin Applications

Databases through Python-Flask and MariaDB

Växjö University. Software Security Testing. A Flexible Architecture for Security Testing. School of Mathematics and System Engineering

Part I. System call overview. Secure Operating System Design and Implementation System Calls. Overview. Abstraction. Jon A. Solworth.

Verification of a Dynamic Channel Model using the SPIN Model Checker

Knocking down the HACIENDA with TCP Stealth

Administrivia. Course Objectives. Overview. Lecture Notes Week markem/cs333/ 2. Staff. 3. Prerequisites. 4. Grading. 1. Theory and application

Distributed Systems Principles and Paradigms

Introduction to ArcGIS Server Development

Design Patterns and Refactoring

Distributed Systems Principles and Paradigms. Chapter 06: Synchronization

Python. chrysn

CS 3411 Systems Programming

Computer Architecture

Troubleshooting Replication and Geodata Services. Liz Parrish & Ben Lin

Introductory MPI June 2008

Advanced Topicson Network Socket Programming

CS 3411 Systems Programming

The Hyper Text Transfer Protocol (HTTP)

Troubleshooting Replication and Geodata Service Issues

Agreement. Today. l Coordination and agreement in group communication. l Consensus

Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

Parallel Ray Tracer TJHSST Senior Research Project Computer Systems Lab

Local stageout update

Experiments with Measuring Time in PRISM 4.0 (Addendum)

High-performance processing and development with Madagascar. July 24, 2010 Madagascar development team

Lecture 20: Lower Bounds for Inner Product & Indexing

Numerical differentiation

Android Services. Lecture 4. Operating Systems Practical. 26 October 2016

CPSA and Formal Security Goals

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Introduction to MPI. School of Computational Science, 21 October 2005

Android Security Mechanisms (2)

dnode remote execution in the kingdom of node.js James Halliday (substack) March 22, 2011

CS429: Computer Organization and Architecture

Lecture on Sensor Networks

CS425: Algorithms for Web Scale Data

E23: Hotel Management System Wen Yunlu Hu Xing Chen Ke Tang Haoyuan Module: EEE 101

Problem Set 5 Solutions

PARASITIC COMPUTING: PROBLEMS AND ETHICAL

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

2IN35 VLSI Programming Lab Work Communication Protocols: A Synchronous and an Asynchronous One

Time, Clocks, and the Ordering of Events in a Distributed System

PHP Extension Development with C++

Scripting Languages Fast development, extensible programs

CS 347 Parallel and Distributed Data Processing

TECDIS and TELchart ECS Weather Overlay Guide

CS 7B - Spring Assignment: Adapting the calculator for bitwise expressions. due 2/21/18

TDDD63 Project: API. September 13, 2014

MCS 260 Exam 2 13 November In order to get full credit, you need to show your work.

Lab Manual for ICEN 553/453 Cyber-Physical Systems Fall 2018

Replication cluster on MariaDB 5.5 / ubuntu-server. Mark Schneider ms(at)it-infrastrukturen(dot)org

Solutions. 9. Ans. C. Memory size. 11. Ans. D. a b c d e f a d e b c f a b d c e f a d b c e f a b d e c f a d b e c f. 13. Ans. D. Merge sort n log n

Real Time Operating Systems

CS5371 Theory of Computation. Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)

Networked Control Systems

Distributed Systems. Time, Clocks, and Ordering of Events

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Lecture 24 - MPI ECE 459: Programming for Performance

High-performance Technical Computing with Erlang

ST-Links. SpatialKit. Version 3.0.x. For ArcMap. ArcMap Extension for Directly Connecting to Spatial Databases. ST-Links Corporation.

Appendix 4 Weather. Weather Providers

Modeling Concurrent Systems

Slides for Chapter 14: Time and Global States

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

Automata and Computability. Solutions to Exercises

Recursion. Recursion. Steven Zeil. November 25, 2013

B629 project - StreamIt MPI Backend. Nilesh Mahajan

A Note on Turing Machine Design

Robust Programs with Filtered Iterators

FACTORS AFFECTING CONCURRENT TRUNCATE

A Thread Algebra with Multi-level Strategic Interleaving

Smart Home System using Rasperry Pi

a fast correlation attack implementation

IEEE C /058r3

CS5371 Theory of Computation. Lecture 10: Computability Theory I (Turing Machine)

Exchange ActiveSync: AirSyncBase Namespace Protocol

Extending MISP with Python modules MISP - Malware Information Sharing Platform & Threat Sharing

4th year Project demo presentation

Quantum Socks and Photon Polarization Revision: July 29, 2005 Authors: Appropriate Level: Abstract:

Knocking down the HACIENDA with TCP Stealth

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

BTstack Getting Started. Using MSP430 Examples. Dr. sc. Milanka Ringwald Dr. sc. Matthias Ringwald

Lecture 8: The Dummy Adversary

Transcription:

Concurrent HTTP Proxy Server CS425 - Computer Networks Vaibhav Nagar(14785) Email: vaibhavn@iitk.ac.in August 31, 2016 Elementary features of Proxy Server Proxy server supports the GET method to serve clients requests and then connects to requested host and responds with host data. It can handle requests from both protocols HTTP/1.1 and HTTP/1.0. Multiple clients can connect and make request to it at the same time. Server perfectly provides appropriate Status-code and Response-Phrase values in response to errors or incorrect requests from client. Server is designed such that it can run continuously until an unrecoverable error occurs. The whole code is developed only in C language using its various libraries and string parsing library. 1

Design Choices The code is written in imperative manner which uses a string parsing library. In contrast to Apache server which has fixed buffer size of 8KB, buffer size of my proxy server is fixed to 4KB for handling requests from client. Proxy server blocks after the connection from client is established until it gets double carriage return in the request made. Proxy server forks one new child process to handle a single GET request from the client. Proxy server uses the same 4KB buffer for storing the data from requested host and send to the client. Proxy server ensures two headers- Host and Connection: Close must be sent to the requested host. Test Procedure Testing has been done on the VM provided and as well as on the Ubuntu 14.04 machine. Mozilla Firefox is used as a client which is on the same machine and as well as on different machines in the same network. Moreover it has been tested using telnet and curl as well. Server smoothly handled all the requests made by client during testing and provided the correct responses from requested host. Figure 1: HTTP request from firefox to proxy server 2

Figure 2: Image request from client Figure 3: 500:Internal server error response for HTTPS request 3

Summary Proxy Server successfully handles all GET requests made by client and create new child processes for each new request. Supports both protocols- HTTP 1.1 and 1.0. Server sends appropriate status code and response phrase message depending upon the type of error and request. However, some test cases in python testing scripts which are checking the proxy response and direct response line by line due to which they were not able to passed. Line by line check is not reliable as some headers like Age, Date, Keep-alive header contains a max field whose values change with each new request. Also I was facing some issue with http://example.com/ website which is taking too much time to get data from python script. Then after editing the script such that it makes only direct request to example.com, then I realized it was the issue of the script itself and not my proxy server. In python test.py script, my proxy server passed 3/4 or sometimes 4/4 test cases whereas, in python test conc.py script, my server passed 11/12 when I removed the website example.com from its urls array as it was taking too much time to respond. 4

Appendix Source Code 1 2 3 #i n c l u d e proxy parse. h 4 5 #i n c l u d e <s t d i o. h> 6 #i n c l u d e <s t d l i b. h> 7 #i n c l u d e <s t r i n g. h> 8 #i n c l u d e <sys / types. h> 9 #i n c l u d e <sys / s o c k e t. h> 10 #i n c l u d e <n e t i n e t / in. h> 11 #i n c l u d e <netdb. h> 12 #i n c l u d e <arpa / i n e t. h> 13 #i n c l u d e <unistd. h> 14 #i n c l u d e <f c n t l. h> 15 #i n c l u d e <time. h> 16 #i n c l u d e <sys / wait. h> 17 #i n c l u d e <errno. h> 18 19 #d e f i n e MAX BYTES 4096 20 #d e f i n e MAX CLIENTS 400 21 22 i n t port = 5100; // Default Port 23 i n t s o c k e t I d ; // Server Socket ID 24 25 p i d t c l i e n t P I D [MAX CLIENTS ] ; // PID o f connected c l i e n t s 26 27 28 i n t senderrormessage ( i n t socket, i n t s t a t u s c o d e ) 29 { 30 char s t r [ 1 0 2 4 ] ; 31 char currenttime [ 5 0 ] ; 32 t i m e t now = time ( 0 ) ; 33 34 s t r u c t tm data = gmtime(&now) ; 35 s t r f t i m e ( currenttime, s i z e o f ( currenttime ), %a, %d %b %Y %H:%M:%S %Z, &data ) ; 36 37 switch ( s t a t u s c o d e ) 38 { 39 case 400: s n p r i n t f ( s t r, s i z e o f ( s t r ), HTTP/1.1 400 Bad Request \ r \ncontent Length : 95\ r \ nconnection : keep a l i v e \ r \ncontent Type : t e x t /html\ r \ndate : %s \ r \ n S e r v e r : VaibhavN / 14785\ r \n\ r \n<html><head><title>400 Bad Request </TITLE></HEAD >\n<body><h1>400 Bad Rqeuest </H1>\n</BODY></HTML>, currenttime ) ; 40 p r i n t f ( 400 Bad Request \n ) ; 41 send ( socket, s t r, s t r l e n ( s t r ), 0) ; 42 break ; 43 44 case 403: s n p r i n t f ( s t r, s i z e o f ( s t r ), HTTP/1.1 403 Forbidden \ r \ncontent Length : 112\ r \ncontent Type : t e x t /html\ r \ nconnection : keep a l i v e \ r \ndate : %s \ r \ nserver : VaibhavN / 14785\ r \n\ r \n<html><head><title>403 Forbidden </TITLE></HEAD>\n<BODY>< H1>403 Forbidden </H1><br>P e r m i s s i o n Denied \n</body></html>, currenttime ) ; 5

45 p r i n t f ( 403 Forbidden \n ) ; 46 send ( socket, s t r, s t r l e n ( s t r ), 0) ; 47 break ; 48 49 case 404: s n p r i n t f ( s t r, s i z e o f ( s t r ), HTTP/1.1 404 Not Found\ r \ncontent Length : 91\ r \ncontent Type : t e x t /html\ r \ nconnection : keep a l i v e \ r \ndate : %s \ r \ nserver : VaibhavN / 14785\ r \n\ r \n<html><head><title>404 Not Found</TITLE></HEAD>\n<BODY>< H1>404 Not Found</H1>\n</BODY></HTML>, currenttime ) ; 50 p r i n t f ( 404 Not Found\n ) ; 51 send ( socket, s t r, s t r l e n ( s t r ), 0) ; 52 break ; 53 54 case 500: s n p r i n t f ( s t r, s i z e o f ( s t r ), HTTP/1.1 500 I n t e r n a l Server Error \ r \ ncontent Length : 115\ r \ nconnection : keep a l i v e \ r \ncontent Type : t e x t /html\ r \ ndate : %s \ r \ nserver : VaibhavN /14785\ r \n\ r \n<html><head><title>500 I n t e r n a l Server Error </TITLE></HEAD>\n<BODY><H1>500 I n t e r n a l Server Error </H1>\n</BODY></ HTML>, currenttime ) ; 55 // p r i n t f ( 500 I n t e r n a l Server Error \n ) ; 56 send ( socket, s t r, s t r l e n ( s t r ), 0) ; 57 break ; 58 59 case 501: s n p r i n t f ( s t r, s i z e o f ( s t r ), HTTP/1.1 501 Not Implemented\ r \ncontent Length : 103\ r \ nconnection : keep a l i v e \ r \ncontent Type : t e x t /html\ r \ndate : %s \ r \ n S e r v e r : VaibhavN / 14785\ r \n\ r \n<html><head><title>404 Not Implemented </TITLE></ HEAD>\n<BODY><H1>501 Not Implemented </H1>\n</BODY></HTML>, currenttime ) ; 60 p r i n t f ( 501 Not Implemented\n ) ; 61 send ( socket, s t r, s t r l e n ( s t r ), 0) ; 62 break ; 63 64 case 505: s n p r i n t f ( s t r, s i z e o f ( s t r ), HTTP/1.1 505 HTTP Version Not Supported \ r \ncontent Length : 125\ r \ nconnection : keep a l i v e \ r \ncontent Type : t e x t /html\ r \ ndate : %s \ r \ nserver : VaibhavN /14785\ r \n\ r \n<html><head><title>505 HTTP Version Not Supported </TITLE></HEAD>\n<BODY><H1>505 HTTP V e r s i o n Not Supported </H1>\n</ BODY></HTML>, currenttime ) ; 65 p r i n t f ( 505 HTTP Version Not Supported \n ) ; 66 send ( socket, s t r, s t r l e n ( s t r ), 0) ; 67 break ; 68 69 d e f a u l t : r e turn 1; 70 71 } 72 73 return 1 ; 74 } 75 76 77 78 i n t connectremoteserver ( char host addr, i n t port num ) 79 { 80 // Creating Socket f o r remote s e r ver 81 82 i n t remotesocket = s o c k e t (AF INET, SOCK STREAM, 0) ; 83 84 i f ( remotesocket < 0) 6

85 { 86 p r i n t f ( Error in Creating Socket. \ n ) ; 87 return 1; 88 } 89 90 // Get host by the name or ip address provided 91 92 s t r u c t hostent host = gethostbyname ( host addr ) ; 93 i f ( host == NULL) 94 { 95 f p r i n t f ( s t d e r r, No such host e x i s t s. \ n ) ; 96 return 1; 97 } 98 99 // i n s e r t s ip address and port number o f host in s t r u c t s e r v e r a d d r 100 s t r u c t s o c k a d d r i n s e r v e r a d d r ; 101 102 bzero ( ( char )&s e r v e r a d d r, s i z e o f ( s e r v e r a d d r ) ) ; 103 s e r v e r a d d r. s i n f a m i l y = AF INET ; 104 s e r v e r a d d r. s i n p o r t = htons ( port num ) ; 105 106 bcopy ( ( char ) host >h addr, ( char )&s e r v e r a d d r. s i n a d d r. s addr, host >h l e n g t h ) ; 107 108 // Connect t o Remote s e r v e r 109 110 i f ( connect ( remotesocket, ( s t r u c t sockaddr )&s e r v e r a d d r, ( s o c k l e n t ) s i z e o f ( s e r v e r a d d r ) ) < 0 ) 111 { 112 f p r i n t f ( s t d e r r, Error in connecting! \ n ) ; 113 return 1; 114 } 115 116 return remotesocket ; 117 } 118 119 120 i n t handlegetrequest ( i n t c l i e n t S o c k e t, ParsedRequest request, char buf ) 121 { 122 s t r c p y ( buf, GET ) ; 123 s t r c a t ( buf, request >path ) ; 124 s t r c a t ( buf, ) ; 125 s t r c a t ( buf, request >v e r s i o n ) ; 126 s t r c a t ( buf, \ r \n ) ; 127 128 s i z e t l e n = s t r l e n ( buf ) ; 129 130 i f ( ParsedHeader set ( request, Connection, c l o s e ) < 0) { 131 p r i n t f ( s e t header key not work\n ) ; 132 // return 1; // I f t h i s happens S t i l l try to send r e q u e s t without header 133 } 134 135 i f ( ParsedHeader get ( request, Host ) == NULL) 136 { 7

137 i f ( ParsedHeader set ( request, Host, request >host ) < 0) { 138 p r i n t f ( Set \ Host \ header key not working \n ) ; 139 } 140 } 141 142 i f ( ParsedRequest unparse headers ( request, buf + len, ( s i z e t )MAX BYTES l e n ) < 0) { 143 p r i n t f ( unparse f a i l e d \n ) ; 144 // return 1; // I f t h i s happens S t i l l try to send r e q u e s t without header 145 } 146 147 148 i n t s e r v e r p o r t = 8 0 ; // Default Remote Server Port 149 i f ( request >port!= NULL) 150 s e r v e r p o r t = a t o i ( request >port ) ; 151 152 i n t remotesocketid = connectremoteserver ( request >host, s e r v e r p o r t ) ; 153 154 i f ( remotesocketid < 0) 155 return 1; 156 157 i n t bytes s e n d = send ( remotesocketid, buf, s t r l e n ( buf ), 0) ; 158 159 bzero ( buf, MAX BYTES) ; 160 161 bytes send = recv ( remotesocketid, buf, MAX BYTES 1, 0) ; 162 163 while ( b ytes s e n d > 0) 164 { 165 bytes s e n d = send ( c l i e n t S o c k e t, buf, bytes send, 0) ; 166 167 i f ( b ytes s e n d < 0) 168 { 169 p e r r o r ( Error in sending data to c l i e n t s o c k e t. \ n ) ; 170 break ; 171 } 172 173 bzero ( buf, MAX BYTES) ; 174 175 bytes s e n d = recv ( remotesocketid, buf, MAX BYTES 1, 0) ; 176 177 178 } 179 p r i n t f ( Done\n ) ; 180 181 bzero ( buf, MAX BYTES) ; 182 183 c l o s e ( remotesocketid ) ; 184 185 return 0 ; 186 187 } 188 8

189 190 i n t checkhttpversion ( char msg) 191 { 192 i n t v e r s i o n = 1; 193 194 i f ( strncmp (msg, HTTP/ 1.1, 8) == 0) 195 { 196 v e r s i o n = 1 ; 197 } 198 e l s e i f ( strncmp (msg, HTTP/ 1.0, 8) == 0) 199 { 200 v e r s i o n = 1 ; // Handling t h i s s i m i l a r to v e r s i o n 1. 1 201 } 202 e l s e 203 v e r s i o n = 1; 204 205 return v e r s i o n ; 206 } 207 208 209 210 i n t requesttype ( char msg) 211 { 212 i n t type = 1; 213 214 i f ( strncmp (msg, GET\0, 4 ) == 0) 215 type = 1 ; 216 e l s e i f ( strncmp (msg, POST\0, 5 ) == 0) 217 type = 2 ; 218 e l s e i f ( strncmp (msg, HEAD\0, 5 ) == 0) 219 type = 3 ; 220 e l s e 221 type = 1; 222 223 return type ; 224 } 225 226 227 228 void respondclient ( i n t s o c k e t ) 229 { 230 231 i n t bytes send, l e n ; // Bytes Transferred 232 233 234 char b u f f e r = ( char ) c a l l o c (MAX BYTES, s i z e o f ( char ) ) ; // Creating b u f f e r o f 4kb f o r a c l i e n t 235 236 // bzero ( b u f f e r, MAX BYTES) ; // Make b u f f e r zero 237 238 bytes send = recv ( socket, b u f f e r, MAX BYTES, 0) ; // Receive Request 239 240 while ( b ytes s e n d > 0) 241 { 9

242 l e n = s t r l e n ( b u f f e r ) ; 243 i f ( s t r s t r ( b u f f e r, \ r \n\ r \n ) == NULL) 244 { 245 // p r i n t f ( Carriage Return Not found! \ n ) ; 246 bytes s e n d = recv ( socket, b u f f e r + len, MAX BYTES len, 0) ; 247 } 248 e l s e { 249 break ; 250 } 251 } 252 253 i f ( bytes send > 0) 254 { 255 // p r i n t f ( %s \n, b u f f e r ) ; 256 l e n = s t r l e n ( b u f f e r ) ; 257 258 // Create a ParsedRequest to use. This ParsedRequest 259 // i s dynamically a l l o c a t e d. 260 ParsedRequest req = ParsedRequest create ( ) ; 261 262 i f ( ParsedRequest parse ( req, b u f f e r, l e n ) < 0) 263 { 264 senderrormessage ( socket, 500) ; // 500 i n t e r n a l e r r o r 265 p r i n t f ( parse f a i l e d \n ) ; 266 } 267 e l s e 268 { 269 270 bzero ( b u f f e r, MAX BYTES) ; 271 272 i n t type = requesttype ( req >method ) ; 273 274 i f ( type == 1) // GET Request 275 { 276 i f ( req >host && req >path && ( checkhttpversion ( req >v e r s i o n ) == 1) ) 277 { 278 bytes s e n d = handlegetrequest ( socket, req, b u f f e r ) ; // Handle GET r e q u e s t 279 i f ( b y t e s s e n d == 1) 280 { 281 senderrormessage ( socket, 500) ; 282 } 283 } 284 285 e l s e 286 senderrormessage ( socket, 500) ; // 500 I n t e r n a l Error 287 288 } 289 e l s e i f ( type == 2) // POST Request 290 { 291 p r i n t f ( POST: Not implemented \n ) ; 292 senderrormessage ( socket, 500) ; 293 } 294 e l s e i f ( type == 3) // HEAD Request 10

295 { 296 p r i n t f ( HEAD: Not implemented \n ) ; 297 senderrormessage ( socket, 500) ; 298 } 299 e l s e // Unknown Method Request 300 { 301 p r i n t f ( Unknown Method : Not implemented \n ) ; 302 senderrormessage ( socket, 500) ; 303 } 304 305 306 } 307 308 ParsedRequest destroy ( req ) ; 309 310 } 311 312 i f ( bytes s e n d < 0) 313 { 314 p e r r o r ( Error in r e c e i v i n g from c l i e n t. \ n ) ; 315 } 316 e l s e i f ( b y t e s s e n d == 0) 317 { 318 p r i n t f ( C l i e n t d i s c o n n e c t e d! \ n ) ; 319 } 320 321 shutdown ( socket, SHUT RDWR) ; 322 c l o s e ( s o c k e t ) ; // Close s o c k e t 323 f r e e ( b u f f e r ) ; 324 return ; 325 } 326 327 328 329 i n t f i n d A v a i l a b l e C h i l d ( i n t i ) 330 { 331 i n t j = i ; 332 p i d t r e t p i d ; 333 i n t c h i l d s t a t e ; 334 335 do 336 { 337 i f ( c l i e n t P I D [ j ] == 0) 338 return j ; 339 e l s e 340 { 341 r e t p i d = waitpid ( c l i e n t P I D [ j ], &c h i l d s t a t e, WNOHANG) ; // Finds s t a t u s change o f pid 342 343 i f ( r e t p i d == c l i e n t P I D [ j ] ) // Child e x i t e d 344 { 345 c l i e n t P I D [ j ] = 0 ; 346 return j ; 347 } 11

348 e l s e i f ( r e t p i d == 0) // Child i s s t i l l running 349 { 350 ; 351 } 352 e l s e 353 p e r r o r ( Error in waitpid c a l l \n ) ; 354 } 355 j = ( j +1)%MAX CLIENTS; 356 } 357 while ( j!= i ) ; 358 359 return 1; 360 } 361 362 363 364 i n t main ( i n t argc, char argv [ ] ) { 365 366 i n t newsocket, c l i e n t l e n ; 367 368 s t r u c t s o c k a d d r i n s e r v e r a d d r, c l i e n t a d d r ; 369 370 bzero ( client PID, MAX CLIENTS) ; 371 372 // Fetching Arguments 373 i n t params = 1 ; 374 375 i f ( argc == 2) 376 { 377 port = a t o i ( argv [ params ] ) ; 378 } 379 e l s e 380 { 381 p r i n t f ( Wrong Arguments! Usage : %s <port number>\n, argv [ 0 ] ) ; 382 e x i t ( 1 ) ; 383 } 384 385 p r i n t f ( S e t t i n g Proxy Server Port : %d\n, port ) ; 386 387 388 // Creating socket 389 390 s o c k e t I d = s o c k e t (AF INET, SOCK STREAM, 0) ; 391 392 i f ( s o c k e t I d < 0) 393 { 394 p e r r o r ( Error in Creating Socket. \ n ) ; 395 e x i t ( 1 ) ; 396 } 397 12

398 i n t r e u s e =1; 399 i f ( s e t s o c k o p t ( socketid, SOL SOCKET, SO REUSEADDR, ( const char )&reuse, s i z e o f ( r e u s e ) ) < 0) 400 p e r r o r ( s e t s o c k o p t (SO REUSEADDR) f a i l e d \n ) ; 401 402 // 403 404 // Binding s o c k e t with given port number and s e r v e r i s s e t to connect with any ip address 405 406 bzero ( ( char )&s e r v e r a d d r, s i z e o f ( s e r v e r a d d r ) ) ; 407 s e r v e r a d d r. s i n f a m i l y = AF INET ; 408 s e r v e r a d d r. s i n p o r t = htons ( port ) ; 409 s e r v e r a d d r. s i n a d d r. s addr = INADDR ANY; 410 411 i f ( bind ( socketid, ( s t r u c t sockaddr )&s e r v e r a d d r, s i z e o f ( s e r v e r a d d r ) ) < 0 ) 412 { 413 p e r r o r ( Binding Error : Port may not be f r e e. Try Using d i f f r e n t port number. \ n ) ; 414 e x i t ( 1 ) ; 415 } 416 417 p r i n t f ( Binding s u c c e s s f u l on port : %d\n, port ) ; 418 419 // 420 421 // L i s t e n i n g f o r c o n n e c t i o n s and accept upto MAX CLIENTS in queue 422 423 i n t s t a t u s = l i s t e n ( socketid, MAX CLIENTS) ; 424 425 i f ( s t a t u s < 0 ) 426 { 427 p e r r o r ( Error in L i s t e n i n g! \ n ) ; 428 e x i t ( 1 ) ; 429 } 430 431 // 432 433 // I n f i n i t e Loop f o r a c c e p t i n g connections 434 435 i n t i =0; 436 i n t r e t ; 437 438 while ( 1 ) 439 { 440 // p r i n t f ( L i s t e n i n g f o r a c l i e n t to connect! \ n ) ; 13

441 bzero ( ( char )&c l i e n t a d d r, s i z e o f ( c l i e n t a d d r ) ) ; // Clears s t r u c t c l i e n t a d d r 442 c l i e n t l e n = s i z e o f ( c l i e n t a d d r ) ; 443 444 newsocket = accept ( socketid, ( s t r u c t sockaddr )&c l i e n t a d d r, ( s o c k l e n t )& c l i e n t l e n ) ; // Accepts connection 445 i f ( newsocket < 0) 446 { 447 f p r i n t f ( s t d e r r, Error in Accepting connection! \ n ) ; 448 e x i t ( 1 ) ; 449 } 450 451 452 // Getting IP address and port number o f c l i e n t 453 454 s t r u c t s o c k a d d r i n c l i e n t p t = ( s t r u c t s o c k a d d r i n )&c l i e n t a d d r ; 455 s t r u c t i n a d d r i p a d d r = c l i e n t p t >s i n a d d r ; 456 char s t r [INET ADDRSTRLEN ] ; // INET ADDRSTRLEN: Default ip address s i z e 457 i n e t n t o p ( AF INET, &ip addr, str, INET ADDRSTRLEN ) ; 458 p r i n t f ( New C l i e n t connected with port no. : %d and ip address : %s \n, ntohs ( c l i e n t a d d r. s i n p o r t ), s t r ) ; 459 460 461 // 462 // Forks new c l i e n t 463 464 i = f i n d A v a i l a b l e C h i l d ( i ) ; 465 466 i f ( i>= 0 && i < MAX CLIENTS) 467 { 468 r e t = f o r k ( ) ; 469 470 i f ( r e t == 0) // Create c h i l d p r o c e s s 471 { 472 respondclient ( newsocket ) ; 473 e x i t ( 0 ) ; // Child e x i t s 474 } 475 e l s e 476 { 477 p r i n t f ( \nchild %d Created with PID = %d\n \n, i, r e t ) ; 478 c l i e n t P I D [ i ] = r e t ; 479 480 } 481 } 482 e l s e 483 { 484 i = 0 ; 485 c l o s e ( newsocket ) ; 486 p r i n t f ( No more C l i e n t can connect! \ n ) ; 487 } 14

488 489 490 // And goes back to l i s t e n again f o r another c l i e n t 491 } 492 493 c l o s e ( s o c k e t I d ) ; // Close s o c k e t 494 495 return 0 ; 496 } 497 498 Listing 1: Concurrent HTTP Proxy Server PS: The code really looks better than this in sublime text editor on full screen. 15