re: Minimal API in c VIEW POST


Since you haven't specified what sort of API you are interested in, I've taken the liberty of assuming a simple line-based protocol (request/response) in this example, most higher layer textual protocols such as HTTP/SMTP/NNTP/IMAP/etc. are built on top of plain lines of text:

// Trivial line-based network API in C
// Phlash, 2019
// Save as: netapi.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

// It went wrong - try to print a descriptive error and bail out
int barf(char *msg) {
        return 1;

int main(int argc, char **argv) {
        int list, sess, size, port = 9000;
        struct sockaddr_in addr;

        if (argc > 1)
                port = atoi(argv[1]);

        list = socket(PF_INET, SOCK_STREAM, 0);
        if (list<0)
                return barf("creating socket");

        addr.sin_family = AF_INET;
        // NB: listens on all interfaces and is accessible from off the machine - beware!
        addr.sin_addr.s_addr = INADDR_ANY;
        addr.sin_port = htons(port);
        if (bind(list, (struct sockaddr *)&addr, sizeof(addr)) < 0)
                return barf("binding to port");
        listen(list, 1);

        printf("waiting for connection on port: %d\n", port);
        size = sizeof(addr);
        while ((sess = accept(list, (struct sockaddr *)&addr, &size)) > 0) {
                // Here we open the raw socket as a stdio FILE so we can use fgets() / fprintf()
                FILE *fp = fdopen(sess, "r+");
                char buf[1024];
                printf("connection from %s, reading a line of input\n", inet_ntoa(addr.sin_addr));
                // Our made up protocol says hello and waits for text
                fprintf(fp, "200 hello, type stuff!\n\r");
                // because stdio is buffered, we flush to ensure our message gets out
                // now we read a line of text terminated by \n
                if (fgets(buf, sizeof(buf), fp) == NULL)
                        return barf("reading input");
                printf("responding\n", buf);
                // echo the text back with some made up protocol bits
                fprintf(fp, "250 message received:\n\r%s200 OK\n\r", buf);
                // we could continue reading/writing but this is an example :)
                size = sizeof(addr);
        return 0;

This 'works for me' on my local machine, compiled with vanilla gcc:

$ gcc -o netapi netapi.c
$ ./netapi

then in another terminal:

$ telnet localhost 9000

Enjoy :)


Thank you so much !

I will build my researches around this, thank you for your time !!


You're welcome - there are a few things I would say about writing network services in C, and particularly on unix-like systems:

  • memory management will be a PITA as soon as you want to do anything complex (there is already a 1024 byte buffer in this example!)
  • look at the inetd way of doing this - it manages networking and sub programs provide protocol handling via stdio, use the pipes Luke :)
  • have a go at turning the example into an HTTP/0.9 server (no request headers, just the request line to parse), if you feel brave look at adding handler threads with pthreads... then never expose to the 'net as pretty much all C network code has vulnerabilities, especially mine!

Have fun!

code of conduct - report abuse