#define _GNU_SOURCE #include "protocol.h" #include "helpers.h" #include #include #include #include const char *http_methods[] = { "GET /","POST /","HEAD /","OPTIONS /","PUT /","DELETE /","CONNECT /","TRACE /",NULL }; bool IsHttp(const uint8_t *data, size_t len) { const char **method; size_t method_len; for (method = http_methods; *method; method++) { method_len = strlen(*method); if (method_len <= len && !memcmp(data, *method, method_len)) return true; } return false; } bool HttpExtractHost(const uint8_t *data, size_t len, char *host, size_t len_host) { const uint8_t *p, *s, *e=data+len; p = (uint8_t*)strncasestr((char*)data, "\nHost:", len); if (!p) return false; p+=6; while(pp) { size_t slen = s-p; if (host && len_host) { if (slen>=len_host) slen=len_host-1; for(size_t i=0;i=6 && data[0]==0x16 && data[1]==0x03 && data[2]>=0x01 && data[2]<=0x03 && data[5]==0x01 && (pntoh16(data+3)+5)<=len; } bool TLSFindExt(const uint8_t *data, size_t len, uint16_t type, const uint8_t **ext, size_t *len_ext) { // +0 // u8 ContentType: Handshake // u16 Version: TLS1.0 // u16 Length // +5 // u8 HandshakeType: ClientHello // u24 Length // u16 Version // c[32] random // u8 SessionIDLength // // u16 CipherSuitesLength // // u8 CompressionMethodsLength // // u16 ExtensionsLength size_t l,ll; l = 1+2+2+1+3+2+32; // SessionIDLength if (len<(l+1)) return false; ll = data[6]<<16 | data[7]<<8 | data[8]; // HandshakeProtocol length if (len<(ll+9)) return false; l += data[l]+1; // CipherSuitesLength if (len<(l+2)) return false; l += pntoh16(data+l)+2; // CompressionMethodsLength if (len<(l+1)) return false; l += data[l]+1; // ExtensionsLength if (len<(l+2)) return false; data+=l; len-=l; l=pntoh16(data); data+=2; len-=2; if (l=4) { uint16_t etype=pntoh16(data); size_t elen=pntoh16(data+2); data+=4; l-=4; if (l=len_host) slen=len_host-1; for(size_t i=0;i