forked from mirrors/qotd
This commit is contained in:
@@ -9,4 +9,5 @@ RUN make
|
||||
|
||||
FROM alpine
|
||||
COPY --from=builder /opt/builder/build/qotd /
|
||||
COPY quotes.txt /
|
||||
ENTRYPOINT ["/qotd"]
|
||||
@@ -0,0 +1,2 @@
|
||||
Test1
|
||||
Test2
|
||||
@@ -5,32 +5,39 @@
|
||||
#include "server.h"
|
||||
#include "config.h"
|
||||
|
||||
//TODO: object-like C
|
||||
// TODO: object-like C
|
||||
|
||||
extern int errno;
|
||||
|
||||
_Noreturn void udpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
|
||||
_Noreturn void udpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength)
|
||||
{
|
||||
char clientMessage[MESSAGE_STRING_LENGTH], quote[MESSAGE_STRING_LENGTH];
|
||||
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
// receive data
|
||||
|
||||
if (recvfrom(serverSocket, clientMessage, MESSAGE_STRING_LENGTH, 0, clientAddress, (socklen_t *) &addressLength) >= 0) {
|
||||
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) {
|
||||
if (sendto(serverSocket, quote, strlen(quote), 0, clientAddress, addressLength) == -1)
|
||||
{
|
||||
perror("udp: sendto");
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
perror("udp: recvfrom");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tcpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long addressLength) {
|
||||
void tcpListen(int serverSocket, struct sockaddr *clientAddress, unsigned long addressLength)
|
||||
{
|
||||
int clientSocket;
|
||||
char quote[MESSAGE_STRING_LENGTH] = "", clientMessage[MESSAGE_STRING_LENGTH] = "";
|
||||
|
||||
@@ -38,90 +45,96 @@ void tcpListen(int serverSocket, struct sockaddr * clientAddress, unsigned long
|
||||
puts("server listening");
|
||||
|
||||
// accept connections
|
||||
//TODO: t h r e a d s
|
||||
while ((clientSocket = accept(serverSocket, clientAddress, (socklen_t *) &addressLength)) >= 0) {
|
||||
// TODO: t h r e a d s
|
||||
while ((clientSocket = accept(serverSocket, clientAddress, (socklen_t *)&addressLength)) >= 0)
|
||||
{
|
||||
puts("client connected");
|
||||
|
||||
// receive data
|
||||
if (recv(clientSocket, clientMessage, MESSAGE_STRING_LENGTH, 0) >= 0) {
|
||||
printf("client says: %s\n", clientMessage);
|
||||
|
||||
// send data
|
||||
getRandomQuote(quote);
|
||||
printf("responding: \"%s\"\n", quote);
|
||||
if (send(clientSocket, quote, strlen(quote), 0) == -1) {
|
||||
perror("tcp: send");
|
||||
}
|
||||
} else {
|
||||
perror("tcp: recv");
|
||||
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) {
|
||||
if (clientSocket < 0)
|
||||
{
|
||||
puts("client connection failed");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
|
||||
void server(int tcp, int ipv6) {
|
||||
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) {
|
||||
if (ipv6)
|
||||
{
|
||||
serverAddress6.sin6_family = AF_INET6;
|
||||
serverAddress6.sin6_addr = in6addr_any;
|
||||
serverAddress6.sin6_port = htons(DEFAULT_LISTEN_PORT);
|
||||
serverAddress = (struct sockaddr *) &serverAddress6;
|
||||
serverAddress = (struct sockaddr *)&serverAddress6;
|
||||
serverAddressLength = sizeof serverAddress6;
|
||||
|
||||
clientAddress6.sin6_family = AF_INET6;
|
||||
clientAddress = (struct sockaddr *) &clientAddress6;
|
||||
clientAddress = (struct sockaddr *)&clientAddress6;
|
||||
clientAddressLength = sizeof clientAddress6;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
serverAddress4.sin_family = AF_INET;
|
||||
serverAddress4.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddress4.sin_port = htons(DEFAULT_LISTEN_PORT);
|
||||
serverAddress = (struct sockaddr *) &serverAddress4;
|
||||
serverAddress = (struct sockaddr *)&serverAddress4;
|
||||
serverAddressLength = sizeof serverAddress4;
|
||||
|
||||
clientAddress4.sin_family = AF_INET;
|
||||
clientAddress = (struct sockaddr *) &clientAddress4;
|
||||
clientAddress = (struct sockaddr *)&clientAddress4;
|
||||
clientAddressLength = sizeof clientAddress4;
|
||||
}
|
||||
|
||||
if (tcp) {
|
||||
if (tcp)
|
||||
{
|
||||
socketType = SOCK_STREAM;
|
||||
socketProtocol = IPPROTO_TCP;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
socketType = SOCK_DGRAM;
|
||||
socketProtocol = IPPROTO_UDP;
|
||||
}
|
||||
|
||||
// create sock
|
||||
serverSocket = socket(serverAddress->sa_family, socketType, socketProtocol);
|
||||
if (serverSocket == -1) {
|
||||
if (serverSocket == -1)
|
||||
{
|
||||
puts("socket not created");
|
||||
exit(1);
|
||||
}
|
||||
puts("socket created");
|
||||
|
||||
// bind sock
|
||||
if (bind(serverSocket, serverAddress, serverAddressLength) < 0) {
|
||||
if (bind(serverSocket, serverAddress, serverAddressLength) < 0)
|
||||
{
|
||||
puts("bind failed");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
// TODO: threads to run both at the same time
|
||||
if (tcp) {
|
||||
if (tcp)
|
||||
{
|
||||
tcpListen(serverSocket, clientAddress, clientAddressLength);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
udpListen(serverSocket, clientAddress, clientAddressLength);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user