Use byte-per-byte accesses when accessing possibly unaligned data.

This commit is contained in:
Reki
2022-05-03 10:20:42 +03:00
parent 1bbe9c5700
commit 7ffa952d01
4 changed files with 34 additions and 21 deletions

View File

@@ -27,3 +27,7 @@ bool is_private6(const struct sockaddr_in6* a);
int set_keepalive(int fd);
int get_so_error(int fd);
static inline uint16_t pntoh16(const uint8_t *p) {
return ((uint16_t)p[0] << 8) | (uint16_t)p[1];
}

View File

@@ -45,7 +45,7 @@ bool HttpExtractHost(const uint8_t *data, size_t len, char *host, size_t len_hos
}
bool IsTLSClientHello(const uint8_t *data, size_t len)
{
return len>=6 && data[0]==0x16 && data[1]==0x03 && data[2]>=0x01 && data[2]<=0x03 && data[5]==0x01 && (ntohs(*(uint16_t*)(data+3))+5)<=len;
return len>=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)
{
@@ -76,7 +76,7 @@ bool TLSFindExt(const uint8_t *data, size_t len, uint16_t type, const uint8_t **
l += data[l]+1;
// CipherSuitesLength
if (len<(l+2)) return false;
l += ntohs(*(uint16_t*)(data+l))+2;
l += pntoh16(data+l)+2;
// CompressionMethodsLength
if (len<(l+1)) return false;
l += data[l]+1;
@@ -84,18 +84,17 @@ bool TLSFindExt(const uint8_t *data, size_t len, uint16_t type, const uint8_t **
if (len<(l+2)) return false;
data+=l; len-=l;
l=ntohs(*(uint16_t*)data);
l=pntoh16(data);
data+=2; len-=2;
if (l<len) return false;
uint16_t ntype=htons(type);
while(l>=4)
{
uint16_t etype=*(uint16_t*)data;
size_t elen=ntohs(*(uint16_t*)(data+2));
uint16_t etype=pntoh16(data);
size_t elen=pntoh16(data+2);
data+=4; l-=4;
if (l<elen) break;
if (etype==ntype)
if (etype==type)
{
if (ext && len_ext)
{
@@ -119,7 +118,7 @@ bool TLSHelloExtractHost(const uint8_t *data, size_t len, char *host, size_t len
// u8 data+2 - server name type. 0=host_name
// u16 data+3 - server name length
if (elen<5 || ext[2]!=0) return false;
size_t slen = ntohs(*(uint16_t*)(ext+3));
size_t slen = pntoh16(ext+3);
ext+=5; elen-=5;
if (slen<elen) return false;
if (ext && len_host)