diff --git a/include/client.h b/include/client.h new file mode 100644 index 0000000..7922ad5 --- /dev/null +++ b/include/client.h @@ -0,0 +1,16 @@ +#ifndef __CLIENT_H__ +#define __CLIENT_H__ + +#include +#include +#include +#include +#include + +#include +#include +#include + +int test_client_connection(const char *hostname, const char *port); + +#endif diff --git a/include/server.h b/include/server.h index f3caba0..a6f57ab 100644 --- a/include/server.h +++ b/include/server.h @@ -15,6 +15,6 @@ // how many pending connections the queue will hold #define BACKLOG 10 -int start_server(void); +int start_server(const char *hostname, const char *service); #endif diff --git a/include/utils.h b/include/utils.h index 7f87d4f..62d8eaf 100644 --- a/include/utils.h +++ b/include/utils.h @@ -11,7 +11,7 @@ #include #include -// Print every IPs of a hostname +// print every IPs of a hostname void print_ips(const char *hostname, const char *port); #endif diff --git a/meson.build b/meson.build index 8531fc8..8dc13b7 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,7 @@ -project('tutorial', 'c') +project('cws', 'c') subdir('src') incdir = include_directories('include') -executable('cws', sources, include_directories: incdir) +executable('server', server, include_directories: incdir) +executable('client', client, include_directories: incdir) \ No newline at end of file diff --git a/notes/basic-concepts.md b/notes/basic-concepts.md index 488f40a..9b95591 100644 --- a/notes/basic-concepts.md +++ b/notes/basic-concepts.md @@ -12,6 +12,12 @@ Before reading, this document could contain errors, please check everything you - [struct sockaddr\_storage](#struct-sockaddr_storage) - [IP Addresses](#ip-addresses) - [getaddrinfo()](#getaddrinfo) + - [socket()](#socket-1) + - [bind()](#bind) + - [connect()](#connect) + - [listen()](#listen) + - [accept()](#accept) + - [send() and recv()](#send-and-recv) ### Socket @@ -115,4 +121,44 @@ int getaddrinfo(const char *node, // e.g. "www.example.com" or IP ``` The `node` could be a host name or IP address. `service` could be a port number or a service found in `/etc/services` (e.g. "http" or "ftp" or "telnet"). -`hints` points to a struct you already filled and `res` contains a linked list of results. \ No newline at end of file +`hints` points to a struct you already filled and `res` contains a linked list of results. + +### socket() +```c +int socket(int domain, int type, int protocol); +``` + +`domain` could be `PF_INET` or `PF_INET6`, `type` instead TCP or UDP and `protocol` to 0. `PF_INET` is very close to `AF_INET`! However, we can avoid to put the stuff manually and use the results from `getaddrinfo()`. + +### bind() +Do this if you're going to listen on a port. The port is used by the kernel to match an incoming packet to a socket descriptor. + +```c +int bind(int sockfd, struct sockaddr *my_addr, int addrlen); +``` + +### connect() +```c +int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); +``` + +### listen() +```c +int listen(int sockfd, int backlog); +``` + +`backlog` is the amount of max clients allowed on the incoming queue. A client will wait in the queue until you accept it. + +### accept() +```c +int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); +``` + +After you accept a client, the function will return a new socket file descriptor used to communicate. + +### send() and recv() +```c +int send(int sockfd, const void *msg, int len, int flags); +``` + +Just put `flags` to 0. It will return the bytes sent. diff --git a/src/client.c b/src/client.c new file mode 100644 index 0000000..858aa0c --- /dev/null +++ b/src/client.c @@ -0,0 +1,38 @@ +// THIS FILE IS ONLY A TEST FOR THE BASIC STUFF + +#include "client.h" +#include "colors.h" + +int test_client_connection(const char *hostname, const char *port) { + struct addrinfo hints; + struct addrinfo *res; + + memset(&hints, 0, sizeof hints); + hints.ai_socktype = SOCK_STREAM; + hints.ai_family = AF_INET; + + int status = getaddrinfo(hostname, port, &hints, &res); + if (status != 0) { + fprintf(stderr, RED BOLD "[client] getaddrinfo(): %s\n" RESET, + gai_strerror(status)); + exit(EXIT_FAILURE); + } + + int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + + status = connect(sockfd, res->ai_addr, res->ai_addrlen); + if (status != 0) { + fprintf(stderr, RED BOLD "[client] connect(): %s\n" RESET, + gai_strerror(status)); + exit(EXIT_FAILURE); + } + + char buf[4096]; + int bytes_read = recv(sockfd, buf, 4096, 0); + fprintf(stdout, BLUE "[client] Read %d bytes: %s\n" RESET, bytes_read, + buf); + + freeaddrinfo(res); + + return 0; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 8ec0676..bb8d6e2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,17 +1,14 @@ #include "main.h" #include "colors.h" #include "server.h" -#include "utils.h" int main(int argc, char **argv) { - fprintf(stdout, BOLD GREEN "Running cws...\n" RESET); + fprintf(stdout, BOLD GREEN "[server] Running cws...\n" RESET); - int ret = start_server(); + int ret = start_server(NULL, "3030"); if (ret < 0) { fprintf(stderr, BOLD RED "Unable to start web server\n"); } - print_ips("google.com", "80"); - return 0; } diff --git a/src/mainc.c b/src/mainc.c new file mode 100644 index 0000000..bc36e94 --- /dev/null +++ b/src/mainc.c @@ -0,0 +1,14 @@ +#include "client.h" +#include "colors.h" +#include "main.h" + +int main(int argc, char **argv) { + fprintf(stdout, BOLD GREEN "[client] Running cws...\n" RESET); + + int ret = test_client_connection("localhost", "3030"); + if (ret < 0) { + fprintf(stderr, BOLD RED "Unable to start client\n"); + } + + return 0; +} diff --git a/src/meson.build b/src/meson.build index 77cabbc..64cd73b 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1 +1,2 @@ -sources = files('main.c', 'server.c', 'utils.c') \ No newline at end of file +server = files('main.c', 'server.c', 'utils.c') +client = files('mainc.c', 'client.c') \ No newline at end of file diff --git a/src/server.c b/src/server.c index 6291f33..182aaac 100644 --- a/src/server.c +++ b/src/server.c @@ -1,34 +1,49 @@ #include "server.h" #include "colors.h" -int start_server(void) { - char ipv4[INET_ADDRSTRLEN]; - struct sockaddr_in sa; - - inet_pton(AF_INET, "192.168.0.1", &(sa.sin_addr)); - inet_ntop(AF_INET, &(sa.sin_addr), ipv4, INET_ADDRSTRLEN); - - fprintf(stdout, BLUE "IPv4: %s\n" RESET, ipv4); - - return 0; -} - -void get_local_ip(void) { - char service[] = "3030"; +int start_server(const char *hostname, const char *service) { struct addrinfo hints; struct addrinfo *res; + struct sockaddr_storage their_sa; // incoming clients + socklen_t theirsa_size = sizeof their_sa; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 hints.ai_socktype = SOCK_STREAM; // TCP hints.ai_flags = AI_PASSIVE; // fill in IP for me - int status = getaddrinfo(NULL, service, &hints, &res); + int status = getaddrinfo(hostname, service, &hints, &res); if (status != 0) { - fprintf(stderr, RED "getaddrinfo() error: %s\n" RESET, + fprintf(stderr, + RED BOLD "[server] getaddrinfo() error: %s\n" RESET, gai_strerror(status)); - exit(1); + exit(EXIT_FAILURE); } + // socket file descriptor + int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + + status = bind(sockfd, res->ai_addr, res->ai_addrlen); + if (status != 0) { + fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET, + gai_strerror(status)); + exit(EXIT_FAILURE); + } + + status = listen(sockfd, BACKLOG); + if (status != 0) { + fprintf(stderr, RED BOLD "[server] listen(): %s\n" RESET, + gai_strerror(status)); + exit(EXIT_FAILURE); + } + + char *msg = "Hello there!"; + int msg_len = strlen(msg); + int newfd = accept(sockfd, (struct sockaddr *)&their_sa, &theirsa_size); + int bytes_sent = send(newfd, msg, msg_len, 0); + fprintf(stdout, BLUE "[server] Sent %d bytes\n" RESET, bytes_sent); + freeaddrinfo(res); + + return 0; }