etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

libwebsockets.h (9680B)

      1 #pragma once
      2 /* Mock libwebsockets header for AFL++ fuzzing of ttyd.
      3  * Provides all types and function declarations used by http.c / protocol.c.
      4  * Runtime implementations are in fuzz_lws_mock.c.                         */
      5 #include <stddef.h>
      6 #include <stdint.h>
      7 #include <stdbool.h>
      8 #include <stdio.h>
      9 
     10 /* ── opaque types ──────────────────────────────────────────────────────── */
     11 struct lws           { int _fd; };
     12 struct lws_context   { int _dummy; };
     13 struct lws_vhost     { int _dummy; };
     14 
     15 typedef struct lws           lws_t;
     16 typedef struct lws_context   lws_context;
     17 
     18 /* ── callback reasons ──────────────────────────────────────────────────── */
     19 typedef enum lws_callback_reasons {
     20     LWS_CALLBACK_HTTP = 0,
     21     LWS_CALLBACK_HTTP_WRITEABLE,
     22     LWS_CALLBACK_HTTP_FILE_COMPLETION,
     23     LWS_CALLBACK_HTTP_BIND_PROTOCOL,
     24     LWS_CALLBACK_FILTER_HTTP_CONNECTION,
     25     LWS_CALLBACK_CLOSED_HTTP,
     26     LWS_CALLBACK_ADD_HEADERS,
     27     LWS_CALLBACK_CHECK_ACCESS_RIGHTS,
     28     LWS_CALLBACK_PROCESS_HTML,
     29     LWS_CALLBACK_ESTABLISHED = 100,
     30     LWS_CALLBACK_CLOSED,
     31     LWS_CALLBACK_RECEIVE,
     32     LWS_CALLBACK_SERVER_WRITEABLE,
     33     LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
     34     LWS_CALLBACK_PROTOCOL_INIT,
     35     LWS_CALLBACK_PROTOCOL_DESTROY,
     36     LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
     37     LWS_CALLBACK_SESSION_INFO,
     38     _LWS_CALLBACK_DUMMY = 9999
     39 } lws_callback_reasons;
     40 
     41 /* ── write protocols ───────────────────────────────────────────────────── */
     42 typedef enum lws_write_protocol {
     43     LWS_WRITE_TEXT = 0,
     44     LWS_WRITE_BINARY,
     45     LWS_WRITE_CONTINUATION,
     46     LWS_WRITE_HTTP,
     47     LWS_WRITE_PING,
     48     LWS_WRITE_PONG,
     49     LWS_WRITE_CLOSE,
     50     LWS_WRITE_HTTP_HEADERS,
     51     LWS_WRITE_HTTP_FINAL_HEADERS,
     52     _LWS_WRITE_DUMMY = 9999
     53 } lws_write_protocol;
     54 
     55 /* ── token indexes ─────────────────────────────────────────────────────── */
     56 typedef enum lws_token_indexes {
     57     WSI_TOKEN_GET_URI = 0,
     58     WSI_TOKEN_POST_URI,
     59     WSI_TOKEN_OPTIONS_URI,
     60     WSI_TOKEN_HOST,
     61     WSI_TOKEN_CONNECTION,
     62     WSI_TOKEN_UPGRADE,
     63     WSI_TOKEN_ORIGIN,
     64     WSI_TOKEN_HTTP,
     65     WSI_TOKEN_HTTP_ACCEPT,
     66     WSI_TOKEN_HTTP_AUTHORIZATION,
     67     WSI_TOKEN_HTTP_COOKIE,
     68     WSI_TOKEN_HTTP_CONTENT_LENGTH,
     69     WSI_TOKEN_HTTP_CONTENT_TYPE,
     70     WSI_TOKEN_HTTP_RANGE,
     71     WSI_TOKEN_HTTP_REFERER,
     72     WSI_TOKEN_HTTP_COLON_AUTHORITY,
     73     WSI_TOKEN_HTTP_COLON_METHOD,
     74     WSI_TOKEN_HTTP_COLON_PATH,
     75     WSI_TOKEN_HTTP_COLON_SCHEME,
     76     WSI_TOKEN_HTTP_ACCEPT_ENCODING,
     77     WSI_TOKEN_HTTP_ACCEPT_LANGUAGE,
     78     WSI_TOKEN_HTTP_PRAGMA,
     79     WSI_TOKEN_HTTP_CACHE_CONTROL,
     80     WSI_TOKEN_HTTP_IF_MODIFIED_SINCE,
     81     WSI_TOKEN_HTTP_IF_NONE_MATCH,
     82     WSI_TOKEN_HTTP_TRANSFER_ENCODING,
     83     WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
     84     WSI_TOKEN_HTTP_PROXY_AUTHENTICATE,
     85     WSI_TOKEN_HTTP_PROXY_AUTHORIZATION,
     86     WSI_TOKEN_HTTP_X_FORWARDED_FOR,
     87     WSI_TOKEN_HTTP_URI_ARGS,
     88     WSI_TOKEN_COLON_STATUS,
     89     WSI_TOKEN_HTTP_ACCEPT_RANGES,
     90     WSI_TOKEN_HTTP_LOCATION,
     91     WSI_TOKEN_HTTP_CONTENT_RANGE,
     92     WSI_TOKEN_HTTP_SET_COOKIE,
     93     WSI_TOKEN_HTTP_CONTENT_DISPOSITION,
     94     WSI_TOKEN_HTTP_CONTENT_ENCODING,
     95     WSI_TOKEN_HTTP_CONTENT_LANGUAGE,
     96     WSI_TOKEN_HTTP_CONTENT_LOCATION,
     97     ARRAY_SIZE_WSI_TOKENS,
     98 } lws_token_indexes;
     99 
    100 /* ── HTTP status codes ─────────────────────────────────────────────────── */
    101 #define HTTP_STATUS_OK                    200
    102 #define HTTP_STATUS_NO_CONTENT            204
    103 #define HTTP_STATUS_PARTIAL_CONTENT       206
    104 #define HTTP_STATUS_MOVED_PERMANENTLY     301
    105 #define HTTP_STATUS_FOUND                 302
    106 #define HTTP_STATUS_NOT_MODIFIED          304
    107 #define HTTP_STATUS_BAD_REQUEST           400
    108 #define HTTP_STATUS_UNAUTHORIZED          401
    109 #define HTTP_STATUS_FORBIDDEN             403
    110 #define HTTP_STATUS_NOT_FOUND             404
    111 #define HTTP_STATUS_PROXY_AUTH_REQUIRED   407
    112 #define HTTP_STATUS_INTERNAL_SERVER_ERROR 500
    113 #define HTTP_STATUS_NOT_IMPLEMENTED       501
    114 
    115 /* ── misc types ────────────────────────────────────────────────────────── */
    116 typedef uint64_t lws_filepos_t;
    117 
    118 /* ── misc constants ────────────────────────────────────────────────────── */
    119 #define LWS_PRE 32
    120 #define LWS_CLOSE_STATUS_NOSTATUS               0
    121 #define LWS_CLOSE_STATUS_NORMAL                 1000
    122 #define LWS_CLOSE_STATUS_POLICY_VIOLATION       1008
    123 #define LWS_CLOSE_STATUS_UNEXPECTED_CONDITION   1011
    124 
    125 typedef enum pending_timeout {
    126     NO_PENDING_TIMEOUT = 0,
    127     PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
    128     _PENDING_TIMEOUT_DUMMY = 100
    129 } pending_timeout;
    130 
    131 /* ── protocol registration (used in server.c, not in fuzzing target) ─── */
    132 typedef int (*lws_callback_function)(struct lws *wsi,
    133                                      enum lws_callback_reasons reason,
    134                                      void *user, void *in, size_t len);
    135 struct lws_protocols {
    136     const char            *name;
    137     lws_callback_function  callback;
    138     size_t                 per_session_data_size;
    139     size_t                 rx_buffer_size;
    140     unsigned int           id;
    141     void                  *user;
    142     size_t                 tx_packet_size;
    143 };
    144 
    145 struct lws_http_mount     { int _dummy; };
    146 struct lws_context_creation_info { int _dummy; };
    147 
    148 /* ── logging macros ────────────────────────────────────────────────────── */
    149 #define lwsl_err(fmt, ...)    fprintf(stderr, "ERR: " fmt, ##__VA_ARGS__)
    150 #define lwsl_warn(fmt, ...)   fprintf(stderr, "WRN: " fmt, ##__VA_ARGS__)
    151 #define lwsl_notice(fmt, ...) do {} while (0)
    152 #define lwsl_info(fmt, ...)   do {} while (0)
    153 #define lwsl_debug(fmt, ...)  do {} while (0)
    154 #define lwsl_user(fmt, ...)   do {} while (0)
    155 
    156 /* ── function declarations (implementations in fuzz_lws_mock.c) ────────── */
    157 int    lws_hdr_copy(struct lws *wsi, char *dest, int len,
    158                     enum lws_token_indexes h);
    159 int    lws_hdr_custom_length(struct lws *wsi, const char *name, int nlen);
    160 int    lws_hdr_custom_copy(struct lws *wsi, char *dst, int len,
    161                             const char *name, int nlen);
    162 int    lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len,
    163                               enum lws_token_indexes h, int frag_idx);
    164 int    lws_return_http_status(struct lws *wsi, unsigned int code,
    165                                const char *html_body);
    166 int    lws_add_http_header_status(struct lws *wsi, unsigned int code,
    167                                    unsigned char **p, unsigned char *end);
    168 int    lws_add_http_header_content_length(struct lws *wsi,
    169                                            lws_filepos_t content_length,
    170                                            unsigned char **p,
    171                                            unsigned char *end);
    172 int    lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes h,
    173                                     const unsigned char *value, int length,
    174                                     unsigned char **p, unsigned char *end);
    175 int    lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
    176                                    const unsigned char *value, int length,
    177                                    unsigned char **p, unsigned char *end);
    178 int    lws_finalize_http_header(struct lws *wsi, unsigned char **p,
    179                                  unsigned char *end);
    180 int    lws_finalize_write_http_header(struct lws *wsi, unsigned char *start,
    181                                        unsigned char **p, unsigned char *end);
    182 int    lws_write(struct lws *wsi, unsigned char *buf, size_t len,
    183                  enum lws_write_protocol protocol);
    184 int    lws_callback_on_writable(struct lws *wsi);
    185 void   lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
    186 void   lws_close_reason(struct lws *wsi, int status,
    187                          unsigned char *buf, size_t len);
    188 struct lws_context *lws_get_context(const struct lws *wsi);
    189 int    lws_parse_uri(char *p, const char **prot, const char **host,
    190                      int *port, const char **path);
    191 int    lws_is_final_fragment(struct lws *wsi);
    192 size_t lws_remaining_packet_payload(struct lws *wsi);
    193 int    lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen);
    194 int    lws_serve_http_file(struct lws *wsi, const char *file,
    195                             const char *content_type,
    196                             const char *other_headers, int other_headers_len);
    197 int    lws_http_transaction_completed(struct lws *wsi);
    198 struct lws_context *lws_create_context(struct lws_context_creation_info *info);
    199 void   lws_context_destroy(struct lws_context *context);
    200 int    lws_service(struct lws_context *context, int timeout_ms);
    201 void   lws_cancel_service(struct lws_context *context);
    202 void  *lws_wsi_user(struct lws *wsi);
    203 struct lws *lws_get_network_wsi(struct lws *wsi);
    204 int    lws_get_peer_write_allowance(struct lws *wsi);
    205 int    lws_write_http(struct lws *wsi, void *buf, size_t len);
    206 int    lws_send_pipe_choked(struct lws *wsi);