|
|
@ -6,6 +6,7 @@ |
|
|
|
#include <stdlib.h> |
|
|
|
#include <limits.h> |
|
|
|
#include <fcntl.h> |
|
|
|
#include <sys/select.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <sys/socket.h> |
|
|
|
#include <netinet/in.h> |
|
|
@ -40,23 +41,8 @@ int main(int argc, char *argv[]) |
|
|
|
{ NULL, 0, NULL, 0 } |
|
|
|
}; |
|
|
|
|
|
|
|
fd_set readfds, masterfds; |
|
|
|
|
|
|
|
int port = 10800; // Default port |
|
|
|
uint32_t address = INADDR_ANY; // Default listen address |
|
|
|
int connflag, sockflag = 1; |
|
|
|
int sockfd = 0, connfd = 0; |
|
|
|
struct sockaddr_in sock, client; |
|
|
|
socklen_t client_sz = sizeof(client); |
|
|
|
char connbuff[BUFFERSIZE], timebuff[15], fnbuff[35]; |
|
|
|
ssize_t connbuff_sz; |
|
|
|
|
|
|
|
time_t timeraw; |
|
|
|
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]); |
|
|
|
|
|
|
@ -105,7 +91,7 @@ int main(int argc, char *argv[]) |
|
|
|
} |
|
|
|
|
|
|
|
// Establish the socket |
|
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0); |
|
|
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0); |
|
|
|
if(sockfd == -1){ |
|
|
|
fprintf(stderr, "%s: cannot open socket - %s\n", |
|
|
|
exec_name, strerror(errno) |
|
|
@ -115,8 +101,10 @@ int main(int argc, char *argv[]) |
|
|
|
|
|
|
|
// This sets REUSE on the socket so it's easily reallocated if |
|
|
|
// this program dies |
|
|
|
int sockflag = 1; |
|
|
|
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockflag, sizeof(sockflag)); |
|
|
|
|
|
|
|
struct sockaddr_in sock; |
|
|
|
memset(&sock, 0, sizeof(sock)); |
|
|
|
sock.sin_family = AF_INET; |
|
|
|
sock.sin_addr.s_addr = address; |
|
|
@ -138,6 +126,12 @@ int main(int argc, char *argv[]) |
|
|
|
goto shutdown_error; |
|
|
|
} |
|
|
|
|
|
|
|
// Allocate space for the output fds |
|
|
|
int writefds[FD_SETSIZE]; |
|
|
|
memset(&writefds, 0, sizeof(int) * FD_SETSIZE); |
|
|
|
|
|
|
|
// Setup select variables |
|
|
|
fd_set readfds, masterfds; |
|
|
|
FD_ZERO(&masterfds); |
|
|
|
FD_SET(sockfd, &masterfds); |
|
|
|
|
|
|
@ -154,7 +148,10 @@ int main(int argc, char *argv[]) |
|
|
|
if(FD_ISSET(i, &readfds)){ |
|
|
|
// Deal with new connections |
|
|
|
if(i == sockfd){ |
|
|
|
connfd = accept(sockfd, (struct sockaddr*)&client, &client_sz); |
|
|
|
struct sockaddr_in client; |
|
|
|
socklen_t client_sz = sizeof(client); |
|
|
|
|
|
|
|
int connfd = accept(sockfd, (struct sockaddr*)&client, &client_sz); |
|
|
|
if(connfd == -1){ |
|
|
|
fprintf(stderr, "%s: accept failed - %s\n", |
|
|
|
exec_name, strerror(errno) |
|
|
@ -163,6 +160,7 @@ int main(int argc, char *argv[]) |
|
|
|
} |
|
|
|
|
|
|
|
// Get current flags before setting nonblock |
|
|
|
int connflag; |
|
|
|
if((connflag = fcntl(connfd, F_GETFL)) == -1){ |
|
|
|
fprintf(stderr, "%s: cannot get connection flags - %s\n", |
|
|
|
exec_name, strerror(errno) |
|
|
@ -182,13 +180,14 @@ int main(int argc, char *argv[]) |
|
|
|
|
|
|
|
// Collect the time of the connection, process it into |
|
|
|
// a short string |
|
|
|
time(&timeraw); |
|
|
|
timeptr = localtime(&timeraw); |
|
|
|
time_t timeraw = time(NULL); |
|
|
|
struct tm* timeptr = localtime(&timeraw); |
|
|
|
char timebuff[15]; |
|
|
|
strftime(&timebuff[0], 15, "%Y%m%d%H%M%S", timeptr); |
|
|
|
timeptr = NULL; |
|
|
|
|
|
|
|
// Take the short date/time connection string and add the client address |
|
|
|
// to create a filename |
|
|
|
char fnbuff[35]; |
|
|
|
snprintf(&fnbuff[0], 34, "%s-%s.dat", |
|
|
|
timebuff, inet_ntoa(client.sin_addr) |
|
|
|
); |
|
|
@ -208,7 +207,8 @@ int main(int argc, char *argv[]) |
|
|
|
// Read from existing connections |
|
|
|
} else { |
|
|
|
// Do the read |
|
|
|
connbuff_sz = recv(i, connbuff, BUFFERSIZE, 0); |
|
|
|
char connbuff[BUFFERSIZE]; |
|
|
|
ssize_t connbuff_sz = recv(i, connbuff, BUFFERSIZE, 0); |
|
|
|
if(connbuff_sz > 0){ |
|
|
|
// If the read fails, throw an error, and set the |
|
|
|
// buffer size to 0, which forces a disconnect. |
|
|
|