ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmsg.cc
Go to the documentation of this file.
1 #include "cmsg.h"
2 #include <sys/socket.h>
3 #include <string.h>
4 
5 namespace ns3 {
6 
7 Cmsg::Cmsg (struct msghdr *msg)
8  : m_msg (msg),
9  m_current ((uint8_t *)msg->msg_control),
10  m_len (msg->msg_controllen)
11 {
12 }
13 void
14 Cmsg::Add (int level, int type, int len, const uint8_t *buffer)
15 {
16  int cmsglen = CMSG_SPACE (len);
17  if (m_len < cmsglen)
18  {
19  m_msg->msg_flags |= MSG_CTRUNC;
20  return;
21  }
22  struct cmsghdr msghdr;
23  msghdr.cmsg_len = cmsglen;
24  msghdr.cmsg_level = level;
25  msghdr.cmsg_type = type;
26  memcpy (m_current, &msghdr, sizeof (struct cmsghdr));
27  memcpy (CMSG_DATA ((struct cmsghdr*)m_current), buffer, cmsglen - sizeof (struct cmsghdr));
28  m_current += cmsglen;
29  m_len -= cmsglen;
30 }
31 
32 
33 int
34 Cmsg::GetNext (int *level, int *type, int *len, uint8_t **buffer)
35 {
36  struct cmsghdr *cm = NULL;
37 
38  if (m_len < (int)CMSG_LEN (0))
39  {
40  return -1;
41  }
42 
43  cm = (struct cmsghdr *)m_current;
44  if (cm->cmsg_len == 0 || (int)cm->cmsg_len > m_len)
45  {
46  return -(1);
47  }
48 
49  *level = cm->cmsg_level;
50  *type = cm->cmsg_type;
51  *len = cm->cmsg_len - CMSG_LEN (0);
52  *buffer = CMSG_DATA (cm);
53 
54  m_current += CMSG_ALIGN (cm->cmsg_len);
55  m_len -= CMSG_ALIGN (cm->cmsg_len);
56 
57  return 0;
58 }
59 
60 void
62 {
63  m_msg->msg_controllen -= m_len;
64 }
65 
66 
67 } // namespace ns3