add some notes and basic code
This commit is contained in:
17
src/main.c
17
src/main.c
@@ -1,6 +1,17 @@
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
#include "colors.h"
|
||||
#include "server.h"
|
||||
#include "utils.h"
|
||||
|
||||
int main(void) {
|
||||
puts(GREEN_COLOR "Running cws..." RESET_COLOR);
|
||||
int main(int argc, char **argv) {
|
||||
fprintf(stdout, BOLD GREEN "Running cws...\n" RESET);
|
||||
|
||||
int ret = start_server();
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, BOLD RED "Unable to start web server\n");
|
||||
}
|
||||
|
||||
print_ips("google.com", "80");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
sources = files('main.c', 'server.c')
|
||||
sources = files('main.c', 'server.c', 'utils.c')
|
||||
29
src/server.c
29
src/server.c
@@ -1,5 +1,34 @@
|
||||
#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";
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *res;
|
||||
|
||||
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);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED "getaddrinfo() error: %s\n" RESET,
|
||||
gai_strerror(status));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
}
|
||||
|
||||
40
src/utils.c
Normal file
40
src/utils.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "utils.h"
|
||||
#include "colors.h"
|
||||
|
||||
void print_ips(const char *hostname, const char *port) {
|
||||
struct addrinfo ai;
|
||||
struct addrinfo *res;
|
||||
|
||||
memset(&ai, 0, sizeof ai);
|
||||
|
||||
ai.ai_family = AF_UNSPEC;
|
||||
ai.ai_socktype = SOCK_STREAM;
|
||||
|
||||
int status = getaddrinfo(hostname, port, &ai, &res);
|
||||
if (status < 0) {
|
||||
fprintf(stderr, RED "getaddrinfo(): %s\n" RESET,
|
||||
gai_strerror(status));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char ipv4[INET_ADDRSTRLEN];
|
||||
char ipv6[INET6_ADDRSTRLEN];
|
||||
|
||||
for (struct addrinfo *p = res; p != NULL; p = p->ai_next) {
|
||||
if (p->ai_family == AF_INET) {
|
||||
struct sockaddr_in *sin =
|
||||
(struct sockaddr_in *)p->ai_addr;
|
||||
inet_ntop(AF_INET, &sin->sin_addr, ipv4,
|
||||
INET_ADDRSTRLEN);
|
||||
fprintf(stdout, BLUE "%s\n" RESET, ipv4);
|
||||
} else if (p->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6 =
|
||||
(struct sockaddr_in6 *)p->ai_addr;
|
||||
inet_ntop(AF_INET6, &sin6->sin6_addr, ipv6,
|
||||
INET6_ADDRSTRLEN);
|
||||
fprintf(stdout, BLUE "%s\n" RESET, ipv6);
|
||||
}
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
}
|
||||
Reference in New Issue
Block a user