-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_server_with_cache.c
More file actions
95 lines (82 loc) · 1.99 KB
/
proxy_server_with_cache.c
File metadata and controls
95 lines (82 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "proxy_parse.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/wait.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#define MAX_CLIENTS 10
typedef struct cache_element cache_element;
struct cache_element
{
char *data;
int len;
char *url;
time_t lru_time_track;
cache_element *next;
};
cache_element *find(char *url);
int add_cache_element(char *data, int size, char *url);
void remove_cache_element();
int port_number = 8080;
int proxy_socketId;
pthread_t tid[MAX_CLIENTS];
sem_t semaphore;
pthread_mutex_t lock;
cache_element *head;
int cache_size;
int main(int argc, char *argv[])
{
int client_socketId, client_len;
struct sockaddr_in server_addr, client_addr;
sem_init(&semaphore, 0, MAX_CLIENTS);
pthread_mutex_init(&lock, NULL);
if (argv == 2)
{
port_number = atoi(argv[1]);
}
else
{
printf("Too few arguments\n");
exit(1);
}
printf("Starting proxy server at port : %d\n", port_number);
proxy_socketId = socket(AF_INET, SOCK_STREAM, 0);
if (proxy_socketId < 0)
{
printf("Failed to create a socket\n");
exit(1);
}
int reuse = 1;
if (setsockopt(proxy_socketId, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)) < 0)
{
perror("setSockOpt failed\n");
}
bzero((char *)&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_number);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(proxy_socketId, (struct sockaddr *)&server_addr, sizeof(server_addr) < 0))
{
perror("Port is not available\n");
exit(1);
}
printf("Binding on port %d\n", port_number);
int listen_status = listen(proxy_socketId, MAX_CLIENTS);
if (listen_status < 0)
{
perror("Error in listening\n");
exit(1);
}
int i = 0;
int Connected_socketId[MAX_CLIENTS];
}