connections now create files

This commit is contained in:
Christopher Ramey 2015-02-04 16:38:40 +00:00 committed by cdramey
parent d5bbdd6c44
commit b9d2f8b44f

View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -37,19 +38,22 @@ int main(int argc, char *argv[])
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
int flag = 1; fd_set readfds, masterfds;
int sockfd = 0, connfd = 0;
int port = 10800;
uint32_t address = INADDR_ANY;
struct sockaddr_in sock;
struct sockaddr_in client; int port = 10800; // Default port
uint32_t address = INADDR_ANY; // Default listen address
int sockflag = 1, sockfd = 0, connfd = 0;
struct sockaddr_in sock, client;
socklen_t client_sz = sizeof(client); socklen_t client_sz = sizeof(client);
char timebuff[15], fnbuff[35]; char timebuff[15], fnbuff[35];
time_t timeraw; time_t timeraw;
struct tm* timeptr; struct tm* timeptr;
// Allocate space for the output fds
int writefds[FD_SETSIZE];
memset(&writefds, 0, sizeof(int) * FD_SETSIZE);
exec_name = basename(argv[0]); exec_name = basename(argv[0]);
for(int ch; (ch = getopt_long(argc, argv, "b:p:vh0", longopts, NULL)) != -1;){ for(int ch; (ch = getopt_long(argc, argv, "b:p:vh0", longopts, NULL)) != -1;){
@ -107,7 +111,7 @@ int main(int argc, char *argv[])
// This sets REUSE on the socket so it's easily reallocated if // This sets REUSE on the socket so it's easily reallocated if
// this program dies // this program dies
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockflag, sizeof(sockflag));
memset(&sock, 0, sizeof(sock)); memset(&sock, 0, sizeof(sock));
sock.sin_family = AF_INET; sock.sin_family = AF_INET;
@ -123,21 +127,34 @@ int main(int argc, char *argv[])
} }
// Listen on port // Listen on port
if(listen(sockfd, 5) != 0){ if(listen(sockfd, 10) != 0){
fprintf(stderr, "%s: cannot listen - %s\n", fprintf(stderr, "%s: cannot listen - %s\n",
exec_name, strerror(errno) exec_name, strerror(errno)
); );
goto shutdown_error; goto shutdown_error;
} }
FD_ZERO(&masterfds);
FD_SET(sockfd, &masterfds);
while(1){ while(1){
// Wait for a connection on our bound socket readfds = masterfds;
if(select(FD_SETSIZE, &readfds, NULL, NULL, NULL) == -1){
fprintf(stderr, "%s: socket select error - %s\n",
exec_name, strerror(errno)
);
goto shutdown_error;
}
for(int i = 0; i < FD_SETSIZE; i++){
if(FD_ISSET(i, &readfds)){
// Deal with new connections
if(i == sockfd){
connfd = accept(sockfd, (struct sockaddr*)&client, &client_sz); connfd = accept(sockfd, (struct sockaddr*)&client, &client_sz);
if(connfd == -1){ if(connfd == -1){
fprintf(stderr, "%s: accept failed - %s\n", fprintf(stderr, "%s: accept failed - %s\n",
exec_name, strerror(errno) exec_name, strerror(errno)
); );
goto shutdown_error;
} }
// Collect the time of the connection, process it into // Collect the time of the connection, process it into
@ -149,21 +166,40 @@ int main(int argc, char *argv[])
// Take the short date/time connection string and add the client address // Take the short date/time connection string and add the client address
// to create a filename // to create a filename
snprintf(&fnbuff[0], 34, "%s-%s.dat", timebuff, inet_ntoa(client.sin_addr)); snprintf(&fnbuff[0], 34, "%s-%s.dat",
timebuff, inet_ntoa(client.sin_addr)
);
fprintf(stdout, "filename: %s\n", fnbuff); if((writefds[i] = open(fnbuff, O_WRONLY | O_CREAT, 0644)) == -1){
writefds[i] = 0;
fprintf(stderr, "%s: cannot open - %s\n",
exec_name, strerror(errno)
);
// We're done, close the connection close(connfd);
close(connfd); connfd = 0; } else {
FD_SET(connfd, &masterfds);
}
connfd = 0;
// Read from existing connections
} else {
if(writefds[i] != 0){
close(writefds[i]);
writefds[i] = 0;
}
FD_CLR(i, &masterfds);
close(i);
}
}
}
} }
shutdown_clean: shutdown_clean:
if(connfd > 0) close(connfd);
if(sockfd > 0) close(sockfd); if(sockfd > 0) close(sockfd);
return 0; return 0;
shutdown_error: shutdown_error:
if(connfd > 0) close(connfd);
if(sockfd > 0) close(sockfd); if(sockfd > 0) close(sockfd);
return 1; return 1;
} }