Initial commit: created project structure, makefile, some static files for future server tests and interphase to create a listening socket

This commit is contained in:
Dmitriy Shishkov 2020-06-22 15:59:38 +05:00
commit 2b86380ff4
13 changed files with 245 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
bin/
obj/
tmp/
.vscode/

32
Makefile Normal file
View File

@ -0,0 +1,32 @@
CPPFLAGS := -Iinclude -MMD -MP
CFLAGS := -Wall
LDFLAGS := -Llib
LDLIBS := -lm
SRC_DIR := src
OBJ_DIR := obj
BIN_DIR := bin
EXE := $(BIN_DIR)/server
SRC := $(wildcard $(SRC_DIR)/*.c)
OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
all: $(EXE)
$(EXE): $(OBJ) | $(BIN_DIR)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
$(BIN_DIR) $(OBJ_DIR):
mkdir -p $@
clean:
@$(RM) -rv $(BIN_DIR) $(OBJ_DIR)
-include $(OBJ:.o=.d)
.PHONY: all, clean

2
include/netw.h Normal file
View File

@ -0,0 +1,2 @@
int get_listener_socket(char *port);
void *get_in_addr(struct sockaddr *sa);

1
include/utils.h Normal file
View File

@ -0,0 +1 @@
void err_msg(char *msg);

1
readme.txt Normal file
View File

@ -0,0 +1 @@
Webserver in c. !Experimental!

84
src/netw.c Normal file
View File

@ -0,0 +1,84 @@
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include "../include/utils.h"
#define BACKLOG 10
/**
* @brief Get the listener socket object
*
* @param {char *} port
* @return int
*/
int get_listener_socket(char *port)
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int yes = 1;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(NULL, port, &hints, &servinfo) != 0)
{
err_msg("can't get addres info");
return -1;
}
for (p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
{
err_msg("can't create socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0)
{
err_msg("can't set socket options");
close(sockfd);
freeaddrinfo(servinfo);
return -2;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) < 0)
{
close(sockfd);
err_msg("can't bind socket");
continue;
}
break;
}
freeaddrinfo(servinfo);
if (p == NULL)
{
err_msg("failed to find local adress");
return -3;
}
if (listen(sockfd, BACKLOG) < 0)
{
err_msg("failed to listen");
return -4;
}
return sockfd;
}
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

70
src/server.c Normal file
View File

@ -0,0 +1,70 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include "../include/netw.h"
#include "../include/utils.h"
/**
* Main
*/
int main(int argc, char *argv[])
{
int client_fd;
struct sockaddr_storage cli_addr;
char s[INET6_ADDRSTRLEN];
if (argc != 2)
{
err_msg("Usage: <Server Port>\n");
exit(1);
}
char *port = argv[1];
int listenfd = get_listener_socket(port);
if (listenfd < 0)
{
err_msg("!fatal! couldn't create listening socket");
exit(1);
}
printf("Waiting for connection on port %s...\n", port);
while (1)
{
socklen_t sin_size = sizeof cli_addr;
client_fd = accept(listenfd, (struct sockaddr *)&cli_addr, &sin_size);
if (client_fd < 0)
{
err_msg("couldn't accept client connection");
continue;
}
// | Doesn't work properly yet
// v
int pid = fork();
if (pid < 0)
err_msg("fork failed");
else if (pid == 0)
{
close(listenfd);
inet_ntop(cli_addr.ss_family, get_in_addr((struct sockaddr *)&cli_addr), s, sizeof s);
printf("Got connection from %s\n", s);
// connection(client_fd); // Implement
exit(0);
}
else
close(client_fd);
}
return 0;
}

10
src/utils.c Normal file
View File

@ -0,0 +1,10 @@
#include <stdio.h>
/**
* @brief Prints error
*
* @param {char *} msg
*/
void err_msg (char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}

13
static/404.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body>
</html>

16
static/about/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About</title>
</head>
<body>
<a href="../">Back</a>
<p>Me</p>
<img src="/about/me.jpg" alt="the pig" style="max-height: 200px;" />
</body>
</html>

BIN
static/about/me.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

12
static/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<body>
<p>Testing paragraf</p>
<a href="./about">About me</a>
</body>
</html>