blob: 265d1f59b94d825a3638a685247d6f684695af66 [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#ifndef AOS_NETWORK_SCTP_LIB_H_
2#define AOS_NETWORK_SCTP_LIB_H_
3
4#include <arpa/inet.h>
5#include <netinet/sctp.h>
6
7#include <memory>
8#include <string>
9#include <string_view>
10
11#include "aos/unique_malloc_ptr.h"
12#include "gflags/gflags.h"
13#include "glog/logging.h"
14
15namespace aos {
16namespace message_bridge {
17
18// Resolves a socket and returns the address. This can be either an ipv4 or
19// ipv6 address.
20struct sockaddr_storage ResolveSocket(std::string_view host, int port);
21
22// Returns a formatted version of the address.
23std::string Address(const struct sockaddr_storage &sockaddr);
24// Returns a formatted version of the address family.
25std::string_view Family(const struct sockaddr_storage &sockaddr);
26
27// Message received.
28// This message is malloced bigger than needed and the extra space after it is
29// the data.
30struct Message {
31 // Struct to let us force data to be well aligned.
32 struct OveralignedChar {
33 uint8_t data alignas(32);
34 };
35
36 // Headers.
37 struct {
38 struct sctp_rcvinfo rcvinfo;
39 } header;
40
41 // Address of the sender.
42 struct sockaddr_storage sin;
43
44 // Data type. Is it a block of data, or is it a struct sctp_notification?
45 enum MessageType { kMessage, kNotification } message_type;
46
47 size_t size = 0u;
48 uint8_t *mutable_data() {
49 return reinterpret_cast<uint8_t *>(&actual_data[0].data);
50 }
51 const uint8_t *data() const {
52 return reinterpret_cast<const uint8_t *>(&actual_data[0].data);
53 }
54
Austin Schuhc4202572021-03-31 21:06:55 -070055 uint32_t partial_deliveries = 0;
56
Austin Schuhe84c3ed2019-12-14 15:29:48 -080057 // Returns a human readable peer IP address.
58 std::string PeerAddress() const;
59
60 // Prints out the RcvInfo structure.
61 void LogRcvInfo() const;
62
63 // The start of the data.
64 OveralignedChar actual_data[];
65};
66
67void PrintNotification(const Message *msg);
68
69std::string GetHostname();
70
71// Gets and logs the contents of the sctp_status message.
72void LogSctpStatus(int fd, sctp_assoc_t assoc_id);
73
74// Read and allocate a message.
Austin Schuhc4202572021-03-31 21:06:55 -070075aos::unique_c_ptr<Message> ReadSctpMessage(int fd, size_t max_size);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080076
Austin Schuh2fe4b712020-03-15 14:21:45 -070077// Returns the max network buffer available for reading for a socket.
78size_t ReadRMemMax();
79// Returns the max network buffer available for writing for a socket.
80size_t ReadWMemMax();
81
Austin Schuhe84c3ed2019-12-14 15:29:48 -080082} // namespace message_bridge
83} // namespace aos
84
85#endif // AOS_NETWORK_SCTP_LIB_H_