-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverC.c
More file actions
255 lines (227 loc) · 6.98 KB
/
Copy pathserverC.c
File metadata and controls
255 lines (227 loc) · 6.98 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "common.h"
typedef struct CredentialT
{
char username[MAX_USERNAME_LEN];
char password[MAX_PASSWORD_LEN];
} Credential;
Credential credential[MAX_FILE_LINES] = {0};
int totalLines = 0;
// read and parse credentials file.
void readFile()
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t bytes;
fp = fopen(CRED_FILE, "r");
if (fp == NULL)
{
printf("serverC : Couldn't open file : %s", CRED_FILE);
exit(EXIT_FAILURE);
}
int i = 0;
while ((bytes = getline(&line, &len, fp)) != -1)
{
strncpy(credential[i].username, line, MAX_USERNAME_LEN);
// Reference for usage of strcspn -
// https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input
size_t delimiterPos = strcspn(line, COMMA);
credential[i].username[delimiterPos] = 0;
strncpy(credential[i].password, line + delimiterPos + 1, MAX_PASSWORD_LEN);
credential[i].password[strcspn(credential[i].password, "\n\r")] = 0;
debug("username : %s , password : %s ", credential[i].username, credential[i].password);
i++;
}
totalLines = i;
if (line)
free(line);
fclose(fp);
}
// validate the user credentials
void validateCredentials(char *username, char *password, char *result)
{
int i = 0;
int usernameMatch = 0;
if (!strcmp("", username))
{
strcpy(result, MSG_TYPE_NOAUTH_USER);
return;
}
if(!strcmp("", password))
{
strcpy(result, MSG_TYPE_NOAUTH_PASS);
return;
}
for (i = 0; i < MAX_FILE_LINES; i++)
{
// debug(" map : %s : %s ", credential[i].username, credential[i].password);
if (!strcmp(credential[i].username, username) &&
!strcmp(credential[i].password, password))
{
strcpy(result, MSG_TYPE_AUTH);
return;
}
if (!strcmp(credential[i].username, username))
{
usernameMatch++;
}
}
strcpy(result, usernameMatch ? MSG_TYPE_NOAUTH_PASS : MSG_TYPE_NOAUTH_USER);
return;
}
// verify credentials
void verifyCredentials(int sockfd, char *buf)
{
char username[MAX_USERNAME_LEN + 1] = {0};
char password[MAX_PASSWORD_LEN + 1] = {0};
strcpy(username, buf);
size_t delimiterPos = strcspn(buf, DELIMITER);
username[delimiterPos] = 0;
strcpy(password, buf + delimiterPos + 1);
// Beej’s guide to network programming, 6.3
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
char serverMPort[10];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // set to AF_INET to use IPv4
hints.ai_socktype = SOCK_DGRAM;
sprintf(serverMPort, "%d", SERVERM_UDP_PORT);
/* convert a text string representation of an IP address to an addrinfo structure
that contains a sockaddr structure for the IP address and other information. */
if ((rv = getaddrinfo(localhost, serverMPort, &hints, &servinfo)) != 0)
{
perror("serverC: getaddrinfo");
return;
}
// loop through all the results and make a socket
for (p = servinfo; p != NULL; p = p->ai_next)
{
if (p != NULL)
{
break;
}
}
// check if the socket was bound successfully or not
if (p == NULL)
{
printf("serverC: failed to create socket\n");
return;
}
char msg[MAX_CREDENTIAL_LEN] = {0};
validateCredentials(username, password, msg);
if ((numbytes = sendto(sockfd, msg, strlen(msg), 0,
p->ai_addr, p->ai_addrlen)) == -1)
{
perror("serverC: sendto");
close(sockfd);
exit(1);
}
debug("Sent %s : %d bytes", msg, numbytes);
printf(SERVERC_CRED_SENT);
}
// receive credentials from socket
void receiveCredentials()
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
struct sockaddr_storage their_addr;
char buf[MAX_CREDENTIAL_LEN];
socklen_t addr_len;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // set to AF_INET to use IPv4
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE; // use my IP
char serverCPort[10];
sprintf(serverCPort, "%d", SERVERC_PORT);
/* convert a text string representation of an IP address to an addrinfo structure
that contains a sockaddr structure for the IP address and other information. */
if ((rv = getaddrinfo(localhost, serverCPort, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return;
}
// loop through all the results and bind to the first we can
for (p = servinfo; p != NULL; p = p->ai_next)
{
// make a socket using the information gleaned from getaddrinfo():
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("serverC: socket");
continue;
}
// bind the socket to the port we passed in to getaddrinfo():
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("serverC: Failed to bind");
continue;
}
break;
}
// check if the socket was bound successfully or not
if (p == NULL)
{
printf("serverC: failed to bind socket\n");
return;
}
freeaddrinfo(servinfo); // all done with this structure
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
int portNo = 0;
//Retrieve the locally-bound name of the specified socket and store it in the sockaddr structure
if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
{
perror("client: getsockname");
close(sockfd);
exit(EXIT_FAILURE);
}
else
{
portNo = ntohs(sin.sin_port);
debug("port number %d\n", portNo);
if (portNo != SERVERC_PORT)
{
assert(0);
}
}
printf(SERVERC_BOOT_MSG, portNo);
addr_len = sizeof their_addr;
while (1)
{
// receive data from socket
if ((numbytes = recvfrom(sockfd, buf, MAX_CREDENTIAL_LEN - 1, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
perror("serverC: recvfrom");
close(sockfd);
exit(1);
}
buf[numbytes] = '\0';
debug("serverC: message received : \"%s\"", buf);
printf(SERVERC_CRED_RECV);
char messageType[MSG_TYPE_LEN] = {0};
strncpy(messageType, buf, MSG_TYPE_LEN);
size_t delimiterPos = strcspn(buf, DELIMITER);
messageType[delimiterPos] = 0;
if (!strcmp(messageType, MSG_TYPE_CRED))
{
verifyCredentials(sockfd, buf + delimiterPos + 1);
}
else
{
debug("serverC: invalid message received : \"%s\"", messageType);
continue;
}
}
close(sockfd);
}
int main()
{
readFile();
receiveCredentials();
return 0;
}