-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
241 lines (200 loc) · 6.73 KB
/
client.c
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
237
238
239
240
241
/*
* client.c
*
* Created on: Oct 15, 2010
* Author: kaduparag
*/
#include "server.h"
//Global
char serverIP[INET_ADDRSTRLEN];
int server_port, max_win_size;
int TIMEOUT_SEC=1;
int TIMEOUT_USEC=0;
char required_file_name[FILE_NAME_LEN];
struct sockaddr_in cliaddr;
void dg_cli(int sockfd, const struct sockaddr *pservaddr, socklen_t servlen);
#if 0
//--------------------------------------------------------------------------------
void err_sys(const char * msg) {
printf("ERROR: %s\n", msg);
exit(1);
}
//--------------------------------------------------------------------------------
int isIPAddress(const char *addr) {
int status = 1, i;
int len = strlen(addr);
for (i = 0; i < len; i++) {
if (addr[i] == '.' || (addr[i] >= '0' && addr[i] <= '9')) {
continue;
} else {
status = 0;
break;
}
}
return status;
}
//------------------------------------------------------------------------------
#endif
int main(int argc, char **argv) {
int sockfd;
struct sockaddr_in servaddr;
int len,on=1;
struct interface_info *head;
struct sockaddr_in closest;
generate_ifi_list(&head);
int ret;
// if (argc != 2) //TODO Uncomment
// err_sys("usage: client <client.in file>");
//Read input configuration from server.in
char line[MAX_LINE];
FILE* fp = fopen("client.in", "r"); //TODO read from argv[1]
if (fgets(line, sizeof(line), fp)) {//Line 1 server ip
len = strlen(line) - 1; //remove the trailing \n
if (line[len] == '\n')
line[len] = '\0';
strcpy(serverIP, line);
if (!isIPAddress(serverIP)) {
err_sys_p("Invalid or missing server ip in the configuration file.");
}
printf("[INFO] Server ip:%s\n", serverIP);
} else {
err_sys_p("Invalid or missing input configuration.");
}
if (fgets(line, sizeof(line), fp)) {//Line 2 server port
server_port = atoi(line);
if (server_port == 0) {
err_sys(
"Invalid or missing server port number in the configuration file.");
}
printf("[INFO] Server port:%d\n", server_port);
} else {
err_sys_p("Invalid or missing input configuration.");
}
if (fgets(line, sizeof(line), fp)) {//Line 3 file name
len = strlen(line) - 1; //remove the trailing \n
if (line[len] == '\n')
line[len] = '\0';
strcpy(required_file_name, line);
if (len == 0)
err_sys("Invalid or missing file name in the configuration file.");
printf("[INFO] File name:%s\n", required_file_name);
} else {
err_sys_p("Invalid or missing input configuration.");
}
if (fgets(line, sizeof(line), fp)) {//Line 4 maximum sliding window size.
max_win_size = atoi(line);
if (max_win_size == 0) {
err_sys_p(
"Invalid or missing Max win size in the configuration file.");
}
printf("[INFO] Max win size:%d\n", max_win_size);
} else {
err_sys_p("Invalid or missing input configuration.");
}
fclose(fp);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(server_port);
bzero(&closest, sizeof(closest));
ret =closest_match_to_interface(head,serverIP,&closest.sin_addr);
//printf("[INFO] closest is %s\n",inet_ntoa(closest.sin_addr));
cliaddr.sin_addr.s_addr = closest.sin_addr.s_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
err_sys_p("Socket error.");
}
if(ret==1)
{
/**returned 1, we should use localhost for both server and client**/
if (inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr) != 1)
err_sys_p("Cannot convert string IP to binary IP.");
}
else if(ret==2)
{
/**then its on local n/w , use dont' route**/
if(setsockopt(sockfd, SOL_SOCKET, SO_DONTROUTE, (void *) &on, sizeof(on)) < 0)
err_sys_p("Can't set SO_DONTROUTE on main socket");
if (inet_pton(AF_INET, serverIP, &servaddr.sin_addr) != 1)
err_sys_p("Cannot convert string IP to binary IP.");
}
else
{
if (inet_pton(AF_INET, serverIP, &servaddr.sin_addr) != 1)
err_sys_p("Cannot convert string IP to binary IP.");
}
dg_cli(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
exit(0);
}
void dg_cli(int sockfd, const struct sockaddr *pservaddr, socklen_t servlen) {
int n,connection_sockfd,attempt_count,success_flag;
char sendline[MAXLINE], recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
//Bind and print client socket addr.
cliaddr.sin_addr.s_addr = htonl(cliaddr.sin_addr.s_addr);
printf("\nClient will use: %s\n",inet_ntoa(cliaddr.sin_addr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(0);
if (bind(sockfd, (struct sockaddr *) &cliaddr,sizeof(cliaddr)))
err_sys_p("Coudlnt bind socket.");
printf("[INFO] Bounded to %s\n", (char*)Sock_ntop((struct sockaddr * ) &cliaddr,sizeof(cliaddr)));
//Connect to well known port of the server
if (connect(sockfd, (struct sockaddr *) pservaddr, servlen) < 0) {
err_sys_p("Connect error");
}
//Send the filename as datagram
int len = strlen(required_file_name);
if (write(sockfd, required_file_name, len) != len) {
err_sys_p("Write error.Server is unreachable");
}
success_flag=0;
for(attempt_count=0;attempt_count<MAX_ATTEMPT;attempt_count++){
if(readable_timeout(sockfd,TIMEOUT_SEC,TIMEOUT_USEC)==0){
printf("Socket timeout...attempt %d failed.\n",attempt_count);
if (write(sockfd, required_file_name, len) != len) { //Try again
err_sys_p("Write error.Server is unreachable");
}
}else{
success_flag=1;
break;
}
}
if(success_flag==0){//TODO what to do if MAX_ATTEMT fail? report and end prgm?
err_sys_p("Socket timeout...max no. of attempt failed.\n");
}
//Read the new connection socket ephemeral port number. This serves as ack for filename too.
if ((n = read(sockfd, recvline, MAXLINE)) == -1)
err_sys_p("Read error. Server is unreachable");
recvline[n] = 0; // null terminate
fputs(recvline, stdout);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(recvline));
if (inet_pton(AF_INET, serverIP, &servaddr.sin_addr) != 1)
err_sys_p("Cannot convert string IP to binary IP.");
connection_sockfd = sockfd;
if (connect(connection_sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
err_sys_p("Connect error");
}
//Send connected ack
strcpy(sendline, "ACK_CONNECTED");
if (write(connection_sockfd, sendline, strlen(sendline)) != strlen(sendline)) {
err_sys_p("Write error.Server is unreachable");
}
printf("%s\n",sendline);
// fflush(NULL);
//Read the file and print output.
while (1) {
if ((n = read(connection_sockfd, recvline, MAXLINE)) == -1)
err_sys_p("Read error. Server is unreachable");
else if (n == 0)
break;
recvline[n] = 0; // null terminate
fputs(recvline, stdout);
//send ACK
/*int len = strlen(sendline);
if (write(connection_sockfd, sendline, len) != len) {
err_sys("Write error.Server is unreachable");
}*/
}
printf("[INFO] File transfer completed.\n");
}