forked from mirrors/qotd
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e23582ac62 | ||
|
|
252493b4ea | ||
|
|
cea7131f77 | ||
|
|
ee2eecf6a2 | ||
|
|
195d4352a0 |
@@ -1,38 +0,0 @@
|
|||||||
name: CMake
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master, dev ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ master, dev ]
|
|
||||||
|
|
||||||
env:
|
|
||||||
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
|
|
||||||
BUILD_TYPE: RelWithDebInfo
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
# The CMake configure and build commands are platform agnostic and should work equally
|
|
||||||
# well on Windows or Mac. You can convert this to a matrix build if you need
|
|
||||||
# cross-platform coverage.
|
|
||||||
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Configure CMake
|
|
||||||
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
|
|
||||||
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
|
|
||||||
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
# Build your program with the given configuration
|
|
||||||
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
|
|
||||||
|
|
||||||
- name: Test
|
|
||||||
working-directory: ${{github.workspace}}/build
|
|
||||||
# Execute tests defined by the CMake configuration.
|
|
||||||
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
|
|
||||||
run: ctest -C ${{env.BUILD_TYPE}}
|
|
||||||
|
|
||||||
+1
-2
@@ -1,4 +1,3 @@
|
|||||||
# Project exclude paths
|
# Project exclude paths
|
||||||
/cmake-build-debug/
|
/cmake-build-debug/
|
||||||
/.idea/
|
/.idea/
|
||||||
build
|
|
||||||
Vendored
-7
@@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
// Use IntelliSense to learn about possible attributes.
|
|
||||||
// Hover to view descriptions of existing attributes.
|
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": []
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
cmake_minimum_required(VERSION 3.19)
|
cmake_minimum_required(VERSION 3.19)
|
||||||
project(qotd C)
|
project(qotd C)
|
||||||
|
|
||||||
message("STATIC_BUILD=${STATIC_BUILD}")
|
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
add_executable(qotd main.c quote.c quote.h server.c server.h config.h)
|
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
|
||||||
|
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
if ( STATIC_BUILD )
|
add_executable(qotd main.c quote.c quote.h server.c server.h config.h)
|
||||||
target_link_libraries(qotd -static)
|
target_link_libraries(qotd Threads::Threads)
|
||||||
endif()
|
|
||||||
-14
@@ -1,14 +0,0 @@
|
|||||||
FROM git.legonzaur.fr/legonzaur/qotd/builder:latest AS builder
|
|
||||||
|
|
||||||
WORKDIR /opt/builder
|
|
||||||
|
|
||||||
COPY ./server .
|
|
||||||
RUN cmake -B build -DSTATIC_BUILD=true
|
|
||||||
WORKDIR build
|
|
||||||
RUN make
|
|
||||||
|
|
||||||
FROM scratch
|
|
||||||
COPY --from=builder /opt/builder/build/qotd /
|
|
||||||
COPY quotes.txt /
|
|
||||||
STOPSIGNAL SIGKILL
|
|
||||||
ENTRYPOINT ["/qotd"]
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
FROM alpine:latest
|
|
||||||
|
|
||||||
RUN apk add --no-cache cmake make gcc musl-dev
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.19)
|
|
||||||
project(qotd C)
|
|
||||||
|
|
||||||
message("STATIC_BUILD=${STATIC_BUILD}")
|
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
|
||||||
|
|
||||||
add_executable(qotd_c main.c)
|
|
||||||
|
|
||||||
if ( STATIC_BUILD )
|
|
||||||
target_link_libraries(qotd -static)
|
|
||||||
endif()
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
#include "client.h"
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../server/config.h
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
#include <arpa/inet.h> // inet_addr()
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <strings.h> // bzero()
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <unistd.h> // read(), write(), close()
|
|
||||||
#include <errno.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include "config.h"
|
|
||||||
void unescape(char *buffer, size_t buf_len)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
bool last_is_escape = false;
|
|
||||||
for (i = 0; i < buf_len; i++)
|
|
||||||
{
|
|
||||||
if (last_is_escape)
|
|
||||||
{
|
|
||||||
switch (buffer[i])
|
|
||||||
{
|
|
||||||
case 'n':
|
|
||||||
buffer[i - 1] = ' ';
|
|
||||||
buffer[i] = '\n';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buffer[i] == '\\')
|
|
||||||
{
|
|
||||||
last_is_escape = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
last_is_escape = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int sockfd, connfd;
|
|
||||||
struct sockaddr_in servaddr4, cli;
|
|
||||||
|
|
||||||
unsigned int servaddrlength;
|
|
||||||
// socket create and verification
|
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (sockfd == -1)
|
|
||||||
{
|
|
||||||
printf("socket creation failed...\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// assign IP, PORT
|
|
||||||
servaddr4.sin_family = AF_INET;
|
|
||||||
servaddr4.sin_addr.s_addr = inet_addr("45.81.62.108");
|
|
||||||
servaddr4.sin_port = htons(DEFAULT_LISTEN_PORT);
|
|
||||||
|
|
||||||
int status = connect(sockfd, (struct sockaddr *)&servaddr4, sizeof servaddr4);
|
|
||||||
// connect the client socket to server socket
|
|
||||||
if (status != 0)
|
|
||||||
{
|
|
||||||
printf("%i\n", errno);
|
|
||||||
printf("connection with the server failed...\n");
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sockaddr *servaddr = (struct sockaddr *)&servaddr4;
|
|
||||||
unsigned int ss = sizeof servaddr;
|
|
||||||
// function for chat
|
|
||||||
char clientMessage[MESSAGE_STRING_LENGTH] = {0};
|
|
||||||
recvfrom(sockfd, clientMessage, MESSAGE_STRING_LENGTH, 0, servaddr, &ss);
|
|
||||||
unescape(clientMessage, (size_t)MESSAGE_STRING_LENGTH);
|
|
||||||
puts(clientMessage);
|
|
||||||
|
|
||||||
// close the socket
|
|
||||||
close(sockfd);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
//
|
||||||
|
// Created by k0rb4k on 26/11/2021.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef QOTD_CONFIG_H
|
||||||
|
#define QOTD_CONFIG_H
|
||||||
|
|
||||||
|
#define QUOTES_FILE "./qotd.txt"
|
||||||
|
#define MESSAGE_STRING_LENGTH 2048
|
||||||
|
#define DEFAULT_LISTEN_PORT 7878 // should be 17 to be RFC compliant
|
||||||
|
|
||||||
|
#endif //QOTD_CONFIG_H
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// TODO: shell args
|
// TODO: shell args
|
||||||
server(1, 1);
|
server();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+14
-21
@@ -5,33 +5,30 @@
|
|||||||
#include "quote.h"
|
#include "quote.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
void getRandomQuote(char quote[2048])
|
//TODO: error handling
|
||||||
{
|
|
||||||
|
void getRandomQuote(char quote[2048]) {
|
||||||
int lineCount;
|
int lineCount;
|
||||||
long line;
|
long line;
|
||||||
|
|
||||||
if ((lineCount = getFileLineCount(QUOTES_FILE)) == -1)
|
if ((lineCount = getFileLineCount(QUOTES_FILE)) == -1) {
|
||||||
{
|
|
||||||
puts("file does not exist");
|
puts("file does not exist");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
line = rand() % lineCount;
|
line = random() % lineCount;
|
||||||
|
|
||||||
getQuote(line, quote);
|
getQuote(line, quote);
|
||||||
}
|
}
|
||||||
|
|
||||||
void getQuote(long line, char quote[2048])
|
void getQuote(long line, char quote[2048]) {
|
||||||
{
|
FILE* list;
|
||||||
FILE *list;
|
|
||||||
|
|
||||||
list = fopen(QUOTES_FILE, "r");
|
list = fopen(QUOTES_FILE, "r");
|
||||||
|
|
||||||
/*skip lines*/
|
/*skip lines*/
|
||||||
while (line > 0)
|
while (line > 0) {
|
||||||
{
|
if (fgetc(list) == '\n') {
|
||||||
if (fgetc(list) == '\n')
|
|
||||||
{
|
|
||||||
line--;
|
line--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,22 +38,18 @@ void getQuote(long line, char quote[2048])
|
|||||||
fclose(list);
|
fclose(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getFileLineCount(char *filename)
|
int getFileLineCount(char *filename) {
|
||||||
{
|
|
||||||
char c;
|
char c;
|
||||||
int lineCount = 0;
|
int lineCount = 0;
|
||||||
FILE *quotesFile = fopen(filename, "r");
|
FILE* quotesFile = fopen(filename, "r");
|
||||||
|
|
||||||
if (quotesFile == NULL)
|
if (quotesFile == NULL) {
|
||||||
{
|
|
||||||
printf("no quote file found at \"%s\"\n", filename);
|
printf("no quote file found at \"%s\"\n", filename);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((c = (char)fgetc(quotesFile)) != EOF)
|
while ((c = (char) fgetc(quotesFile)) != EOF) {
|
||||||
{
|
if (c == '\n') {
|
||||||
if (c == '\n')
|
|
||||||
{
|
|
||||||
lineCount++;
|
lineCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,11 +8,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <bits/types/FILE.h>
|
||||||
|
#include <bits/types/struct_FILE.h>
|
||||||
|
|
||||||
void getRandomQuote(char quote[2048]);
|
void getRandomQuote(char quote[2048]);
|
||||||
|
|
||||||
void getQuote(long line, char quote[2048]);
|
void getQuote(long line, char quote[2048]);
|
||||||
|
|
||||||
int getFileLineCount(char *filename);
|
int getFileLineCount(char* filename);
|
||||||
|
|
||||||
#endif // QOTD_QUOTE_H
|
#endif //QOTD_QUOTE_H
|
||||||
-35
@@ -1,35 +0,0 @@
|
|||||||
@dodge9395 imagine, tu sors avec un marionnettiste-\n@berkut.2000 MAIS JOUE J'EN AI RIEN À FOUTRE
|
|
||||||
@xanatonaka Si y'a un second au tour aux présidentielles entre Erika Furudo et Adolf Hitler, je vote Adolf Hitler.
|
|
||||||
@xanatonaka je pensais qu'il voulait dire les bougnoules
|
|
||||||
@dodge9395 mon gros zizi\n@lea.mfao oh wow !
|
|
||||||
@dodge9395 suce ma bite dédé\n@lea.mfao oh wow !
|
|
||||||
@legonzaur le sexe, c'est un metroidvania?
|
|
||||||
@xanatonaka Je suis Xana Lennon, HAHA
|
|
||||||
@xanatonaka bègue c'est le b de LGBT
|
|
||||||
@lea.mfao Pourquoi tu voudrais te foutre en l'air alors que tu pourrais me foutre en l'air
|
|
||||||
@lea.mfao LA PETASSE C'EST MOI
|
|
||||||
@lea.mfao Cette action jme fais enculer, j'aime bien
|
|
||||||
@dodge9395 Pourquoi la recherche? Parce que la haine
|
|
||||||
@pixelboum Zoo zan sen
|
|
||||||
@berkut.2000 Faut arrêter de respirer à un moment
|
|
||||||
@oshiida Mange putain de bites
|
|
||||||
@pixelboum Toutes les sorcières sont lesbiennes
|
|
||||||
@legonzaur Un taboulé chaud
|
|
||||||
@fhuris les boonas sont vanilla
|
|
||||||
@oshiida de spoèces à ours qui parlent
|
|
||||||
@fhuris parce que je suis un bandeur de boules
|
|
||||||
@dodge9395 une game sans pix interdit à tous les pix surtout Pixelboum du serveur discord Let me Lulu, celui qui s'appelle Baptiste Dusart et qui a étudié à 2600 (autres baptiste bienvenue)\nnon à baptiste
|
|
||||||
@smolyuli Défonce moi mais c'est pas moi!
|
|
||||||
@lea.mfao C'est pas ma faute si je suis raciste
|
|
||||||
@usaminolulu Passons à la phase douche!
|
|
||||||
@dodge9395 Jambon pourquoi tu es un perso de Baki
|
|
||||||
@lea.mfao Typique Pix qui balance son sexe partout
|
|
||||||
@berkut.2000 Tu te champignon
|
|
||||||
@lea.mfao Pov : je me matraque le dé pendant le date
|
|
||||||
@lea.mfao j'aimerais beaucoup avoir mion/shion à mon q
|
|
||||||
@legonzaur moi quand\nQuand je veux bang un aspirateur
|
|
||||||
@pixelboum Le sexe mais avec un coil head
|
|
||||||
@berkut.2000 Non je vais me pisser dessus sinon
|
|
||||||
@legonzaur On rajoute la saucise?
|
|
||||||
@fhuris Finalement, pourquoi checker quand on peut voler ailleurs
|
|
||||||
https://discord.com/channels/755837083742109797/821436483349119006/1269242508819431547
|
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
//
|
||||||
|
// Created by k0rb4k on 30/03/2021.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "server.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
//TODO: object-like C
|
||||||
|
//TODO: refactoring, cleanup
|
||||||
|
|
||||||
|
extern int errno;
|
||||||
|
|
||||||
|
_Noreturn void udpListen(void *args[]) {
|
||||||
|
struct sockinfo *server = (struct sockinfo *) args[0];
|
||||||
|
struct sockinfo *client = (struct sockinfo *) args[1];
|
||||||
|
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||||
|
|
||||||
|
// create args
|
||||||
|
server->handle = socket(server->address->sa_family, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
if (server->handle == -1) {
|
||||||
|
perror("socket not created");
|
||||||
|
pthread_exit((void *) 1);
|
||||||
|
}
|
||||||
|
puts("socket created");
|
||||||
|
|
||||||
|
// bind args
|
||||||
|
if (bind(server->handle, server->address, server->addressLength) < 0) {
|
||||||
|
perror("bind failed");
|
||||||
|
pthread_exit((void *) 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
// receive data
|
||||||
|
if (recvfrom(server->handle, clientMessage, MESSAGE_STRING_LENGTH, 0, client->address,
|
||||||
|
&client->addressLength) >= 0) {
|
||||||
|
printf("received: %s\n", clientMessage);
|
||||||
|
|
||||||
|
// send data
|
||||||
|
getRandomQuote(quote);
|
||||||
|
printf("sending: %s\n", quote);
|
||||||
|
if (sendto(server->handle, quote, (long) strlen(quote), 0, client->address, client->addressLength) == -1) {
|
||||||
|
perror("[UDP: SENDTO]");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
perror("[UDP: RECVFROM]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void tcpListen(void *args[]) {
|
||||||
|
struct sockinfo *server = (struct sockinfo *) args[0];
|
||||||
|
struct sockinfo *client = (struct sockinfo *) args[1];
|
||||||
|
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||||
|
|
||||||
|
// create args
|
||||||
|
server->handle = socket(server->address->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if (server->handle == -1) {
|
||||||
|
perror("socket not created");
|
||||||
|
pthread_exit((void *) 1);
|
||||||
|
}
|
||||||
|
puts("socket created");
|
||||||
|
|
||||||
|
// bind args
|
||||||
|
if (bind(server->handle, server->address, server->addressLength) < 0) {
|
||||||
|
perror("bind failed");
|
||||||
|
pthread_exit((void *) 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
listen(server->handle, 5);
|
||||||
|
puts("server listening");
|
||||||
|
|
||||||
|
// accept connections
|
||||||
|
//TODO: t h r e a d s
|
||||||
|
while ((client->handle = accept(server->handle, client->address, &client->addressLength)) >= 0) {
|
||||||
|
puts("client connected");
|
||||||
|
|
||||||
|
// receive data
|
||||||
|
if (recv(client->handle, clientMessage, MESSAGE_STRING_LENGTH, 0) >= 0) {
|
||||||
|
printf("client says: %s\n", clientMessage);
|
||||||
|
|
||||||
|
// send data
|
||||||
|
getRandomQuote(quote);
|
||||||
|
printf("responding: \"%s\"\n", quote);
|
||||||
|
if (send(client->handle, quote, (long) strlen(quote), 0) == -1) {
|
||||||
|
perror("[TCP: SEND]");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
perror("[TCP: RECV]");
|
||||||
|
}
|
||||||
|
|
||||||
|
close(client->handle);
|
||||||
|
|
||||||
|
puts("closed connection");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (client->handle < 0) {
|
||||||
|
perror("client connection failed");
|
||||||
|
pthread_exit((void *) 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void server() {
|
||||||
|
struct sockinfo client;
|
||||||
|
struct sockinfo udp, tcp;
|
||||||
|
struct sockaddr_in6 serverAddress, clientAddress;
|
||||||
|
|
||||||
|
serverAddress.sin6_family = AF_INET6;
|
||||||
|
serverAddress.sin6_addr = in6addr_any;
|
||||||
|
serverAddress.sin6_port = htons(DEFAULT_LISTEN_PORT);
|
||||||
|
|
||||||
|
tcp.address = (struct sockaddr *) &serverAddress;
|
||||||
|
udp.address = (struct sockaddr *) &serverAddress;
|
||||||
|
tcp.addressLength = sizeof serverAddress;
|
||||||
|
udp.addressLength = sizeof serverAddress;
|
||||||
|
|
||||||
|
clientAddress.sin6_family = AF_INET6;
|
||||||
|
client.address = (struct sockaddr *) &clientAddress;
|
||||||
|
client.addressLength = sizeof clientAddress;
|
||||||
|
|
||||||
|
void *tcpArgs[] = {&tcp, &client};
|
||||||
|
void *udpArgs[] = {&udp, &client};
|
||||||
|
|
||||||
|
// TODO: threads to run both at the same time
|
||||||
|
pthread_t tcpHandle, udpHandle;
|
||||||
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_create(&tcpHandle, &attr, (void *(*)(void *)) tcpListen, tcpArgs);
|
||||||
|
pthread_create(&udpHandle, &attr, (void *(*)(void *)) udpListen, udpArgs);
|
||||||
|
|
||||||
|
void *tcpReturn, *udpReturn;
|
||||||
|
pthread_join(tcpHandle, tcpReturn);
|
||||||
|
pthread_join(udpHandle, udpReturn);
|
||||||
|
}
|
||||||
@@ -14,13 +14,20 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#include "quote.h"
|
#include "quote.h"
|
||||||
|
|
||||||
void server(int tcp, int ipv6);
|
struct sockinfo {
|
||||||
|
int handle;
|
||||||
|
struct sockaddr * address;
|
||||||
|
socklen_t addressLength;
|
||||||
|
};
|
||||||
|
|
||||||
void tcpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength);
|
void server();
|
||||||
|
|
||||||
_Noreturn void udpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength);
|
void tcpListen(void * args[]);
|
||||||
|
|
||||||
|
_Noreturn void udpListen(void * args[]);
|
||||||
|
|
||||||
#endif //QOTD_SERVER_H
|
#endif //QOTD_SERVER_H
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by k0rb4k on 26/11/2021.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef QOTD_CONFIG_H
|
|
||||||
#define QOTD_CONFIG_H
|
|
||||||
|
|
||||||
#define QUOTES_FILE "./quotes.txt"
|
|
||||||
#define MESSAGE_STRING_LENGTH 512
|
|
||||||
#define DEFAULT_LISTEN_PORT 17 // should be 17 to be RFC compliant
|
|
||||||
|
|
||||||
#endif // QOTD_CONFIG_H
|
|
||||||
-140
@@ -1,140 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by k0rb4k on 30/03/2021.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "server.h"
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
// TODO: object-like C
|
|
||||||
|
|
||||||
extern int errno;
|
|
||||||
|
|
||||||
_Noreturn void udpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength)
|
|
||||||
{
|
|
||||||
char clientMessage[MESSAGE_STRING_LENGTH], quote[MESSAGE_STRING_LENGTH];
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
// receive data
|
|
||||||
|
|
||||||
if (recvfrom(serverSocket, clientMessage, MESSAGE_STRING_LENGTH, 0, clientAddress, (socklen_t *)&addressLength) >= 0)
|
|
||||||
{
|
|
||||||
printf("received: %s\n", clientMessage);
|
|
||||||
|
|
||||||
// send data
|
|
||||||
getRandomQuote(quote);
|
|
||||||
printf("sending: %s\n", quote);
|
|
||||||
if (sendto(serverSocket, quote, strlen(quote), 0, clientAddress, addressLength) == -1)
|
|
||||||
{
|
|
||||||
perror("udp: sendto");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
perror("udp: recvfrom");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void tcpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength)
|
|
||||||
{
|
|
||||||
int clientSocket;
|
|
||||||
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
|
||||||
|
|
||||||
listen(serverSocket, 5);
|
|
||||||
puts("server listening");
|
|
||||||
|
|
||||||
// accept connections
|
|
||||||
// TODO: t h r e a d s
|
|
||||||
while ((clientSocket = accept(serverSocket, clientAddress, (socklen_t *)&addressLength)) >= 0)
|
|
||||||
{
|
|
||||||
puts("client connected");
|
|
||||||
|
|
||||||
getRandomQuote(quote);
|
|
||||||
printf("responding: \"%s\"\n", quote);
|
|
||||||
if (send(clientSocket, quote, strlen(quote), 0) == -1)
|
|
||||||
{
|
|
||||||
perror("tcp: send");
|
|
||||||
}
|
|
||||||
close(clientSocket);
|
|
||||||
|
|
||||||
puts("closed connection");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (clientSocket < 0)
|
|
||||||
{
|
|
||||||
puts("client connection failed");
|
|
||||||
exit(3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void server(int tcp, int ipv6)
|
|
||||||
{
|
|
||||||
int serverSocket, socketType, socketProtocol;
|
|
||||||
unsigned long serverAddressLength, clientAddressLength;
|
|
||||||
struct sockaddr *serverAddress, *clientAddress;
|
|
||||||
struct sockaddr_in serverAddress4, clientAddress4;
|
|
||||||
struct sockaddr_in6 serverAddress6, clientAddress6;
|
|
||||||
|
|
||||||
if (ipv6)
|
|
||||||
{
|
|
||||||
serverAddress6.sin6_family = AF_INET6;
|
|
||||||
serverAddress6.sin6_addr = in6addr_any;
|
|
||||||
serverAddress6.sin6_port = htons(DEFAULT_LISTEN_PORT);
|
|
||||||
serverAddress = (struct sockaddr *)&serverAddress6;
|
|
||||||
serverAddressLength = sizeof serverAddress6;
|
|
||||||
|
|
||||||
clientAddress6.sin6_family = AF_INET6;
|
|
||||||
clientAddress = (struct sockaddr *)&clientAddress6;
|
|
||||||
clientAddressLength = sizeof clientAddress6;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
serverAddress4.sin_family = AF_INET;
|
|
||||||
serverAddress4.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
serverAddress4.sin_port = htons(DEFAULT_LISTEN_PORT);
|
|
||||||
serverAddress = (struct sockaddr *)&serverAddress4;
|
|
||||||
serverAddressLength = sizeof serverAddress4;
|
|
||||||
|
|
||||||
clientAddress4.sin_family = AF_INET;
|
|
||||||
clientAddress = (struct sockaddr *)&clientAddress4;
|
|
||||||
clientAddressLength = sizeof clientAddress4;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tcp)
|
|
||||||
{
|
|
||||||
socketType = SOCK_STREAM;
|
|
||||||
socketProtocol = IPPROTO_TCP;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
socketType = SOCK_DGRAM;
|
|
||||||
socketProtocol = IPPROTO_UDP;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create sock
|
|
||||||
serverSocket = socket(serverAddress->sa_family, socketType, socketProtocol);
|
|
||||||
if (serverSocket == -1)
|
|
||||||
{
|
|
||||||
puts("socket not created");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
puts("socket created");
|
|
||||||
|
|
||||||
// bind sock
|
|
||||||
if (bind(serverSocket, serverAddress, serverAddressLength) < 0)
|
|
||||||
{
|
|
||||||
puts("bind failed");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: threads to run both at the same time
|
|
||||||
if (tcp)
|
|
||||||
{
|
|
||||||
tcpListen(serverSocket, clientAddress, clientAddressLength);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
udpListen(serverSocket, clientAddress, clientAddressLength);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user