blob: 9a1274ac21b59ea4e5d7eaee34c27f7d97c4ef7d [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
55 // Returns a human readable peer IP address.
56 std::string PeerAddress() const;
57
58 // Prints out the RcvInfo structure.
59 void LogRcvInfo() const;
60
61 // The start of the data.
62 OveralignedChar actual_data[];
63};
64
65void PrintNotification(const Message *msg);
66
67std::string GetHostname();
68
69// Gets and logs the contents of the sctp_status message.
70void LogSctpStatus(int fd, sctp_assoc_t assoc_id);
71
72// Read and allocate a message.
73aos::unique_c_ptr<Message> ReadSctpMessage(int fd, int max_size);
74
Austin Schuh2fe4b712020-03-15 14:21:45 -070075// Returns the max network buffer available for reading for a socket.
76size_t ReadRMemMax();
77// Returns the max network buffer available for writing for a socket.
78size_t ReadWMemMax();
79
Austin Schuhe84c3ed2019-12-14 15:29:48 -080080} // namespace message_bridge
81} // namespace aos
82
83#endif // AOS_NETWORK_SCTP_LIB_H_