summaryrefslogtreecommitdiff
path: root/sockets.h
blob: 2a2ef1b3770c73dcbe82881230d6070922f621c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * This file is part of blabouncer (https://www.blatech.co.uk/l_bratch/blabouncer).
 * Copyright (C) 2019 Luke Bratch <luke@bratch.co.uk>.
 *
 * Blabouncer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * Blabouncer is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with blabouncer. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SOCKETS_H_INCLUDED
#define SOCKETS_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <fcntl.h>

#include "functions.h"
#include "structures.h"

#define DEBUG_CRIT 0
#define DEBUG_SOME 1
#define DEBUG_FULL 2

#define BACKLOG 10 // maximum length to which the queue of pending connections for sockfd may grow

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa);

// Create socket to connect to real IRC server
// Returns the socket descriptor on success, or -1 on error
int createserversocket(char *host, char *port);

// Create listening socket to listen for bouncer client connections
int createclientsocket(char *listenport);

void init_openssl();

void cleanup_openssl();

// Create OpenSSL context, type = 0 for IRC server-side (OpenSSL client)
// or type = 1 for bouncer client-side (OpenSSL server)
SSL_CTX *create_openssl_context(int type);

// Configure OpenSSL context, with certfile and keyfile provided if
// IRC server-side or set to NULL if bouncer client-side
void configure_openssl_context(SSL_CTX *ctx, char *certfile, char *keyfile);

// Read from a socket, whether or not using TLS
int sockread(SSL *fd, char *buf, int bufsize, int tls);

// Write to a socket, whether or not using TLS
int socksend(SSL *fd, char *buf, int bufsize, int tls);

// Return character array of latest OpenSSL error
char *openssl_error_string();

// Set a socket "fd" to be blocking ("blocking" = 1) or non-blocking ("blocking" = 0).
// Returns 1 on success or 0 on failure.
int fd_toggle_blocking(int fd, int blocking);

// Attempt to do SSL_accept() on a client with fd "fd".  Expects the socket fd to have just been set
// to non-blocking.  Will make the socket blocking again and set the client's pendingsslaccept status
// to 0 if SSL_accept() succeeds.
// Returns 1 on success, 0 on hard failure, or -1 on SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE.
int openssl_accept(int fd, struct client *clients, struct ircdstate *ircdstate, struct settings *settings, struct clientcodes *clientcodes);

#endif