Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #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 | |
| 15 | namespace aos { |
| 16 | namespace message_bridge { |
| 17 | |
| 18 | // Resolves a socket and returns the address. This can be either an ipv4 or |
| 19 | // ipv6 address. |
| 20 | struct sockaddr_storage ResolveSocket(std::string_view host, int port); |
| 21 | |
| 22 | // Returns a formatted version of the address. |
| 23 | std::string Address(const struct sockaddr_storage &sockaddr); |
| 24 | // Returns a formatted version of the address family. |
| 25 | std::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. |
| 30 | struct 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 | |
| 65 | void PrintNotification(const Message *msg); |
| 66 | |
| 67 | std::string GetHostname(); |
| 68 | |
| 69 | // Gets and logs the contents of the sctp_status message. |
| 70 | void LogSctpStatus(int fd, sctp_assoc_t assoc_id); |
| 71 | |
| 72 | // Read and allocate a message. |
| 73 | aos::unique_c_ptr<Message> ReadSctpMessage(int fd, int max_size); |
| 74 | |
| 75 | } // namespace message_bridge |
| 76 | } // namespace aos |
| 77 | |
| 78 | #endif // AOS_NETWORK_SCTP_LIB_H_ |