Add mineziperd server
This commit is contained in:
parent
43ac716bf2
commit
e3b9f1f488
3 changed files with 82 additions and 13 deletions
|
|
@ -1,14 +1,6 @@
|
||||||
set(mineziper_STATIC_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
# mineziper(d) sources
|
||||||
set(mineziper_STATIC_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
add_executable(mineziper ${CMAKE_CURRENT_SOURCE_DIR}/mineziper.c)
|
||||||
|
|
||||||
# mineziper sources
|
|
||||||
set(mineziper_SRC
|
|
||||||
${mineziper_STATIC_SRC}/mineziper.c
|
|
||||||
)
|
|
||||||
|
|
||||||
set(mineziper_INCLUDE
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(mineziper ${mineziper_SRC} ${mineziper_INCLUDE})
|
|
||||||
target_include_directories(mineziper PUBLIC ${mineziper_STATIC_INCLUDE})
|
|
||||||
target_link_libraries(mineziper PUBLIC libmineziper)
|
target_link_libraries(mineziper PUBLIC libmineziper)
|
||||||
|
|
||||||
|
add_executable(mineziperd ${CMAKE_CURRENT_SOURCE_DIR}/mineziperd.c)
|
||||||
|
target_link_libraries(mineziperd PUBLIC libmineziper)
|
||||||
|
|
|
||||||
77
mineziper/mineziperd.c
Normal file
77
mineziper/mineziperd.c
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "libmineziper.h"
|
||||||
|
#include "libmineziper_crypto.h"
|
||||||
|
|
||||||
|
#define PORT 8989
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int server_sockfd, client_sockfd;
|
||||||
|
int server_len, client_len;
|
||||||
|
struct sockaddr_in server_address;
|
||||||
|
struct sockaddr_in client_address;
|
||||||
|
|
||||||
|
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
server_address.sin_family = AF_INET;
|
||||||
|
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
server_address.sin_port = htons(PORT);
|
||||||
|
server_len = sizeof(server_address);
|
||||||
|
bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
|
||||||
|
|
||||||
|
// Create a connection queue, ignore child exit details and wait for clients.
|
||||||
|
|
||||||
|
// Handle only one connection at a time for now
|
||||||
|
listen(server_sockfd, 1);
|
||||||
|
|
||||||
|
signal(SIGCHLD, SIG_IGN);
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
printf("mineziperd waiting for duty\n");
|
||||||
|
|
||||||
|
/* Accept connection. */
|
||||||
|
client_len = sizeof(client_address);
|
||||||
|
client_sockfd = accept(
|
||||||
|
server_sockfd, (struct sockaddr *) &client_address, &client_len);
|
||||||
|
|
||||||
|
/* Fork to create a process for this client and perform a test to see
|
||||||
|
whether we're the parent or the child. */
|
||||||
|
if (fork() == 0)
|
||||||
|
{
|
||||||
|
// Get size of buffer
|
||||||
|
unsigned int read_size;
|
||||||
|
read(client_sockfd, &read_size, sizeof(int));
|
||||||
|
|
||||||
|
char *buffer = malloc(read_size);
|
||||||
|
if (!buffer)
|
||||||
|
{
|
||||||
|
fprintf(
|
||||||
|
stderr, "[ERROR] Cannot allocate buffer of size %d\n", read_size);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int crc = xcrc32(buffer, read_size, -1);
|
||||||
|
bool scan_result = scan_zip(buffer, read_size);
|
||||||
|
|
||||||
|
write(client_sockfd, &crc, sizeof(crc));
|
||||||
|
write(client_sockfd, &scan_result, sizeof(scan_result));
|
||||||
|
|
||||||
|
close(client_sockfd);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, we must be the parent
|
||||||
|
else
|
||||||
|
{
|
||||||
|
close(client_sockfd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue