BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /* Socket
borlanic 0:fbdae7e6d805 2 * Copyright (c) 2015 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 5 * you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 6 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 7 *
borlanic 0:fbdae7e6d805 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 9 *
borlanic 0:fbdae7e6d805 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 13 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 14 * limitations under the License.
borlanic 0:fbdae7e6d805 15 */
borlanic 0:fbdae7e6d805 16
borlanic 0:fbdae7e6d805 17 #include "NetworkStack.h"
borlanic 0:fbdae7e6d805 18 #include "nsapi_dns.h"
borlanic 0:fbdae7e6d805 19 #include "mbed.h"
borlanic 0:fbdae7e6d805 20 #include "stddef.h"
borlanic 0:fbdae7e6d805 21 #include <new>
borlanic 0:fbdae7e6d805 22
borlanic 0:fbdae7e6d805 23
borlanic 0:fbdae7e6d805 24 // Default NetworkStack operations
borlanic 0:fbdae7e6d805 25 nsapi_error_t NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
borlanic 0:fbdae7e6d805 26 {
borlanic 0:fbdae7e6d805 27 // check for simple ip addresses
borlanic 0:fbdae7e6d805 28 if (address->set_ip_address(name)) {
borlanic 0:fbdae7e6d805 29 if (version != NSAPI_UNSPEC && address->get_ip_version() != version) {
borlanic 0:fbdae7e6d805 30 return NSAPI_ERROR_DNS_FAILURE;
borlanic 0:fbdae7e6d805 31 }
borlanic 0:fbdae7e6d805 32
borlanic 0:fbdae7e6d805 33 return NSAPI_ERROR_OK;
borlanic 0:fbdae7e6d805 34 }
borlanic 0:fbdae7e6d805 35
borlanic 0:fbdae7e6d805 36 // if the version is unspecified, try to guess the version from the
borlanic 0:fbdae7e6d805 37 // ip address of the underlying stack
borlanic 0:fbdae7e6d805 38 if (version == NSAPI_UNSPEC) {
borlanic 0:fbdae7e6d805 39 SocketAddress testaddress;
borlanic 0:fbdae7e6d805 40 if (testaddress.set_ip_address(this->get_ip_address())) {
borlanic 0:fbdae7e6d805 41 version = testaddress.get_ip_version();
borlanic 0:fbdae7e6d805 42 }
borlanic 0:fbdae7e6d805 43 }
borlanic 0:fbdae7e6d805 44
borlanic 0:fbdae7e6d805 45 return nsapi_dns_query(this, name, address, version);
borlanic 0:fbdae7e6d805 46 }
borlanic 0:fbdae7e6d805 47
borlanic 0:fbdae7e6d805 48 nsapi_error_t NetworkStack::add_dns_server(const SocketAddress &address)
borlanic 0:fbdae7e6d805 49 {
borlanic 0:fbdae7e6d805 50 return nsapi_dns_add_server(address);
borlanic 0:fbdae7e6d805 51 }
borlanic 0:fbdae7e6d805 52
borlanic 0:fbdae7e6d805 53 nsapi_error_t NetworkStack::setstackopt(int level, int optname, const void *optval, unsigned optlen)
borlanic 0:fbdae7e6d805 54 {
borlanic 0:fbdae7e6d805 55 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 56 }
borlanic 0:fbdae7e6d805 57
borlanic 0:fbdae7e6d805 58 nsapi_error_t NetworkStack::getstackopt(int level, int optname, void *optval, unsigned *optlen)
borlanic 0:fbdae7e6d805 59 {
borlanic 0:fbdae7e6d805 60 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 61 }
borlanic 0:fbdae7e6d805 62
borlanic 0:fbdae7e6d805 63 nsapi_error_t NetworkStack::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen)
borlanic 0:fbdae7e6d805 64 {
borlanic 0:fbdae7e6d805 65 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 66 }
borlanic 0:fbdae7e6d805 67
borlanic 0:fbdae7e6d805 68 nsapi_error_t NetworkStack::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen)
borlanic 0:fbdae7e6d805 69 {
borlanic 0:fbdae7e6d805 70 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 71 }
borlanic 0:fbdae7e6d805 72
borlanic 0:fbdae7e6d805 73
borlanic 0:fbdae7e6d805 74 // NetworkStackWrapper class for encapsulating the raw nsapi_stack structure
borlanic 0:fbdae7e6d805 75 class NetworkStackWrapper : public NetworkStack
borlanic 0:fbdae7e6d805 76 {
borlanic 0:fbdae7e6d805 77 private:
borlanic 0:fbdae7e6d805 78 inline nsapi_stack_t *_stack()
borlanic 0:fbdae7e6d805 79 {
borlanic 0:fbdae7e6d805 80 return reinterpret_cast<nsapi_stack_t *>(
borlanic 0:fbdae7e6d805 81 reinterpret_cast<uint8_t *>(this)
borlanic 0:fbdae7e6d805 82 - offsetof(nsapi_stack_t, _stack_buffer));
borlanic 0:fbdae7e6d805 83 }
borlanic 0:fbdae7e6d805 84
borlanic 0:fbdae7e6d805 85 inline const nsapi_stack_api_t *_stack_api()
borlanic 0:fbdae7e6d805 86 {
borlanic 0:fbdae7e6d805 87 return _stack()->stack_api;
borlanic 0:fbdae7e6d805 88 }
borlanic 0:fbdae7e6d805 89
borlanic 0:fbdae7e6d805 90 public:
borlanic 0:fbdae7e6d805 91 virtual const char *get_ip_address()
borlanic 0:fbdae7e6d805 92 {
borlanic 0:fbdae7e6d805 93 if (!_stack_api()->get_ip_address) {
borlanic 0:fbdae7e6d805 94 return 0;
borlanic 0:fbdae7e6d805 95 }
borlanic 0:fbdae7e6d805 96
borlanic 0:fbdae7e6d805 97 static uint8_t buffer[sizeof(SocketAddress)];
borlanic 0:fbdae7e6d805 98 SocketAddress *address = new (buffer) SocketAddress(_stack_api()->get_ip_address(_stack()));
borlanic 0:fbdae7e6d805 99 return address->get_ip_address();
borlanic 0:fbdae7e6d805 100 }
borlanic 0:fbdae7e6d805 101
borlanic 0:fbdae7e6d805 102 virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
borlanic 0:fbdae7e6d805 103 {
borlanic 0:fbdae7e6d805 104 if (!_stack_api()->gethostbyname) {
borlanic 0:fbdae7e6d805 105 return NetworkStack::gethostbyname(name, address, version);
borlanic 0:fbdae7e6d805 106 }
borlanic 0:fbdae7e6d805 107
borlanic 0:fbdae7e6d805 108 nsapi_addr_t addr = {NSAPI_UNSPEC, 0};
borlanic 0:fbdae7e6d805 109 nsapi_error_t err = _stack_api()->gethostbyname(_stack(), name, &addr, version);
borlanic 0:fbdae7e6d805 110 address->set_addr(addr);
borlanic 0:fbdae7e6d805 111 return err;
borlanic 0:fbdae7e6d805 112 }
borlanic 0:fbdae7e6d805 113
borlanic 0:fbdae7e6d805 114 virtual nsapi_error_t add_dns_server(const SocketAddress &address)
borlanic 0:fbdae7e6d805 115 {
borlanic 0:fbdae7e6d805 116 if (!_stack_api()->add_dns_server) {
borlanic 0:fbdae7e6d805 117 return NetworkStack::add_dns_server(address);
borlanic 0:fbdae7e6d805 118 }
borlanic 0:fbdae7e6d805 119
borlanic 0:fbdae7e6d805 120 return _stack_api()->add_dns_server(_stack(), address.get_addr());
borlanic 0:fbdae7e6d805 121 }
borlanic 0:fbdae7e6d805 122
borlanic 0:fbdae7e6d805 123 virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen)
borlanic 0:fbdae7e6d805 124 {
borlanic 0:fbdae7e6d805 125 if (!_stack_api()->setstackopt) {
borlanic 0:fbdae7e6d805 126 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 127 }
borlanic 0:fbdae7e6d805 128
borlanic 0:fbdae7e6d805 129 return _stack_api()->setstackopt(_stack(), level, optname, optval, optlen);
borlanic 0:fbdae7e6d805 130 }
borlanic 0:fbdae7e6d805 131
borlanic 0:fbdae7e6d805 132 virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen)
borlanic 0:fbdae7e6d805 133 {
borlanic 0:fbdae7e6d805 134 if (!_stack_api()->getstackopt) {
borlanic 0:fbdae7e6d805 135 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 136 }
borlanic 0:fbdae7e6d805 137
borlanic 0:fbdae7e6d805 138 return _stack_api()->getstackopt(_stack(), level, optname, optval, optlen);
borlanic 0:fbdae7e6d805 139 }
borlanic 0:fbdae7e6d805 140
borlanic 0:fbdae7e6d805 141 protected:
borlanic 0:fbdae7e6d805 142 virtual nsapi_error_t socket_open(nsapi_socket_t *socket, nsapi_protocol_t proto)
borlanic 0:fbdae7e6d805 143 {
borlanic 0:fbdae7e6d805 144 if (!_stack_api()->socket_open) {
borlanic 0:fbdae7e6d805 145 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 146 }
borlanic 0:fbdae7e6d805 147
borlanic 0:fbdae7e6d805 148 return _stack_api()->socket_open(_stack(), socket, proto);
borlanic 0:fbdae7e6d805 149 }
borlanic 0:fbdae7e6d805 150
borlanic 0:fbdae7e6d805 151 virtual nsapi_error_t socket_close(nsapi_socket_t socket)
borlanic 0:fbdae7e6d805 152 {
borlanic 0:fbdae7e6d805 153 if (!_stack_api()->socket_close) {
borlanic 0:fbdae7e6d805 154 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 155 }
borlanic 0:fbdae7e6d805 156
borlanic 0:fbdae7e6d805 157 return _stack_api()->socket_close(_stack(), socket);
borlanic 0:fbdae7e6d805 158 }
borlanic 0:fbdae7e6d805 159
borlanic 0:fbdae7e6d805 160 virtual nsapi_error_t socket_bind(nsapi_socket_t socket, const SocketAddress &address)
borlanic 0:fbdae7e6d805 161 {
borlanic 0:fbdae7e6d805 162 if (!_stack_api()->socket_bind) {
borlanic 0:fbdae7e6d805 163 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 164 }
borlanic 0:fbdae7e6d805 165
borlanic 0:fbdae7e6d805 166 return _stack_api()->socket_bind(_stack(), socket, address.get_addr(), address.get_port());
borlanic 0:fbdae7e6d805 167 }
borlanic 0:fbdae7e6d805 168
borlanic 0:fbdae7e6d805 169 virtual nsapi_error_t socket_listen(nsapi_socket_t socket, int backlog)
borlanic 0:fbdae7e6d805 170 {
borlanic 0:fbdae7e6d805 171 if (!_stack_api()->socket_listen) {
borlanic 0:fbdae7e6d805 172 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 173 }
borlanic 0:fbdae7e6d805 174
borlanic 0:fbdae7e6d805 175 return _stack_api()->socket_listen(_stack(), socket, backlog);
borlanic 0:fbdae7e6d805 176 }
borlanic 0:fbdae7e6d805 177
borlanic 0:fbdae7e6d805 178 virtual nsapi_error_t socket_connect(nsapi_socket_t socket, const SocketAddress &address)
borlanic 0:fbdae7e6d805 179 {
borlanic 0:fbdae7e6d805 180 if (!_stack_api()->socket_connect) {
borlanic 0:fbdae7e6d805 181 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 182 }
borlanic 0:fbdae7e6d805 183
borlanic 0:fbdae7e6d805 184 return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port());
borlanic 0:fbdae7e6d805 185 }
borlanic 0:fbdae7e6d805 186
borlanic 0:fbdae7e6d805 187 virtual nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *socket, SocketAddress *address)
borlanic 0:fbdae7e6d805 188 {
borlanic 0:fbdae7e6d805 189 if (!_stack_api()->socket_accept) {
borlanic 0:fbdae7e6d805 190 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 191 }
borlanic 0:fbdae7e6d805 192
borlanic 0:fbdae7e6d805 193 nsapi_addr_t addr = {NSAPI_IPv4, 0};
borlanic 0:fbdae7e6d805 194 uint16_t port = 0;
borlanic 0:fbdae7e6d805 195
borlanic 0:fbdae7e6d805 196 nsapi_error_t err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port);
borlanic 0:fbdae7e6d805 197
borlanic 0:fbdae7e6d805 198 if (address) {
borlanic 0:fbdae7e6d805 199 address->set_addr(addr);
borlanic 0:fbdae7e6d805 200 address->set_port(port);
borlanic 0:fbdae7e6d805 201 }
borlanic 0:fbdae7e6d805 202
borlanic 0:fbdae7e6d805 203 return err;
borlanic 0:fbdae7e6d805 204 }
borlanic 0:fbdae7e6d805 205
borlanic 0:fbdae7e6d805 206 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t socket, const void *data, nsapi_size_t size)
borlanic 0:fbdae7e6d805 207 {
borlanic 0:fbdae7e6d805 208 if (!_stack_api()->socket_send) {
borlanic 0:fbdae7e6d805 209 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 210 }
borlanic 0:fbdae7e6d805 211
borlanic 0:fbdae7e6d805 212 return _stack_api()->socket_send(_stack(), socket, data, size);
borlanic 0:fbdae7e6d805 213 }
borlanic 0:fbdae7e6d805 214
borlanic 0:fbdae7e6d805 215 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t socket, void *data, nsapi_size_t size)
borlanic 0:fbdae7e6d805 216 {
borlanic 0:fbdae7e6d805 217 if (!_stack_api()->socket_recv) {
borlanic 0:fbdae7e6d805 218 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 219 }
borlanic 0:fbdae7e6d805 220
borlanic 0:fbdae7e6d805 221 return _stack_api()->socket_recv(_stack(), socket, data, size);
borlanic 0:fbdae7e6d805 222 }
borlanic 0:fbdae7e6d805 223
borlanic 0:fbdae7e6d805 224 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t socket, const SocketAddress &address, const void *data, nsapi_size_t size)
borlanic 0:fbdae7e6d805 225 {
borlanic 0:fbdae7e6d805 226 if (!_stack_api()->socket_sendto) {
borlanic 0:fbdae7e6d805 227 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 228 }
borlanic 0:fbdae7e6d805 229
borlanic 0:fbdae7e6d805 230 return _stack_api()->socket_sendto(_stack(), socket, address.get_addr(), address.get_port(), data, size);
borlanic 0:fbdae7e6d805 231 }
borlanic 0:fbdae7e6d805 232
borlanic 0:fbdae7e6d805 233 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t socket, SocketAddress *address, void *data, nsapi_size_t size)
borlanic 0:fbdae7e6d805 234 {
borlanic 0:fbdae7e6d805 235 if (!_stack_api()->socket_recvfrom) {
borlanic 0:fbdae7e6d805 236 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 237 }
borlanic 0:fbdae7e6d805 238
borlanic 0:fbdae7e6d805 239 nsapi_addr_t addr = {NSAPI_IPv4, 0};
borlanic 0:fbdae7e6d805 240 uint16_t port = 0;
borlanic 0:fbdae7e6d805 241
borlanic 0:fbdae7e6d805 242 nsapi_size_or_error_t err = _stack_api()->socket_recvfrom(_stack(), socket, &addr, &port, data, size);
borlanic 0:fbdae7e6d805 243
borlanic 0:fbdae7e6d805 244 if (address) {
borlanic 0:fbdae7e6d805 245 address->set_addr(addr);
borlanic 0:fbdae7e6d805 246 address->set_port(port);
borlanic 0:fbdae7e6d805 247 }
borlanic 0:fbdae7e6d805 248
borlanic 0:fbdae7e6d805 249 return err;
borlanic 0:fbdae7e6d805 250 }
borlanic 0:fbdae7e6d805 251
borlanic 0:fbdae7e6d805 252 virtual void socket_attach(nsapi_socket_t socket, void (*callback)(void *), void *data)
borlanic 0:fbdae7e6d805 253 {
borlanic 0:fbdae7e6d805 254 if (!_stack_api()->socket_attach) {
borlanic 0:fbdae7e6d805 255 return;
borlanic 0:fbdae7e6d805 256 }
borlanic 0:fbdae7e6d805 257
borlanic 0:fbdae7e6d805 258 return _stack_api()->socket_attach(_stack(), socket, callback, data);
borlanic 0:fbdae7e6d805 259 }
borlanic 0:fbdae7e6d805 260
borlanic 0:fbdae7e6d805 261 virtual nsapi_error_t setsockopt(nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen)
borlanic 0:fbdae7e6d805 262 {
borlanic 0:fbdae7e6d805 263 if (!_stack_api()->setsockopt) {
borlanic 0:fbdae7e6d805 264 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 265 }
borlanic 0:fbdae7e6d805 266
borlanic 0:fbdae7e6d805 267 return _stack_api()->setsockopt(_stack(), socket, level, optname, optval, optlen);
borlanic 0:fbdae7e6d805 268 }
borlanic 0:fbdae7e6d805 269
borlanic 0:fbdae7e6d805 270 virtual nsapi_error_t getsockopt(nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen)
borlanic 0:fbdae7e6d805 271 {
borlanic 0:fbdae7e6d805 272 if (!_stack_api()->getsockopt) {
borlanic 0:fbdae7e6d805 273 return NSAPI_ERROR_UNSUPPORTED;
borlanic 0:fbdae7e6d805 274 }
borlanic 0:fbdae7e6d805 275
borlanic 0:fbdae7e6d805 276 return _stack_api()->getsockopt(_stack(), socket, level, optname, optval, optlen);
borlanic 0:fbdae7e6d805 277 }
borlanic 0:fbdae7e6d805 278 };
borlanic 0:fbdae7e6d805 279
borlanic 0:fbdae7e6d805 280
borlanic 0:fbdae7e6d805 281 // Conversion function for network stacks
borlanic 0:fbdae7e6d805 282 NetworkStack *nsapi_create_stack(nsapi_stack_t *stack)
borlanic 0:fbdae7e6d805 283 {
borlanic 0:fbdae7e6d805 284 MBED_STATIC_ASSERT(sizeof stack->_stack_buffer >= sizeof(NetworkStackWrapper),
borlanic 0:fbdae7e6d805 285 "The nsapi_stack_t stack buffer must fit a NetworkStackWrapper");
borlanic 0:fbdae7e6d805 286 return new (stack->_stack_buffer) NetworkStackWrapper;
borlanic 0:fbdae7e6d805 287 }
borlanic 0:fbdae7e6d805 288
borlanic 0:fbdae7e6d805 289 NetworkStack *nsapi_create_stack(NetworkStack *stack)
borlanic 0:fbdae7e6d805 290 {
borlanic 0:fbdae7e6d805 291 return stack;
borlanic 0:fbdae7e6d805 292 }
borlanic 0:fbdae7e6d805 293