SNode.C
Loading...
Searching...
No Matches
net::un::dgram::Socket Class Reference

#include <Socket.h>

Inheritance diagram for net::un::dgram::Socket:
Collaboration diagram for net::un::dgram::Socket:

Public Member Functions

 Socket ()
 
 ~Socket () override
 
ssize_t sendFd (SocketAddress &&destAddress, int sendfd)
 
ssize_t sendFd (SocketAddress &destAddress, int sendfd)
 
ssize_t recvFd (int *recvfd)
 
- Public Member Functions inherited from net::un::phy::PhysicalSocket< net::phy::dgram::PeerSocket >
 PhysicalSocket (int type, int protocol)
 
 PhysicalSocket (PhysicalSocket &&physicalSocket) noexcept
 
PhysicalSocketoperator= (PhysicalSocket &&physicalSocket) noexcept
 
 ~PhysicalSocket () override
 
int bind (SocketAddress &bindAddress)
 
- Public Member Functions inherited from net::phy::PhysicalSocket< net::un::SocketAddress >
 PhysicalSocket ()=delete
 
 PhysicalSocket (PhysicalSocket &)=delete
 
 PhysicalSocket (const PhysicalSocket &)=delete
 
 PhysicalSocket (int fd, const SocketAddress &bindAddress)
 
PhysicalSocketoperator= (PhysicalSocket &)=delete
 
PhysicalSocketoperator= (const PhysicalSocket &)=delete
 
int open (const std::map< int, const PhysicalSocketOption > &socketOptions, Flags flags)
 
int bind (SocketAddress &bindAddress)
 
bool isValid () const
 
int getSockError (int &cErrno) const
 
int getSockName (typename SocketAddress::SockAddr &localSockAddr, typename SocketAddress::SockLen &localSockAddrLen)
 
int getPeerName (typename SocketAddress::SockAddr &remoteSockAddr, typename SocketAddress::SockLen &remoteSockAddrLen)
 
SocketAddress getBindAddress () const
 
- Public Member Functions inherited from core::Descriptor
 Descriptor ()=delete
 
 Descriptor (const Descriptor &d)=delete
 
Descriptoroperator= (int fd)
 
Descriptoroperator= (const Descriptor &descriptor)=delete
 
Descriptoroperator= (Descriptor &&descriptor) noexcept
 
int getFd () const
 

Private Types

using Super = net::un::phy::PhysicalSocket<net::phy::dgram::PeerSocket>
 

Additional Inherited Members

- Public Types inherited from net::un::phy::PhysicalSocket< net::phy::dgram::PeerSocket >
using Super
 
- Public Types inherited from net::phy::dgram::PeerSocket< net::un::SocketAddress >
using SocketAddress
 
- Public Types inherited from net::phy::PhysicalSocket< net::un::SocketAddress >
enum  Flags
 
- Protected Types inherited from net::phy::dgram::PeerSocket< net::un::SocketAddress >
using Super
 
- Protected Types inherited from net::phy::PhysicalSocket< net::un::SocketAddress >
using SocketAddress
 
- Protected Member Functions inherited from net::phy::PhysicalSocket< net::un::SocketAddress >
 PhysicalSocket (int domain, int type, int protocol)
 
 PhysicalSocket (PhysicalSocket &&physicalSocket) noexcept=default
 
PhysicalSocketoperator= (PhysicalSocket &&) noexcept=default
 
 ~PhysicalSocket () override
 
- Protected Member Functions inherited from core::Descriptor
 Descriptor (int fd)
 
 Descriptor (Descriptor &&descriptor) noexcept
 
virtual ~Descriptor ()
 
- Protected Attributes inherited from net::phy::PhysicalSocket< net::un::SocketAddress >
SocketAddress bindAddress
 
int domain
 
int type
 
int protocol
 

Detailed Description

Definition at line 34 of file Socket.h.

Member Typedef Documentation

◆ Super

Constructor & Destructor Documentation

◆ Socket()

net::un::dgram::Socket::Socket ( )

Definition at line 34 of file Socket.cpp.

35 : Super(SOCK_DGRAM, 0) {
36 }
net::un::phy::PhysicalSocket< net::phy::dgram::PeerSocket > Super
Definition Socket.h:36

References Socket().

Referenced by Socket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~Socket()

net::un::dgram::Socket::~Socket ( )
override

Definition at line 38 of file Socket.cpp.

38 {
39 }

Member Function Documentation

◆ recvFd()

ssize_t net::un::dgram::Socket::recvFd ( int * recvfd)

Definition at line 75 of file Socket.cpp.

75 {
76 union {
77 struct cmsghdr cm;
78 char control[CMSG_SPACE(sizeof(int))] = {};
79 } control_un;
80
81 msghdr msg{};
82 msg.msg_control = control_un.control;
83 msg.msg_controllen = sizeof(control_un.control);
84
85 msg.msg_name = nullptr;
86 msg.msg_namelen = 0;
87
88 iovec iov[1];
89 msg.msg_iov = iov;
90 msg.msg_iovlen = 1;
91
92 char ptr = 0;
93 msg.msg_iov[0].iov_base = &ptr;
94 msg.msg_iov[0].iov_len = 1;
95
96 ssize_t n = 0;
97
98 if ((n = recvmsg(core::Descriptor::getFd(), &msg, 0)) > 0) {
99 cmsghdr* cmptr = nullptr;
100
101 if ((cmptr = CMSG_FIRSTHDR(&msg)) != nullptr && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
102 if (cmptr->cmsg_level != SOL_SOCKET || cmptr->cmsg_type != SCM_RIGHTS) {
103 errno = EBADE;
104 *recvfd = -1;
105 n = -1;
106 } else {
107 *recvfd = *reinterpret_cast<int*>(CMSG_DATA(cmptr));
108 }
109 } else {
110 errno = ENOMSG;
111 *recvfd = -1;
112 n = -1;
113 }
114 }
115
116 return n;
117 }
int getFd() const

◆ sendFd() [1/2]

ssize_t net::un::dgram::Socket::sendFd ( SocketAddress && destAddress,
int sendfd )

Definition at line 41 of file Socket.cpp.

41 {
42 return sendFd(destAddress, sendfd);
43 }
ssize_t sendFd(SocketAddress &&destAddress, int sendfd)
Definition Socket.cpp:41

References sendFd().

Here is the call graph for this function:

◆ sendFd() [2/2]

ssize_t net::un::dgram::Socket::sendFd ( SocketAddress & destAddress,
int sendfd )

Definition at line 45 of file Socket.cpp.

45 {
46 union {
47 struct cmsghdr cm;
48 char control[CMSG_SPACE(sizeof(int))] = {};
49 } control_un;
50
51 msghdr msg{};
52 msg.msg_name = const_cast<sockaddr*>(&destAddress.getSockAddr());
53 msg.msg_namelen = destAddress.getSockAddrLen();
54
55 msg.msg_control = control_un.control;
56 msg.msg_controllen = sizeof(control_un.control);
57
58 iovec iov[1];
59 msg.msg_iov = iov;
60 msg.msg_iovlen = 1;
61
62 char ptr = 0;
63 msg.msg_iov[0].iov_base = &ptr;
64 msg.msg_iov[0].iov_len = 1;
65
66 cmsghdr* cmptr = CMSG_FIRSTHDR(&msg);
67 cmptr->cmsg_level = SOL_SOCKET;
68 cmptr->cmsg_type = SCM_RIGHTS;
69 *reinterpret_cast<int*>(CMSG_DATA(cmptr)) = sendfd;
70 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
71
72 return sendmsg(core::Descriptor::getFd(), &msg, 0);
73 }

Referenced by sendFd().

Here is the caller graph for this function:

The documentation for this class was generated from the following files: