libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
DoIPConnection.h
Go to the documentation of this file.
1 #ifndef DOIPCONNECTION_H
2 #define DOIPCONNECTION_H
3 
4 
5 #include "DoIPConfig.h"
6 #include "DoIPMessage.h"
7 #include "DoIPNegativeAck.h"
9 #include "DoIPServerModel.h"
10 #include "DoIPDefaultConnection.h"
11 #include <arpa/inet.h>
12 #include <array>
13 #include <iostream>
14 #include <net/if.h>
15 #include <netinet/in.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <unistd.h>
20 
21 
22 namespace doip {
23 
24 
26  public:
27 
28  DoIPConnection(int tcpSocket, UniqueServerModelPtr model);
29 
30  int receiveTcpMessage();
31  size_t receiveFixedNumberOfBytesFromTCP(uint8_t *receivedData, size_t payloadLength);
32 
33  void sendDiagnosticPayload(const DoIPAddress &sourceAddress, const ByteArray &payload);
34  bool isSocketActive() { return m_tcpSocket != 0; };
35 
36  void triggerDisconnection();
37 
38  void sendDiagnosticAck(const DoIPAddress &sourceAddress);
40 
41 
42  // === IConnectionContext interface implementation ===
43 
44  /**
45  * @brief Send a DoIP protocol message to the client
46  * @param msg The DoIP message to send
47  */
48  ssize_t sendProtocolMessage(const DoIPMessage &msg) override;
49 
50  /**
51  * @brief Close the TCP connection
52  * @param reason Why the connection is being closed
53  */
54  void closeConnection(DoIPCloseReason reason) override;
55 
56  /**
57  * @brief Get the server's logical address
58  * @return The server's DoIP logical address
59  */
60  DoIPAddress getServerAddress() const override;
61 
62  /**
63  * @brief Get the currently client (active source) address
64  * @return The client (active source) address, or 0 if no routing is active
65  */
66  DoIPAddress getClientAddress() const override;
67 
68  /**
69  * @brief Set the client (active source) address after routing activation
70  * @param address The client's source address
71  */
72  void setClientAddress(const DoIPAddress& address) override;
73 
74  /**
75  * @brief Handle an incoming diagnostic message (application callback)
76  * @param msg The diagnostic message received
77  * @return std::nullopt for ACK, or NACK code
78  */
80 
81  /**
82  * @brief Notify application that connection is closing
83  * @param reason Why the connection is closing
84  */
85  void notifyConnectionClosed(DoIPCloseReason reason) override;
86 
87  /**
88  * @brief Notify application that diagnostic ACK/NACK was sent
89  * @param ack The ACK/NACK that was sent
90  */
91  void notifyDiagnosticAckSent(DoIPDiagnosticAck ack) override;
92 
93  // === Downstream (Subnet) Operations ===
94 
95  /**
96  * @brief Check if downstream forwarding is available
97  * @return true if the server model has a downstream handler configured
98  */
99  bool hasDownstreamHandler() const override;
100 
101  private:
102  DoIPAddress m_logicalAddress;
103 
104  // TCP socket-specific members
105  int m_tcpSocket;
106  std::array<uint8_t, DOIP_MAXIMUM_MTU> m_receiveBuf{};
107  bool m_isClosing{false}; // TODO: Guard against recursive closeConnection calls -> solve this
108  std::optional<DoIPMessage> m_pendingDownstreamRequest;
109 
110  void closeSocket();
111 
112  int reactOnReceivedTcpMessage(const DoIPMessage &message);
113 
114  void handleMessage(const DoIPMessage &message);
115  ssize_t sendMessage(const uint8_t *message, size_t messageLength);
116 };
117 
118 } // namespace doip
119 
120 #endif /* DOIPCONNECTION_H */
bool hasDownstreamHandler() const override
Check if downstream forwarding is available.
DoIPAddress getClientAddress() const override
Get the currently client (active source) address.
void sendDiagnosticAck(const DoIPAddress &sourceAddress)
void sendDiagnosticNegativeAck(const DoIPAddress &sourceAddress, DoIPNegativeDiagnosticAck ackCode)
DoIPDiagnosticAck notifyDiagnosticMessage(const DoIPMessage &msg) override
Handle an incoming diagnostic message (application callback)
void notifyDiagnosticAckSent(DoIPDiagnosticAck ack) override
Notify application that diagnostic ACK/NACK was sent.
ssize_t sendProtocolMessage(const DoIPMessage &msg) override
Send a DoIP protocol message to the client.
void setClientAddress(const DoIPAddress &address) override
Set the client (active source) address after routing activation.
void closeConnection(DoIPCloseReason reason) override
Close the TCP connection.
size_t receiveFixedNumberOfBytesFromTCP(uint8_t *receivedData, size_t payloadLength)
Receive exactly payloadLength bytes from the TCP stream and put them into receivedData.
DoIPConnection(int tcpSocket, UniqueServerModelPtr model)
void notifyConnectionClosed(DoIPCloseReason reason) override
Notify application that connection is closing.
void sendDiagnosticPayload(const DoIPAddress &sourceAddress, const ByteArray &payload)
DoIPAddress getServerAddress() const override
Get the server's logical address.
Default implementation of IConnectionContext.
Represents a complete DoIP message with internal ByteArray representation.
Definition: DoIPMessage.h:82
Definition: AnsiColors.h:3
uint16_t DoIPAddress
Represents a 16-bit DoIP address consisting of high and low significant bytes.
Definition: DoIPAddress.h:26
std::optional< DoIPNegativeDiagnosticAck > DoIPDiagnosticAck
Alias for diagnostic acknowledgment type.
DoIPCloseReason
Reason for connection closure.
std::unique_ptr< DoIPServerModel > UniqueServerModelPtr
A dynamic array of bytes with utility methods for network protocol handling.
Definition: ByteArray.h:60