make it so.
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
bin/
|
15
Makefile
Normal file
15
Makefile
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
TARGET=867
|
||||||
|
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)
|
4
README.md
Normal file
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# 867
|
||||||
|
|
||||||
|
An implementation of [RFC 867](https://tools.ietf.org/html/rfc867), the “time
|
||||||
|
of the day” service, for the heck of it.
|
63
main.c
Normal file
63
main.c
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
void die(const char *msg) {
|
||||||
|
perror(msg);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* get_tod() {
|
||||||
|
time_t t = time(NULL);
|
||||||
|
struct tm *tm = localtime(&t);
|
||||||
|
char* res = malloc(64);
|
||||||
|
|
||||||
|
int len = strftime(res, 64, "%c\n", tm);
|
||||||
|
|
||||||
|
res[len] = '\0';
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
int sockfd;
|
||||||
|
int newsockfd;
|
||||||
|
int port = 13;
|
||||||
|
socklen_t clilen;
|
||||||
|
struct sockaddr_in serv_addr, cli_addr;
|
||||||
|
int n;
|
||||||
|
char* tod;
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
tod = get_tod();
|
||||||
|
n = write(newsockfd, tod, 64);
|
||||||
|
if (n < 0) die("error writing to socket");
|
||||||
|
free(tod);
|
||||||
|
close(newsockfd);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(sockfd);
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user