finish network
This commit is contained in:
16
src/input.c
16
src/input.c
@ -2,6 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
@ -121,3 +122,18 @@ void del_split(char **sp) {
|
||||
void del_lines(char **lines) {
|
||||
del_split(lines);
|
||||
}
|
||||
|
||||
const char *capture_system(const char *cmd, int buf_sz) {
|
||||
if (buf_sz == 0) {
|
||||
buf_sz = DEFAULT_CAPTURE_SYSTEM_BUFSIZE;
|
||||
}
|
||||
char *buf = malloc(buf_sz);
|
||||
FILE *tmp = popen(cmd, "r");
|
||||
if (tmp == NULL) {
|
||||
fprintf(stderr, "libflint: failed to open FILE *tmp in capture_system. Errno: %d\n", errno);
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
fgets(buf, buf_sz, tmp);
|
||||
return buf;
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "lfnetwork.h"
|
||||
|
||||
@ -95,50 +96,47 @@ int serve(Server *s, int backlog_size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Server is waiting for connections on port %d...\n", s->port);
|
||||
s->handler(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handler_tcp_echo(Server *s) {
|
||||
struct sockaddr_storage client_addr;
|
||||
static void *tcp_echo_thread(void *vargp) {
|
||||
while (1) {
|
||||
socklen_t client_addr_sz = sizeof(client_addr);
|
||||
int new_fd = accept(s->fd, (struct sockaddr *)&client_addr, &client_addr_sz);
|
||||
if (new_fd == -1) {
|
||||
printf("failed to accept. Errno: %d\n", errno);
|
||||
continue;
|
||||
}
|
||||
char recv_buf[256];
|
||||
int fd = *(int *) vargp;
|
||||
|
||||
char buf[33];
|
||||
inet_ntop(client_addr.ss_family, get_in_addr((struct sockaddr *)&client_addr), buf, 32);
|
||||
printf("Received connection from %s\n", buf);
|
||||
|
||||
// Start child process
|
||||
if (!fork()) {
|
||||
close(s->fd); // Child doesn't need the initial socket
|
||||
|
||||
char recv_buf[256];
|
||||
int r = recv(new_fd, recv_buf, 256, 0);
|
||||
int r = (int)recv(fd, recv_buf, 256, 0);
|
||||
if (r < 1) {
|
||||
if (r == -1) {
|
||||
fprintf(stderr, "Failed to recv. Errno: %d\n", errno);
|
||||
goto CHILD_END;
|
||||
} else if (r == 0) {
|
||||
fprintf(stderr, "Client closed connection\n");
|
||||
goto CHILD_END;
|
||||
}
|
||||
close(fd);
|
||||
break;
|
||||
}
|
||||
|
||||
if (send(new_fd, recv_buf, strlen(recv_buf), 0) == -1) {
|
||||
fprintf(stderr, "Failed to send echo\n");
|
||||
}
|
||||
|
||||
CHILD_END:
|
||||
close(new_fd);
|
||||
_exit(0);
|
||||
}
|
||||
// End child process
|
||||
|
||||
close(new_fd); // new_fd is not used by the parent
|
||||
if (send(fd, recv_buf, strlen(recv_buf), 0) == -1) {
|
||||
fprintf(stderr, "Failed to send echo. Errno: %d\n", errno);
|
||||
close(fd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handler_tcp_echo(Server *s) {
|
||||
while (1) {
|
||||
struct sockaddr_storage client_addr;
|
||||
socklen_t client_addr_sz = sizeof(client_addr);
|
||||
int new_fd = accept(s->fd, (struct sockaddr *)&client_addr, &client_addr_sz);
|
||||
if (new_fd == -1) {
|
||||
fprintf(stderr, "failed to accept. Errno: %d\n", errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
pthread_t srv_tid;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
pthread_create(&srv_tid, &attr, tcp_echo_thread, &new_fd);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user