forked from dyninc/OpenBFDD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RecvMsg.h
139 lines (124 loc) · 4 KB
/
RecvMsg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**************************************************************
* Copyright (c) 2011, Dynamic Network Services, Inc.
* Jake Montgomery ([email protected]) & Tom Daly ([email protected])
* Distributed under the FreeBSD License - see LICENSE
***************************************************************/
#pragma once
#include "SockAddr.h"
#include "SmartPointer.h"
namespace openbfdd
{
class Socket;
/**
* A container for recv or recvmsg results.
*/
class RecvMsg
{
public:
/**
* Creates an empty RecvMsgData.
*
* Must call AllocBuffers() before calling RecvMsg().
*/
RecvMsg();
/**
* Creates a RecvMsg and allocates storage.
*
* @throw - yes
*
* @param bufferSize [in] - The size of the buffer for receiving data.
*
* @param controlSize [in] - The size of the buffer for receiving control
* messages. This should be large enough for all enabled
* messages. Use CMSG_SPACE to calculate desired size. May be
* 0 when used for recv.
*/
RecvMsg(size_t bufferSize, size_t controlSize);
/**
* Allocates storage.
*
* @throw - yes
*
* @param bufferSize [in] - The size of the buffer for receiving data.
*
* @param controlSize [in] - The size of the buffer for receiving control
* messages. This should be large enough for all enabled
* messages. Use CMSG_SPACE to calculate desired size. May be
* 0 when used for recv.
*/
void AllocBuffers(size_t bufferSize, size_t controlSize);
/**
* Call recvmsg for the given socket.
* Call GetLastError() on failure to get the errno.
*
* @param socket
*
* @return bool - false on failure.
*/
bool DoRecvMsg(const Socket &socket);
/**
* Call recv for the given socket. Call GetLastError() on failure to get the
* errno.
*
* @param socket
* @param flags - Flags for recv.
*
* @return bool - false on failure.
*/
bool DoRecv(const Socket &socket, int flags);
/**
* @return - The error from the last DoRecvMsg call. 0 if it succeeded.
*/
int GetLastError() { return m_error;}
/**
* Gets the TTL or Hops (for IPv6).
*
*
* @param success [out] - False on failure.
*
* @return uint8_t - The ttl or hops. 0 on failure. (Note 0 is also a valid
* value, use success to determine failure).
*/
uint8_t GetTTLorHops(bool *success = NULL);
/**
* Gets the destination address.
*
* @return IpAddr - IsValid() will return false on failure.
*/
const IpAddr& GetDestAddress() { return m_destAddress;}
/**
* The source address.
*
* @return SockAddr - IsValid() will return true on failure.
*/
const SockAddr& GetSrcAddress() { return m_sourceAddress;}
/**
* Gets the data from the last DoRecvMsg(), if successful.
*
*
* @return - Data from the last DoRecvMsg(), if successful. NULL if DoRecvMsg
* was never called, or it failed.
*/
uint8_t* GetData() { return m_dataBufferValidSize ? m_dataBuffer.val : NULL;}
/**
* Gets the size of the data from the last DoRecvMsg(), if successful.
*
*
* @return - The size of valid data from the last DoRecvMsg(), if
* successful. 0 if DoRecvMsg was never called, or it failed.
*/
size_t GetDataSize() { return m_dataBufferValidSize;}
private:
void clear();
private:
Raii<uint8_t>::DeleteArray m_controlBuffer; // Not using vector, because we do not want initialization.
size_t m_controlBufferSize;
Raii<uint8_t>::DeleteArray m_dataBuffer; // Not using vector, because we do not want initialization.
size_t m_dataBufferSize;
size_t m_dataBufferValidSize; // Only valid after successful DoRecvMsg
SockAddr m_sourceAddress;
IpAddr m_destAddress;
int16_t m_ttlOrHops; // -1 for invalid
int m_error;
};
}