µ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-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#include <cerrno>
14
15#if defined(OS_USE_OS_APP_CONFIG_H)
16#include <cmsis-plus/os-app-config.h>
17#endif
18
21
23
24// ----------------------------------------------------------------------------
25
26#if defined(__clang__)
27#pragma clang diagnostic ignored "-Wc++98-compat"
28#endif
29
30// ----------------------------------------------------------------------------
31
32namespace os
33{
34 namespace posix
35 {
36 // ========================================================================
37
39 io
40 { impl, type::socket }, //
41 net_stack_ (&ns)
42 {
43#if defined(OS_TRACE_POSIX_IO_SOCKET)
44 trace::printf ("socket::%s()=@%p\n", __func__, this);
45#endif
46 }
47
49 {
50#if defined(OS_TRACE_POSIX_IO_SOCKET)
51 trace::printf ("socket::%s() @%p\n", __func__, this);
52#endif
53
54 net_stack_ = nullptr;
55 }
56
57 // ------------------------------------------------------------------------
58
59 class socket*
60 socket::accept (/* struct */ sockaddr* address, socklen_t* address_len)
61 {
62 errno = 0;
63
64 // Execute the implementation specific code.
65 class socket* new_socket = impl ().do_accept (address, address_len);
66 if (new_socket == nullptr)
67 {
68 return nullptr;
69 }
70 new_socket->alloc_file_descriptor ();
71 return new_socket;
72 }
73
74 int
75 socket::bind (const /* struct */ sockaddr* address, socklen_t address_len)
76 {
77 errno = 0;
78
79 // Execute the implementation specific code.
80 return impl ().do_bind (address, address_len);
81 }
82
83 int
84 socket::connect (const /* struct */ sockaddr* address, socklen_t address_len)
85 {
86 errno = 0;
87
88 // Execute the implementation specific code.
89 return impl ().do_connect (address, address_len);
90 }
91
92 int
93 socket::getpeername (/* struct */ sockaddr* address, socklen_t* address_len)
94 {
95 errno = 0;
96
97 // Execute the implementation specific code.
98 return impl ().do_getpeername (address, address_len);
99 }
100
101 int
102 socket::getsockname (/* struct */ sockaddr* address, socklen_t* address_len)
103 {
104 errno = 0;
105
106 // Execute the implementation specific code.
107 return impl ().do_getsockname (address, address_len);
108 }
109
110 int
111 socket::getsockopt (int level, int option_name, void* option_value,
112 socklen_t* option_len)
113 {
114 errno = 0;
115
116 // Execute the implementation specific code.
117 return impl ().do_getsockopt (level, option_name, option_value,
118 option_len);
119 }
120
121 int
122 socket::listen (int backlog)
123 {
124 errno = 0;
125
126 // Execute the implementation specific code.
127 return impl ().do_listen (backlog);
128 }
129
130 ssize_t
131 socket::recv (void* buffer, size_t length, int flags)
132 {
133 errno = 0;
134
135 // Execute the implementation specific code.
136 return impl ().do_recv (buffer, length, flags);
137 }
138
139 ssize_t
140 socket::recvfrom (void* buffer, size_t length, int flags,
141 /* struct */ sockaddr* address, socklen_t* address_len)
142 {
143 errno = 0;
144
145 // Execute the implementation specific code.
146 return impl ().do_recvfrom (buffer, length, flags, address, address_len);
147 }
148
149 ssize_t
150 socket::recvmsg (/* struct */ msghdr* message, int flags)
151 {
152 errno = 0;
153
154 // Execute the implementation specific code.
155 return impl ().do_recvmsg (message, flags);
156 }
157
158 ssize_t
159 socket::send (const void* buffer, size_t length, int flags)
160 {
161 errno = 0;
162
163 // Execute the implementation specific code.
164 return impl ().do_send (buffer, length, flags);
165 }
166
167 ssize_t
168 socket::sendmsg (const /* struct */ msghdr* message, int flags)
169 {
170 errno = 0;
171
172 // Execute the implementation specific code.
173 return impl ().do_sendmsg (message, flags);
174 }
175
176 ssize_t
177 socket::sendto (const void* message, size_t length, int flags,
178 const /* struct */ sockaddr* dest_addr, socklen_t dest_len)
179 {
180 errno = 0;
181
182 // Execute the implementation specific code.
183 return impl ().do_sendto (message, length, flags, dest_addr, dest_len);
184 }
185
186 int
187 socket::setsockopt (int level, int option_name, const void* option_value,
188 socklen_t option_len)
189 {
190 errno = 0;
191
192 // Execute the implementation specific code.
193 return impl ().do_setsockopt (level, option_name, option_value,
194 option_len);
195 }
196
197 int
199 {
200 errno = 0;
201
202 // Execute the implementation specific code.
203 return impl ().do_shutdown (how);
204 }
205
206 int
208 {
209 errno = 0;
210
211 // Execute the implementation specific code.
212 return impl ().do_sockatmark ();
213 }
214 // ========================================================================
215
217 {
218#if defined(OS_TRACE_POSIX_IO_SOCKET)
219 trace::printf ("socket_impl::%s()=%p\n", __func__, this);
220#endif
221 }
222
224 {
225#if defined(OS_TRACE_POSIX_IO_SOCKET)
226 trace::printf ("socket_impl::%s() @%p\n", __func__, this);
227#endif
228 }
229
230 // ==========================================================================
231 } /* namespace posix */
232} /* namespace os */
233
234// ----------------------------------------------------------------------------
Base I/O class.
Definition io.h:87
io * alloc_file_descriptor(void)
Definition io.cpp:190
Network stack class.
Definition net-stack.h:82
virtual int do_bind(const sockaddr *address, socklen_t address_len)=0
virtual int do_getpeername(sockaddr *address, socklen_t *address_len)=0
virtual int do_getsockopt(int level, int option_name, void *option_value, socklen_t *option_len)=0
virtual socket * do_accept(sockaddr *address, socklen_t *address_len)=0
virtual int do_listen(int backlog)=0
virtual ~socket_impl() override
Definition socket.cpp:223
virtual int do_setsockopt(int level, int option_name, const void *option_value, socklen_t option_len)=0
virtual ssize_t do_recvfrom(void *buffer, size_t length, int flags, sockaddr *address, socklen_t *address_len)=0
virtual ssize_t do_sendmsg(const msghdr *message, int flags)=0
virtual ssize_t do_recv(void *buffer, size_t length, int flags)=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 sockaddr *dest_addr, socklen_t dest_len)=0
virtual int do_getsockname(sockaddr *address, socklen_t *address_len)=0
virtual ssize_t do_recvmsg(msghdr *message, int flags)=0
virtual int do_connect(const sockaddr *address, socklen_t address_len)=0
virtual int do_sockatmark(void)=0
virtual int do_shutdown(int how)=0
Network socket.
virtual int setsockopt(int level, int option_name, const void *option_value, socklen_t option_len)
Definition socket.cpp:187
virtual int sockatmark(void)
Definition socket.cpp:207
virtual int listen(int backlog)
Definition socket.cpp:122
virtual int getsockname(sockaddr *address, socklen_t *address_len)
Definition socket.cpp:102
virtual socket * accept(sockaddr *address, socklen_t *address_len)
Definition socket.cpp:60
virtual int bind(const sockaddr *address, socklen_t address_len)
Definition socket.cpp:75
virtual ssize_t recv(void *buffer, size_t length, int flags)
Definition socket.cpp:131
virtual ssize_t sendto(const void *message, size_t length, int flags, const sockaddr *dest_addr, socklen_t dest_len)
Definition socket.cpp:177
virtual ssize_t send(const void *buffer, size_t length, int flags)
Definition socket.cpp:159
virtual int getpeername(sockaddr *address, socklen_t *address_len)
Definition socket.cpp:93
virtual ssize_t sendmsg(const msghdr *message, int flags)
Definition socket.cpp:168
virtual int connect(const sockaddr *address, socklen_t address_len)
Definition socket.cpp:84
virtual ssize_t recvfrom(void *buffer, size_t length, int flags, sockaddr *address, socklen_t *address_len)
Definition socket.cpp:140
virtual int shutdown(int how)
Definition socket.cpp:198
virtual ~socket() override
Definition socket.cpp:48
virtual ssize_t recvmsg(msghdr *message, int flags)
Definition socket.cpp:150
virtual int getsockopt(int level, int option_name, void *option_value, socklen_t *option_len)
Definition socket.cpp:111
socket_impl & impl(void) const
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
System namespace.
uint32_t socklen_t