-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbserver (1).c
More file actions
328 lines (268 loc) · 8.97 KB
/
dbserver (1).c
File metadata and controls
328 lines (268 loc) · 8.97 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include "msg.h"
void Usage(char *progname);
void PrintOut(int fd, struct sockaddr *addr, size_t addrlen);
void PrintReverseDNS(struct sockaddr *addr, size_t addrlen);
void PrintServerSide(int client_fd, int sock_family);
int Listen(char *portnum, int *sock_family);
void HandleClient(int c_fd, struct sockaddr *addr, size_t addrlen,
int sock_family);
void* ChildHandler(void* arg);
char* Put(char* name, uint32_t id);
char* Get(uint32_t id);
struct arg_struct{
int clientFd;
struct sockaddr_storage addr;
socklen_t addrLen;
int sockFam;
};
int
main(int argc, char **argv)
{
// Expect the port number as a command line argument.
if (argc != 2) {
Usage(argv[0]);
}
int sock_family;
int listen_fd = Listen(argv[1], &sock_family);
if (listen_fd <= 0) {
// We failed to bind/listen to a socket. Quit with failure.
printf("Couldn't bind to any addresses.\n");
return EXIT_FAILURE;
}
// Loop forever, accepting a connection from a client and doing
// an echo trick to it.
while (1) {
struct sockaddr_storage caddr;
socklen_t caddr_len = sizeof(caddr);
int client_fd = accept(listen_fd,
(struct sockaddr *)(&caddr),
&caddr_len);
if (client_fd < 0) {
if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK))
continue;
printf("Failure on accept:%d \n ", strerror(errno));
break;
}
struct arg_struct *arg = (struct arg_struct*) malloc(sizeof(struct arg_struct));
arg -> clientFd = client_fd;
arg -> addr = caddr;
arg -> addrLen = caddr_len;
arg -> sockFam = sock_family;
pthread_t p1;
pthread_create(&p1,NULL,ChildHandler, arg);
}
// Close socket
close(listen_fd);
return EXIT_SUCCESS;
}
void Usage(char *progname) {
printf("usage: %s port \n", progname);
exit(EXIT_FAILURE);
}
int
Listen(char *portnum, int *sock_family) {
// Populate the "hints" addrinfo structure for getaddrinfo().
// ("man addrinfo")
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; // IPv6 (also handles IPv4 clients)
hints.ai_socktype = SOCK_STREAM; // stream
hints.ai_flags = AI_PASSIVE; // use wildcard "in6addr_any" address
hints.ai_flags |= AI_V4MAPPED; // use v4-mapped v6 if no v6 found
hints.ai_protocol = IPPROTO_TCP; // tcp protocol
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
// getaddrinfo() returns a list of
// address structures via the output parameter "result".
struct addrinfo *result;
int res = getaddrinfo(NULL, portnum, &hints, &result);
// Did addrinfo() fail?
if (res != 0) {
printf( "getaddrinfo failed: %s", gai_strerror(res));
return -1;
}
// Loop through the returned address structures until we are able
// to create a socket and bind to one. The address structures are
// linked in a list through the "ai_next" field of result.
int listen_fd = -1;
struct addrinfo *rp;
for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_fd = socket(rp->ai_family,
rp->ai_socktype,
rp->ai_protocol);
if (listen_fd == -1) {
// Creating this socket failed. So, loop to the next returned
// result and try again.
printf("socket() failed:%d \n ", strerror(errno));
listen_fd = -1;
continue;
}
// Configure the socket; we're setting a socket "option." In
// particular, we set "SO_REUSEADDR", which tells the TCP stack
// so make the port we bind to available again as soon as we
// exit, rather than waiting for a few tens of seconds to recycle it.
int optval = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(optval));
// Try binding the socket to the address and port number returned
// by getaddrinfo().
if (bind(listen_fd, rp->ai_addr, rp->ai_addrlen) == 0) {
// Bind worked! Print out the information about what
// we bound to.
//PrintOut(listen_fd, rp->ai_addr, rp->ai_addrlen);
// Return to the caller the address family.
*sock_family = rp->ai_family;
break;
}
// The bind failed. Close the socket, then loop back around and
// try the next address/port returned by getaddrinfo().
close(listen_fd);
listen_fd = -1;
}
// Free the structure returned by getaddrinfo().
freeaddrinfo(result);
// If we failed to bind, return failure.
if (listen_fd == -1)
return listen_fd;
// Success. Tell the OS that we want this to be a listening socket.
if (listen(listen_fd, SOMAXCONN) != 0) {
printf("Failed to mark socket as listening:%d \n ", strerror(errno));
close(listen_fd);
return -1;
}
// Return to the client the listening file descriptor.
return listen_fd;
}
void
HandleClient(int c_fd, struct sockaddr *addr, size_t addrlen,
int sock_family)
{
// Print out information about the client.
printf("\nNew client connection \n" );
PrintOut(c_fd, addr, addrlen);
PrintReverseDNS(addr, addrlen);
PrintServerSide(c_fd, sock_family);
// Loop, reading data and echo'ing it back, until the client
// closes the connection.
while (1) {
char clientbuf[1024];
ssize_t res = read(c_fd, clientbuf, 1023);
if (res == 0) {
printf("[The client disconnected.] \n");
break;
}
if (res == -1) {
if ((errno == EAGAIN) || (errno == EINTR))
continue;
printf(" Error on client socket:%d \n ", strerror(errno));
break;
}
clientbuf[res] = '\0';
}
close(c_fd);
}
void
PrintOut(int fd, struct sockaddr *addr, size_t addrlen)
{
printf("Socket [%d] is bound to: \n", fd);
if (addr->sa_family == AF_INET) {
// Print out the IPV4 address and port
char astring[INET_ADDRSTRLEN];
struct sockaddr_in *in4 = (struct sockaddr_in *)(addr);
inet_ntop(AF_INET, &(in4->sin_addr), astring, INET_ADDRSTRLEN);
printf(" IPv4 address %s", astring);
printf(" and port %d\n", ntohs(in4->sin_port));
} else if (addr->sa_family == AF_INET6) {
// Print out the IPV6 address and port
char astring[INET6_ADDRSTRLEN];
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)(addr);
inet_ntop(AF_INET6, &(in6->sin6_addr), astring, INET6_ADDRSTRLEN);
printf("IPv6 address %s", astring);
printf(" and port %d\n", ntohs(in6->sin6_port));
} else {
printf(" ???? address and port ???? \n");
}
}
void
PrintReverseDNS(struct sockaddr *addr, size_t addrlen) {
char hostname[1024]; // ought to be big enough.
if (getnameinfo(addr, addrlen, hostname, 1024, NULL, 0, 0) != 0) {
sprintf(hostname, "[reverse DNS failed]");
}
printf("DNS name: %s \n", hostname);
}
void
PrintServerSide(int client_fd, int sock_family) {
char hname[1024];
hname[0] = '\0';
printf("Server side interface is ");
if (sock_family == AF_INET) {
// The server is using an IPv4 address.
struct sockaddr_in srvr;
socklen_t srvrlen = sizeof(srvr);
char addrbuf[INET_ADDRSTRLEN];
getsockname(client_fd, (struct sockaddr *) &srvr, &srvrlen);
inet_ntop(AF_INET, &srvr.sin_addr, addrbuf, INET_ADDRSTRLEN);
printf("%s", addrbuf);
// Get the server's dns name, or return it's IP address as
// a substitute if the dns lookup fails.
getnameinfo((const struct sockaddr *) &srvr,
srvrlen, hname, 1024, NULL, 0, 0);
printf(" [%s]\n", hname);
} else {
// The server is using an IPv6 address.
struct sockaddr_in6 srvr;
socklen_t srvrlen = sizeof(srvr);
char addrbuf[INET6_ADDRSTRLEN];
getsockname(client_fd, (struct sockaddr *) &srvr, &srvrlen);
inet_ntop(AF_INET6, &srvr.sin6_addr, addrbuf, INET6_ADDRSTRLEN);
printf("%s", addrbuf);
// Get the server's dns name, or return it's IP address as
// a substitute if the dns lookup fails.
getnameinfo((const struct sockaddr *) &srvr,
srvrlen, hname, 1024, NULL, 0, 0);
printf(" [%s]\n", hname);
}
}
void* ChildHandler(void* arg){
struct arg_struct *args = (struct arg_struct*) arg;
int clientFd = args -> clientFd;
struct sockaddr_storage addr = args -> addr;
socklen_t addrLen = args -> addrLen;
int sockFam = args -> sockFam;
HandleClient(clientFd,(struct sockaddr *)(&addr),addrLen, sockFam);
exit(0);
}
char* Put (char* name, uint32_t id){
struct record rec;
strcpy(rec.name,name);
rec.id = id;
FILE* f = fopen("database.dat","a");
if(fwrite(&rec, sizeof(struct record),1,f) == 0)
return "Put Failed";
fclose(f);
return "Put Success";
}
char* Get(uint32_t id){
struct record rec;
rec.id = id;
FILE* f = fopen("database.dat", "rb");
while( (fread(&rec,sizeof(struct record),1,f) == 1) && (rec.id != id));
if(rec.id == id)
printf("%s",rec.name);
else
return "Get Failed";
return "Get Success";
}