µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
socket.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2015 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <cerrno>
31
33
34// ----------------------------------------------------------------------------
35
36#if defined(__clang__)
37#pragma clang diagnostic ignored "-Wc++98-compat"
38#endif
39
40// ----------------------------------------------------------------------------
41
42namespace os
43{
44 namespace posix
45 {
46 // ========================================================================
47
49 io
50 { impl, type::socket }, //
51 net_stack_ (&ns)
52 {
53#if defined(OS_TRACE_POSIX_IO_SOCKET)
54 trace::printf ("socket::%s()=@%p\n", __func__, this);
55#endif
56 }
57
59 {
60#if defined(OS_TRACE_POSIX_IO_SOCKET)
61 trace::printf ("socket::%s() @%p\n", __func__, this);
62#endif
63
64 net_stack_ = nullptr;
65 }
66
67 // ------------------------------------------------------------------------
68
69 class socket*
70 socket::accept (struct sockaddr* address, socklen_t* address_len)
71 {
72 errno = 0;
73
74 // Execute the implementation specific code.
75 class socket* new_socket = impl ().do_accept (address, address_len);
76 if (new_socket == nullptr)
77 {
78 return nullptr;
79 }
80 new_socket->alloc_file_descriptor ();
81 return new_socket;
82 }
83
84 int
85 socket::bind (const struct sockaddr* address, socklen_t address_len)
86 {
87 errno = 0;
88
89 // Execute the implementation specific code.
90 return impl ().do_bind (address, address_len);
91 }
92
93 int
94 socket::connect (const struct sockaddr* address, socklen_t address_len)
95 {
96 errno = 0;
97
98 // Execute the implementation specific code.
99 return impl ().do_connect (address, address_len);
100 }
101
102 int
103 socket::getpeername (struct sockaddr* address, socklen_t* address_len)
104 {
105 errno = 0;
106
107 // Execute the implementation specific code.
108 return impl ().do_getpeername (address, address_len);
109 }
110
111 int
112 socket::getsockname (struct sockaddr* address, socklen_t* address_len)
113 {
114 errno = 0;
115
116 // Execute the implementation specific code.
117 return impl ().do_getsockname (address, address_len);
118 }
119
120 int
121 socket::getsockopt (int level, int option_name, void* option_value,
122 socklen_t* option_len)
123 {
124 errno = 0;
125
126 // Execute the implementation specific code.
127 return impl ().do_getsockopt (level, option_name, option_value,
128 option_len);
129 }
130
131 int
132 socket::listen (int backlog)
133 {
134 errno = 0;
135
136 // Execute the implementation specific code.
137 return impl ().do_listen (backlog);
138 }
139
140 ssize_t
141 socket::recv (void* buffer, size_t length, int flags)
142 {
143 errno = 0;
144
145 // Execute the implementation specific code.
146 return impl ().do_recv (buffer, length, flags);
147 }
148
149 ssize_t
150 socket::recvfrom (void* buffer, size_t length, int flags,
151 struct sockaddr* address, socklen_t* address_len)
152 {
153 errno = 0;
154
155 // Execute the implementation specific code.
156 return impl ().do_recvfrom (buffer, length, flags, address, address_len);
157 }
158
159 ssize_t
160 socket::recvmsg (struct msghdr* message, int flags)
161 {
162 errno = 0;
163
164 // Execute the implementation specific code.
165 return impl ().do_recvmsg (message, flags);
166 }
167
168 ssize_t
169 socket::send (const void* buffer, size_t length, int flags)
170 {
171 errno = 0;
172
173 // Execute the implementation specific code.
174 return impl ().do_send (buffer, length, flags);
175 }
176
177 ssize_t
178 socket::sendmsg (const struct msghdr* message, int flags)
179 {
180 errno = 0;
181
182 // Execute the implementation specific code.
183 return impl ().do_sendmsg (message, flags);
184 }
185
186 ssize_t
187 socket::sendto (const void* message, size_t length, int flags,
188 const struct sockaddr* dest_addr, socklen_t dest_len)
189 {
190 errno = 0;
191
192 // Execute the implementation specific code.
193 return impl ().do_sendto (message, length, flags, dest_addr, dest_len);
194 }
195
196 int
197 socket::setsockopt (int level, int option_name, const void* option_value,
198 socklen_t option_len)
199 {
200 errno = 0;
201
202 // Execute the implementation specific code.
203 return impl ().do_setsockopt (level, option_name, option_value,
204 option_len);
205 }
206
207 int
209 {
210 errno = 0;
211
212 // Execute the implementation specific code.
213 return impl ().do_shutdown (how);
214 }
215
216 int
218 {
219 errno = 0;
220
221 // Execute the implementation specific code.
222 return impl ().do_sockatmark ();
223 }
224 // ========================================================================
225
227 {
228#if defined(OS_TRACE_POSIX_IO_SOCKET)
229 trace::printf ("socket_impl::%s()=%p\n", __func__, this);
230#endif
231 }
232
234 {
235#if defined(OS_TRACE_POSIX_IO_SOCKET)
236 trace::printf ("socket_impl::%s() @%p\n", __func__, this);
237#endif
238 }
239
240 // ==========================================================================
241 } /* namespace posix */
242} /* namespace os */
243
244// ----------------------------------------------------------------------------
Base I/O class.
Definition io.h:98
io * alloc_file_descriptor(void)
Definition io.cpp:201
Network stack class.
Definition net-stack.h:92
virtual int do_getsockopt(int level, int option_name, void *option_value, socklen_t *option_len)=0
virtual int do_listen(int backlog)=0
virtual ~socket_impl() override
Definition socket.cpp:233
virtual int do_setsockopt(int level, int option_name, const void *option_value, socklen_t option_len)=0
virtual ssize_t do_recv(void *buffer, size_t length, int flags)=0
virtual int do_getpeername(struct sockaddr *address, socklen_t *address_len)=0
virtual ssize_t do_send(const void *buffer, size_t length, int flags)=0
virtual ssize_t do_sendto(const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)=0
virtual ssize_t do_recvmsg(struct msghdr *message, int flags)=0
virtual int do_bind(const struct sockaddr *address, socklen_t address_len)=0
virtual class socket * do_accept(struct sockaddr *address, socklen_t *address_len)=0
virtual ssize_t do_sendmsg(const struct msghdr *message, int flags)=0
virtual ssize_t do_recvfrom(void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len)=0
virtual int do_sockatmark(void)=0
virtual int do_connect(const struct sockaddr *address, socklen_t address_len)=0
virtual int do_shutdown(int how)=0
virtual int do_getsockname(struct sockaddr *address, socklen_t *address_len)=0
Network socket.
virtual int setsockopt(int level, int option_name, const void *option_value, socklen_t option_len)
Definition socket.cpp:197
virtual int sockatmark(void)
Definition socket.cpp:217
virtual int getsockname(struct sockaddr *address, socklen_t *address_len)
Definition socket.cpp:112
virtual int listen(int backlog)
Definition socket.cpp:132
virtual ssize_t recvmsg(struct msghdr *message, int flags)
Definition socket.cpp:160
virtual ssize_t sendmsg(const struct msghdr *message, int flags)
Definition socket.cpp:178
virtual int getpeername(struct sockaddr *address, socklen_t *address_len)
Definition socket.cpp:103
virtual ssize_t recv(void *buffer, size_t length, int flags)
Definition socket.cpp:141
virtual ssize_t send(const void *buffer, size_t length, int flags)
Definition socket.cpp:169
virtual ssize_t recvfrom(void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len)
Definition socket.cpp:150
virtual int shutdown(int how)
Definition socket.cpp:208
virtual int connect(const struct sockaddr *address, socklen_t address_len)
Definition socket.cpp:94
virtual ~socket() override
Definition socket.cpp:58
virtual int bind(const struct sockaddr *address, socklen_t address_len)
Definition socket.cpp:85
virtual int getsockopt(int level, int option_name, void *option_value, socklen_t *option_len)
Definition socket.cpp:121
virtual class socket * accept(struct sockaddr *address, socklen_t *address_len)
Definition socket.cpp:70
socket_impl & impl(void) const
virtual ssize_t sendto(const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
Definition socket.cpp:187
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
System namespace.
uint32_t socklen_t