-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
236 lines (173 loc) · 5.32 KB
/
Copy pathclient.c
File metadata and controls
236 lines (173 loc) · 5.32 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<sys/wait.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/utsname.h>
#define MAXDATASIZE 1025
#define GET 0
#define ADD 1
#define LOOKUP 2
#define LISTALL 3
#define UPLOADPORT 8000
struct utsname sysname;
char hostname[512];
char* reqHeader(int sockfd,int option,int rfcid,char* title){
char* temp=NULL;
char fulltext[2048]="";
if(option == ADD){
temp = "ADD RFC ";
char rfcid_string[32];
sprintf(rfcid_string,"%d",rfcid);
char port_string[32];
sprintf(port_string,"%d",UPLOADPORT);
strcat(fulltext,temp);
strcat(fulltext,rfcid_string);
strcat(fulltext," P2P-CI/1.0\nHost: ");
strcat(fulltext,hostname);
strcat(fulltext,"\nPort: ");
strcat(fulltext,port_string);
strcat(fulltext,"\nTitle: ");
strcat(fulltext,title);
strcat(fulltext,"\0");
printf("Sending %s\n",fulltext);
}else if(option == LOOKUP){
temp = "LOOKUP RFC ";
char rfcid_string[32];
sprintf(rfcid_string,"%d",rfcid);
char port_string[32];
sprintf(port_string,"%d",UPLOADPORT);
strcat(fulltext,temp);
strcat(fulltext,rfcid_string);
strcat(fulltext," P2P-CI/1.0\nHost: ");
strcat(fulltext,hostname);
strcat(fulltext,"\nPort: ");
strcat(fulltext,port_string);
strcat(fulltext,"\0");
printf("Sending %s\n",fulltext);
}else if(option == LISTALL){
temp = "LIST ALL ";
char port_string[32];
sprintf(port_string,"%d",UPLOADPORT);
strcat(fulltext,temp);
strcat(fulltext," P2P-CI/1.0\nHost: ");
strcat(fulltext,hostname);
strcat(fulltext,"\nPort: ");
strcat(fulltext,port_string);
strcat(fulltext,"\0");
printf("Sending %s\n",fulltext);
}else if(option == GET){
temp = "GET RFC ";
char rfcid_string[32];
sprintf(rfcid_string,"%d",rfcid);
strcat(fulltext,temp);
strcat(fulltext,rfcid_string);
strcat(fulltext," P2P-CI/1.0\nHost: ");
strcat(fulltext,hostname);
strcat(fulltext,"\nOS: ");
strcat(fulltext,sysname.sysname);
strcat(fulltext," ");
strcat(fulltext,sysname.release);
}
sleep(1);
if(send(sockfd,fulltext,strlen(fulltext),0) == -1)
perror("send");
return fulltext;
}
//This method is used to initialize the client by sending rfc details to the server
int initClient(int sockfd){
printf("Initializing client\n");
FILE *fp;
int id,numbytes;
char title[256];
char* buf;
fp = fopen("index.txt","r");
if(fp == NULL){
perror("Rfc index not found");
exit(-1);
}
while(fscanf(fp,"%d %[^\t\n]",&id,title) == 2){
//Send add requests to the server
reqHeader(sockfd,ADD,id,title);
buf=(char *)malloc(MAXDATASIZE);
numbytes = recv(sockfd,buf,MAXDATASIZE-1,0);
buf[numbytes] = '\0';
printf("Response from server : %s\n\n",buf);
}
fclose(fp);
return 0;
}
void *get_in_addr(struct sockaddr *sa){
if(sa->sa_family == AF_INET){
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int sendGet(int sockfd,int rfcid){
printf("Get request for : %d\n",rfcid);
reqHeader(sockfd,GET,rfcid,NULL);
}
int sendLookup(int sockfd, int rfcid){
char* buf;
int numbytes;
reqHeader(sockfd,LOOKUP,rfcid,NULL);
buf=(char *)malloc(MAXDATASIZE);
numbytes = recv(sockfd,buf,MAXDATASIZE-1,0);
buf[numbytes] = '\0';
printf("Response from server : %s\n\n",buf);
}
int sendList(int sockfd){
char* buf;
int numbytes;
reqHeader(sockfd,LISTALL,NULL,NULL);
buf=(char *)malloc(MAXDATASIZE);
numbytes = recv(sockfd,buf,MAXDATASIZE-1,0);
buf[numbytes] = '\0';
printf("Response from server : %s\n",buf);
}
int main(int argc,char *argv[]){
char* PORTNO = "5540";
char* SERVERIP = "127.0.0.1";
//Populate the OS name
if(uname(&sysname)){
perror("Error obtaining os name");
exit(-1);
}
hostname[511]='\0';
gethostname(hostname,511);
char *buf;
int status,sockfd,numbytes;
struct addrinfo hints,*res,*me;
socklen_t addr_size;
char s[INET6_ADDRSTRLEN];
//Server details
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(SERVERIP,PORTNO,&hints,&res);
//Create a socket
if((sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1){
perror("Server:socket creating error");
}
if(connect(sockfd,res->ai_addr,res->ai_addrlen) == -1){
close(sockfd);
perror("Client : Connect");
exit(1);
}
inet_ntop(res->ai_family,get_in_addr((struct sockaddr *)res->ai_addr),s,sizeof(s));
gethostname(hostname,1023);
printf("Hostname : %s\n",hostname);
printf("Client connecting to : %s\n",s);
initClient(sockfd);
sendLookup(sockfd,789);
sendList(sockfd);
close(sockfd);
return 0;
}