Debian Codegraph @

tools/oob_shared.c

raw symbols SCIP
1 /* librist. Copyright © 2020 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata, Ph.D. <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
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 // The source IP address
37 unsigned int address = 0;
38 inet_pton(AF_INET, sourceip, &address);
39 ip->iph_sourceip = address;
40 // The destination IP address
41 inet_pton(AF_INET, destip, &address);
42 ip->iph_destip = address;
43 // make sure the checksum area is zero or the calculated checksum will be wrong
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 // TODO: map ipv6 to ipv4
51 }
52 else {
53 // TODO
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 // We populate a valid IP header here but we do not really use it for any type of routing
65 // We also populate a message that has the same information already present in the IP header but write it in text format
66 // This is only for demonstration purposes as we do not use the IP or the text message for anything on
67 // the receiving end or inside the library. However, this is a good method to create internal communication
68 // messages between peers. When designing your solution, just remember that in the OOB channel, there is no
69 // extra buffer delay and no packet recovery.
70 struct ipheader *ip = (struct ipheader *) buffer;
71 // unassigned protocol 252 used for API communication, api_id (54321 to identify this API message type)
72 populate_ip_header(ip, sourceip, destip, RIST_OOB_API_IP_IDENT_AUTH, RIST_OOB_API_IP_PROTOCOL);
73 /* buffer is uint16_t*: pointer math must be done in bytes, or the
74 * message lands at 2x the header size and the receiver can't find it. */
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 // Calculate the checksum for integrity since there is no packet recovery
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 /* The header fields are read before any other check, so the datagram
89 * must be at least a full header; otherwise the three checks below run
90 * on out-of-bounds bytes and message_len can go negative (unbounded
91 * %.*s read in the callers' log path). */
92 if (buffer_len < header_size) {
93 *message_len = RIST_OOB_ERROR_INVALID_LENGTH;
94 return NULL;
95 }
96
97 // Check reported length vs buffer length
98 if (htons(buffer_len) != ip->iph_len) {
99 *message_len = RIST_OOB_ERROR_INVALID_LENGTH;
100 return NULL;
101 }
102
103 // Check for protocol type and only process API messages for now
104 if (ip->iph_protocol != RIST_OOB_API_IP_PROTOCOL) {
105 *message_len = RIST_OOB_ERROR_INVALID_PROTO;
106 return NULL;
107 }
108
109 // Check for only support API call
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 // Move the buffer pointer and return the payload length
116 *message_len = buffer_len - header_size;
117 return buffer + header_size;
118 }