Skip to content

cajald/anntp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

anntp

a simple NNTP library for C

Support Ukraine!

anntp is a minimalistic, light, and hackable single-header NNTP (Network News Transfer Protocol) client library implemented in (semi-)portable C99.

It is low level, but still usable, and portable between UNIX-like systems.

Right now, it supports:

  • multiple connections,
  • low-level server/client I/O, plus a bazillion variations of the same two functions.
  • TLS (just TLS; no STARTTLS, that is on the works, sorry),
  • error handling
  • "idiomatic" C API

Requirements

  • A C compiler (gcc, clang, tcc, ...)
  • OpenSSL/LibreSSL (optional, for TLS support)
  • An UNIX-like system.

Usage

  1. Copy anntp.h to your project

  2. Include the library in as many files as you want, but in exactly ONE file, add:

    #define ANNTP_IMPLEMENTATION

    Before including anntp.h

  3. If you want TLS support, add this to every file that uses anntp:

    #define ANNTP_TLS
    1. If you want STARTTLS, use the anntp_starttls() function once you've made the non-TLS connection.
  4. ENJOY!

Memory Management

You need to free stuff manually, this is C, what did you expect?

The anntp_free_* functions free stuff. See their args or name to see what they free.

NOTE: All strings returned by the library are explicitly dynamically allocated.

Error handling

There is an AnntpErrCode enum with the different error types:

  • ANE_OK: success
  • ANE_PARAMS: wrong parameters
  • ANE_IO: very generic I/O error (OOM, sending, recieving... the spectrum is broad)
  • ANE_TLS: only produced if TLS support is on, TLS encryption failure
  • ANE_PROTO: protocol error; the server returned an unexpected response
  • ANE_AUTH: authentication failure

Some functions probably return their error codes in negative if they return a ssize_t success; to get the real error code of a function use the AERR() macro.

Examples

See examples/ for a couple of examples, but for now you can read the greeting with:

#include <stdio.h>

#define ANNTP_IMPLEMENTATION
#define ANNTP_TLS /* remove for no tls */
#include "anntp.h"

int
main(int argc, char** argv)
{
	char host[] = "news.example.com";
	char* port = "563"; /* change to 119 for no tls */
	anntp_init();
	if (argc >= 2)
		host = argv[2];
	AnntpConnection* c = anntp_mkconn(host, port, false);
	if (!c)
		return 1;

	char line[256];
	ssize_t res = anntp_readline(c, line, sizeof(line));
	if (!ANNTP_ISERR(res))
		puts(line);
	else {
		fprintf(stderr, anntp_strerror(AERR(res)));
		return 1;
	}

	anntp_freeconn(c);
	return 0;
}

Contributing

The git repository is on sr.ht, contribute patches via the anntp-devel mailing list. There's also an user's mailing list; the anntp-users list.

Send issues to this tracker.

License

CC0. See the LICENSE file for more information. Warranty:

Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law.

Originally written by Mario Rosell <mario AT mariorosell DOT es>.

About

a simple nntp implementation in C; in just one header!

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages