Receive connections
This commit is contained in:
parent
6bd93b93b0
commit
34e77a7c39
2 changed files with 85 additions and 57 deletions
|
@ -1,47 +1,5 @@
|
|||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
int open_socket(const char *ip, const char *port, int *res_sd) {
|
||||
// get address info
|
||||
|
||||
struct addrinfo addr_spec;
|
||||
struct addrinfo *addrinfo_res;
|
||||
|
||||
memset(&addr_spec, 0, sizeof(addr_spec));
|
||||
addr_spec.ai_family = AF_UNSPEC;
|
||||
addr_spec.ai_socktype = SOCK_STREAM;
|
||||
|
||||
int addrinfo_status = getaddrinfo(ip, port, &addr_spec, &addrinfo_res);
|
||||
if (addrinfo_status != 0) {
|
||||
fprintf(stderr, "get address info: %s\n", gai_strerror(addrinfo_status));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sd = socket(addrinfo_res->ai_family, addrinfo_res->ai_socktype, addrinfo_res->ai_protocol);
|
||||
|
||||
if (sd == -1) {
|
||||
perror("failed to open socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (bind(sd, addrinfo_res->ai_addr, addrinfo_res->ai_addrlen) == -1) {
|
||||
perror("failed to bind to port");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listen(sd, 5)) {
|
||||
perror("failed to start listening for connections");
|
||||
return 1;
|
||||
}
|
||||
|
||||
*res_sd = sd;
|
||||
freeaddrinfo(addrinfo_res);
|
||||
return 0;
|
||||
}
|
||||
#include "sockets.c"
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
|
@ -55,22 +13,9 @@ int main (int argc, char **argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
int new_sd = accept(sd, NULL, NULL);
|
||||
if (new_sd == -1) {
|
||||
perror("failed to accept new connection");
|
||||
if (accept_loop(sd) != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *dat = (char *) malloc(sizeof(char) * 128);
|
||||
|
||||
int num_bytes = recv(new_sd, dat, 128, 0);
|
||||
do {
|
||||
if (num_bytes == -1) {
|
||||
perror("failed to receive from remote host");
|
||||
return 1;
|
||||
}
|
||||
printf("%s", dat);
|
||||
} while ((num_bytes = recv(new_sd, dat, 128, 0)) != 0);
|
||||
|
||||
return 0;
|
||||
}
|
83
firewall-server/sockets.c
Normal file
83
firewall-server/sockets.c
Normal file
|
@ -0,0 +1,83 @@
|
|||
//
|
||||
// Created by akp on 02/11/23.
|
||||
//
|
||||
|
||||
#include <pthread.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int open_socket(const char *ip, const char *port, int *res_sd) {
|
||||
// get address info
|
||||
|
||||
struct addrinfo addr_spec;
|
||||
struct addrinfo *addrinfo_res;
|
||||
|
||||
memset(&addr_spec, 0, sizeof(addr_spec));
|
||||
addr_spec.ai_family = AF_UNSPEC;
|
||||
addr_spec.ai_socktype = SOCK_STREAM;
|
||||
|
||||
int addrinfo_status = getaddrinfo(ip, port, &addr_spec, &addrinfo_res);
|
||||
if (addrinfo_status != 0) {
|
||||
fprintf(stderr, "get address info: %s\n", gai_strerror(addrinfo_status));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sd = socket(addrinfo_res->ai_family, addrinfo_res->ai_socktype, addrinfo_res->ai_protocol);
|
||||
|
||||
if (sd == -1) {
|
||||
perror("failed to open socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (bind(sd, addrinfo_res->ai_addr, addrinfo_res->ai_addrlen) == -1) {
|
||||
perror("failed to bind to port");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listen(sd, 5)) {
|
||||
perror("failed to start listening for connections");
|
||||
return 1;
|
||||
}
|
||||
|
||||
*res_sd = sd;
|
||||
freeaddrinfo(addrinfo_res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_connection(int *sdptr) {
|
||||
int sd = *sdptr;
|
||||
fprintf(stderr, "TODO: handle connection %d\n", sd);
|
||||
|
||||
if (shutdown(sd, 2) != 0) {
|
||||
perror("warning: shutdown connection in thread failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (close(sd) != 0) {
|
||||
perror("warning: shutdown connection in thread failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int accept_loop(int sd) {
|
||||
pthread_t thread_id;
|
||||
|
||||
while (1) {
|
||||
int new_sd = accept(sd, NULL, NULL);
|
||||
if (new_sd == -1) {
|
||||
perror("failed to accept new connection");
|
||||
return 1;
|
||||
}
|
||||
|
||||
pthread_create(
|
||||
&thread_id,
|
||||
NULL,
|
||||
(void *) handle_connection,
|
||||
&new_sd
|
||||
);
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in a new issue