permanent-satori-hole/include/websock.h
2023-12-27 01:35:22 +01:00

81 lines
2.7 KiB
C

#ifndef H_SATORI_WEBSOCK
#define H_SATORI_WEBSOCK
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "buffer.h"
#include "sock.h"
typedef struct _sat_websock_close_info {
uint16_t code;
char *reason;
} sat_websock_close_info, *sat_websock_close_info_ptr;
typedef struct _sat_websock_ctx {
sat_sock_ptr sock;
sat_websock_close_info_ptr closeInfo;
sat_buffer_ptr buffer;
} sat_websock, *sat_websock_ptr;
sat_websock_ptr sat_websock_alloc(void);
void sat_websock_free(sat_websock_ptr ws);
void sat_websock_init(sat_websock_ptr ws, sat_sock_ptr sock);
void sat_websock_tidy(sat_websock_ptr ws);
void sat_websock_close(sat_websock_ptr ws, int closeCode, char *closeReason);
bool sat_websock_is_closed(sat_websock_ptr ws);
int sat_websock_buffer(sat_websock_ptr ws, size_t want);
int sat_websock_recv(sat_websock_ptr ws, uint8_t *buffer, size_t size);
int sat_websock_send(sat_websock_ptr ws, uint8_t *buffer, size_t count);
typedef enum _sat_websock_opcode {
// non-control
SAT_WEBSOCK_CONTINUE = 0,
SAT_WEBSOCK_TEXT,
SAT_WEBSOCK_BINARY,
// control
SAT_WEBSOCK_CLOSE = 8,
SAT_WEBSOCK_PING,
SAT_WEBSOCK_PONG,
} sat_websock_opcode;
typedef struct _sat_websock_frame_header {
bool isFinal;
sat_websock_opcode opcode;
bool isMasked;
char mask[4];
uint64_t length; // this max size is so ridiculous, should 127 just tell the server to fuck off?
uint64_t offset; // keeps track of data sent for masking
} sat_websock_frame_header, *sat_websock_frame_header_ptr;
void sat_websock_frame_recv_header(sat_websock_ptr ws, sat_websock_frame_header_ptr header);
int sat_websock_frame_recv(sat_websock_ptr ws, sat_websock_frame_header_ptr header, uint8_t *buffer, size_t size);
void sat_websock_frame_send_header(sat_websock_ptr ws, sat_websock_frame_header_ptr header);
int sat_websock_frame_send(sat_websock_ptr ws, sat_websock_frame_header_ptr header, uint8_t *buffer, size_t count);
typedef struct _sat_websock_handshake {
sat_websock_ptr websock;
char *host;
char *path;
char *origin;
char *protocol; // want to switch this to char** but i need to make a glue func first
char key[16];
bool completed;
bool upgraded;
uint16_t statusCode;
char *statusReason;
} sat_websock_handshake, *sat_websock_handshake_ptr;
sat_websock_handshake_ptr sat_websock_handshake_alloc(void);
void sat_websock_handshake_free(sat_websock_handshake_ptr hs, bool freeArg);
int sat_websock_handshake_gen_key(sat_websock_handshake_ptr hs);
int sat_websock_handshake_send(sat_websock_handshake_ptr hs);
int sat_websock_handshake_recv(sat_websock_handshake_ptr hs);
#endif // H_SATORI_WEBSOCK