make it so

This commit is contained in:
2017-09-18 10:15:34 +02:00
commit 1d952df45c
4 changed files with 83 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bin/

15
Makefile Normal file
View File

@@ -0,0 +1,15 @@
TARGET=868
BUILDDIR=bin/
PREFIX=/usr/local/bin/
MAIN=main.c
override CFLAGS+=-Werror -Wall -g -fPIC -O2 -DNDEBUG -ftrapv -Wfloat-equal -Wundef -Wwrite-strings -Wuninitialized -pedantic -std=gnu11
all: main.c
mkdir -p $(BUILDDIR)
$(CC) $(MAIN) -o $(BUILDDIR)$(TARGET) $(CFLAGS)
install: all
install $(BUILDDIR)$(TARGET) $(PREFIX)$(TARGET)
uninstall:
rm -rf $(PREFIX)$(TARGET)

8
README.md Normal file
View File

@@ -0,0 +1,8 @@
# 868
An implementation of [RFC 868](https://tools.ietf.org/html/rfc862), the “time”
service, for the heck of it.
## Limitations
Im not sure this is actually correct.

59
main.c Normal file
View File

@@ -0,0 +1,59 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
void die(const char *msg) {
perror(msg);
exit(1);
}
int32_t get_date() {
int32_t res;
res = time(NULL);
res += 2208988800; // epoch to braindead format
return res;
}
int main(int argc, char *argv[]) {
int sockfd;
int newsockfd;
int port = 37;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
int32_t date;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) die("error opening socket.");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) die("error binding socket.");
clilen = sizeof(cli_addr);
while (1) {
listen(sockfd, 5);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) die("error accepting");
date = get_date();
n = write(newsockfd, &date, 4);
if (n < 0) die("error writing");
close(newsockfd);
}
close(sockfd);
return 0;
}