gmid/gmid.h

169 lines
3.9 KiB
C
Raw Normal View History

2020-12-24 16:49:55 +01:00
/*
* Copyright (c) 2020 Omar Polo <op@omarpolo.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2020-12-24 16:48:39 +01:00
#ifndef GMID_H
#define GMID_H
#include <arpa/inet.h>
#include <netinet/in.h>
2021-01-11 13:53:46 +01:00
#include <sys/socket.h>
2020-12-24 16:48:39 +01:00
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <tls.h>
#include <unistd.h>
#ifndef __OpenBSD__
# define pledge(a, b) 0
# define unveil(a, b) 0
#endif
#ifndef INFTIM
# define INFTIM -1
#endif
#define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
/* large enough to hold a copy of a gemini URL and still have extra room */
#define PATHBUF 2048
#define SUCCESS 20
#define TEMP_FAILURE 40
#define NOT_FOUND 51
2021-01-11 15:45:57 +01:00
#define PROXY_REFUSED 53
2020-12-24 16:48:39 +01:00
#define BAD_REQUEST 59
#define MAX_USERS 64
#define HOSTSLEN 64
struct vhost {
const char *domain;
const char *cert;
const char *key;
const char *dir;
const char *cgi;
int dirfd;
};
extern struct vhost hosts[HOSTSLEN];
struct conf {
int foreground;
int port;
int ipv6;
};
extern struct conf conf;
2020-12-24 16:48:39 +01:00
enum {
S_HANDSHAKE,
2020-12-24 16:48:39 +01:00
S_OPEN,
S_INITIALIZING,
S_SENDING,
S_CLOSING,
};
struct client {
struct tls *ctx;
int state;
int code;
const char *meta;
int fd, waiting_on_child;
pid_t child;
char sbuf[1024]; /* static buffer */
void *buf, *i; /* mmap buffer */
ssize_t len, off; /* mmap/static buffer */
int af;
2021-01-10 23:29:22 +01:00
struct sockaddr_storage addr;
struct vhost *host; /* host he's talking to */
2020-12-24 16:48:39 +01:00
};
2021-01-11 14:08:00 +01:00
struct iri {
char *schema;
char *host;
char *port;
uint16_t port_no;
char *path;
char *query;
char *fragment;
};
struct parser {
2021-01-11 14:08:00 +01:00
char *iri;
struct iri *parsed;
const char *err;
};
2020-12-24 16:48:39 +01:00
enum {
FILE_EXISTS,
FILE_EXECUTABLE,
FILE_DIRECTORY,
FILE_MISSING,
};
/* gmid.c */
void sig_handler(int);
2020-12-24 16:48:39 +01:00
int starts_with(const char*, const char*);
int start_reply(struct pollfd*, struct client*, int, const char*);
2020-12-28 09:57:58 +01:00
ssize_t filesize(int);
2020-12-24 16:48:39 +01:00
const char *path_ext(const char*);
const char *mime(const char*);
int check_path(struct client*, const char*, int*);
int check_for_cgi(char *, char*, struct pollfd*, struct client*);
int open_file(char*, char*, struct pollfd*, struct client*);
int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
void cgi_poll_on_child(struct pollfd*, struct client*);
void cgi_poll_on_client(struct pollfd*, struct client*);
2020-12-24 16:48:39 +01:00
void handle_cgi(struct pollfd*, struct client*);
void send_file(char*, char*, struct pollfd*, struct client*);
void send_dir(char*, struct pollfd*, struct client*);
void handle_handshake(struct pollfd*, struct client*);
void handle_open_conn(struct pollfd*, struct client*);
2020-12-24 16:48:39 +01:00
void handle(struct pollfd*, struct client*);
void mark_nonblock(int);
int make_soket(int);
void do_accept(int, struct tls*, struct pollfd*, struct client*);
void goodbye(struct pollfd*, struct client*);
2021-01-10 23:56:33 +01:00
void loop(struct tls*, int, int);
2020-12-24 16:48:39 +01:00
char *absolutify_path(const char*);
void yyerror(const char*);
int parse_portno(const char*);
void parse_conf(const char*);
void load_vhosts(struct tls_config*);
void sandbox();
2020-12-24 16:48:39 +01:00
void usage(const char*);
extern FILE *yyin;
extern int yylineno;
extern int yyparse(void);
extern int yylex(void);
/* utf8.c */
int valid_multibyte_utf8(struct parser*);
2021-01-11 14:08:00 +01:00
/* iri.c */
int parse_iri(char*, struct iri*, const char**);
int trim_req_iri(char*);
2020-12-24 16:48:39 +01:00
#endif