| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <string.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include "oob_shared.h" |
| 10 |
#ifdef _WIN32 |
| 11 |
#include <ws2tcpip.h> |
| 12 |
#else |
| 13 |
#include <arpa/inet.h> |
| 14 |
#endif |
| 15 |
#include "socket-shim.h" |
| 16 |
|
| 17 |
static unsigned short csum(unsigned short *buf, int nwords) |
| 18 |
{ |
| 19 |
unsigned long sum; |
| 20 |
for(sum=0; nwords>0; nwords--) |
| 21 |
sum += *buf++; |
| 22 |
sum = (sum >> 16) + (sum &0xffff); |
| 23 |
sum += (sum >> 16); |
| 24 |
return (unsigned short)(~sum); |
| 25 |
} |
| 26 |
|
| 27 |
void populate_ip_header(struct ipheader *ip, char *sourceip, char *destip, unsigned short api_id, unsigned short protocol) |
| 28 |
{ |
| 29 |
ip->iph_verlen = 0x45; |
| 30 |
ip->iph_tos = 0; |
| 31 |
ip->iph_len = htons(sizeof(struct ipheader)); |
| 32 |
ip->iph_ident = htons(api_id); |
| 33 |
ip->iph_flags = 0x0040; |
| 34 |
ip->iph_ttl = 0x40; |
| 35 |
ip->iph_protocol = protocol; |
| 36 |
|
| 37 |
unsigned int address = 0; |
| 38 |
inet_pton(AF_INET, sourceip, &address); |
| 39 |
ip->iph_sourceip = address; |
| 40 |
|
| 41 |
inet_pton(AF_INET, destip, &address); |
| 42 |
ip->iph_destip = address; |
| 43 |
|
| 44 |
ip->iph_chksum = 0; |
| 45 |
} |
| 46 |
|
| 47 |
void populate_ipv4_rist_header(uint16_t address_family, uint8_t *recv_buf, ssize_t recv_bufsize, struct sockaddr * addr, socklen_t addrlen) |
| 48 |
{ |
| 49 |
if (address_family == AF_INET6) { |
| 50 |
|
| 51 |
} |
| 52 |
else { |
| 53 |
|
| 54 |
} |
| 55 |
(void )address_family; |
| 56 |
(void)recv_buf; |
| 57 |
(void)recv_bufsize; |
| 58 |
(void)addr; |
| 59 |
(void)addrlen; |
| 60 |
} |
| 61 |
|
| 62 |
int oob_build_api_payload(uint16_t *buffer, char *sourceip, char *destip, char *message, int message_len) |
| 63 |
{ |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
struct ipheader *ip = (struct ipheader *) buffer; |
| 71 |
|
| 72 |
populate_ip_header(ip, sourceip, destip, RIST_OOB_API_IP_IDENT_AUTH, RIST_OOB_API_IP_PROTOCOL); |
| 73 |
|
| 74 |
|
| 75 |
memcpy((char *)buffer + sizeof(struct ipheader), message, message_len); |
| 76 |
int total_len = sizeof(struct ipheader) + message_len; |
| 77 |
ip->iph_len = htons(total_len); |
| 78 |
|
| 79 |
ip->iph_chksum = csum(buffer, (total_len + 1) / 2); |
| 80 |
return total_len; |
| 81 |
} |
| 82 |
|
| 83 |
char *oob_process_api_message(int buffer_len, char *buffer, int *message_len) |
| 84 |
{ |
| 85 |
struct ipheader *ip = (struct ipheader *) buffer; |
| 86 |
int header_size = sizeof(struct ipheader); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if (buffer_len < header_size) { |
| 93 |
*message_len = RIST_OOB_ERROR_INVALID_LENGTH; |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
if (htons(buffer_len) != ip->iph_len) { |
| 99 |
*message_len = RIST_OOB_ERROR_INVALID_LENGTH; |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
if (ip->iph_protocol != RIST_OOB_API_IP_PROTOCOL) { |
| 105 |
*message_len = RIST_OOB_ERROR_INVALID_PROTO; |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
if (ip->iph_ident != htons(RIST_OOB_API_IP_IDENT_AUTH)) { |
| 111 |
*message_len = RIST_OOB_ERROR_INVALID_IDENT; |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
*message_len = buffer_len - header_size; |
| 117 |
return buffer + header_size; |
| 118 |
} |