Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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