Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of cc3000_hostdriver_mbedsocket by
Socket/Endpoint.cpp@8:4c42ca37ba33, 2013-09-30 (annotated)
- Committer:
- Kojto
- Date:
- Mon Sep 30 19:29:06 2013 +0000
- Revision:
- 8:4c42ca37ba33
- Parent:
- 5:245ac5b73132
- Child:
- 9:4d12b82bc767
Endpoint::set_address correction with IP returned from gethostbyname; Socket - sock_fd set to -1 when socket is closed (still not handling return type. Will add later)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 5:245ac5b73132 | 1 | /* Copyright (C) 2013 mbed.org, MIT License |
Kojto | 4:15b58c119a0a | 2 | * |
Kojto | 4:15b58c119a0a | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Kojto | 4:15b58c119a0a | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Kojto | 4:15b58c119a0a | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Kojto | 4:15b58c119a0a | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Kojto | 4:15b58c119a0a | 7 | * furnished to do so, subject to the following conditions: |
Kojto | 4:15b58c119a0a | 8 | * |
Kojto | 4:15b58c119a0a | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Kojto | 4:15b58c119a0a | 10 | * substantial portions of the Software. |
Kojto | 4:15b58c119a0a | 11 | * |
Kojto | 4:15b58c119a0a | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Kojto | 4:15b58c119a0a | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Kojto | 4:15b58c119a0a | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Kojto | 4:15b58c119a0a | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Kojto | 4:15b58c119a0a | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Kojto | 4:15b58c119a0a | 17 | */ |
Kojto | 4:15b58c119a0a | 18 | #include "Socket/Socket.h" |
Kojto | 4:15b58c119a0a | 19 | #include "Socket/Endpoint.h" |
Kojto | 4:15b58c119a0a | 20 | #include "Helper/def.h" |
Kojto | 4:15b58c119a0a | 21 | #include <cstring> |
Kojto | 4:15b58c119a0a | 22 | |
Kojto | 4:15b58c119a0a | 23 | #include "cc3000.h" |
Kojto | 4:15b58c119a0a | 24 | |
Kojto | 4:15b58c119a0a | 25 | /* Copied from lwip */ |
Kojto | 4:15b58c119a0a | 26 | static char *inet_ntoa_r(const in_addr addr, char *buf, int buflen) |
Kojto | 4:15b58c119a0a | 27 | { |
Kojto | 4:15b58c119a0a | 28 | uint32_t s_addr; |
Kojto | 4:15b58c119a0a | 29 | char inv[3]; |
Kojto | 4:15b58c119a0a | 30 | char *rp; |
Kojto | 4:15b58c119a0a | 31 | uint8_t *ap; |
Kojto | 4:15b58c119a0a | 32 | uint8_t rem; |
Kojto | 4:15b58c119a0a | 33 | uint8_t n; |
Kojto | 4:15b58c119a0a | 34 | uint8_t i; |
Kojto | 4:15b58c119a0a | 35 | int len = 0; |
Kojto | 4:15b58c119a0a | 36 | |
Kojto | 4:15b58c119a0a | 37 | s_addr = addr.s_addr; |
Kojto | 4:15b58c119a0a | 38 | |
Kojto | 4:15b58c119a0a | 39 | rp = buf; |
Kojto | 4:15b58c119a0a | 40 | ap = (uint8_t *)&s_addr; |
Kojto | 4:15b58c119a0a | 41 | for(n = 0; n < 4; n++) { |
Kojto | 4:15b58c119a0a | 42 | i = 0; |
Kojto | 4:15b58c119a0a | 43 | do { |
Kojto | 4:15b58c119a0a | 44 | rem = *ap % (uint8_t)10; |
Kojto | 4:15b58c119a0a | 45 | *ap /= (uint8_t)10; |
Kojto | 4:15b58c119a0a | 46 | inv[i++] = '0' + rem; |
Kojto | 4:15b58c119a0a | 47 | } while(*ap); |
Kojto | 4:15b58c119a0a | 48 | while(i--) { |
Kojto | 4:15b58c119a0a | 49 | if (len++ >= buflen) { |
Kojto | 4:15b58c119a0a | 50 | return NULL; |
Kojto | 4:15b58c119a0a | 51 | } |
Kojto | 4:15b58c119a0a | 52 | *rp++ = inv[i]; |
Kojto | 4:15b58c119a0a | 53 | } |
Kojto | 4:15b58c119a0a | 54 | if (len++ >= buflen) { |
Kojto | 4:15b58c119a0a | 55 | return NULL; |
Kojto | 4:15b58c119a0a | 56 | } |
Kojto | 4:15b58c119a0a | 57 | *rp++ = '.'; |
Kojto | 4:15b58c119a0a | 58 | ap++; |
Kojto | 4:15b58c119a0a | 59 | } |
Kojto | 4:15b58c119a0a | 60 | *--rp = 0; |
Kojto | 4:15b58c119a0a | 61 | return buf; |
Kojto | 4:15b58c119a0a | 62 | } |
Kojto | 0:615c697c33b0 | 63 | |
Kojto | 4:15b58c119a0a | 64 | Endpoint::Endpoint() { |
Kojto | 4:15b58c119a0a | 65 | _cc3000_module = cc3000::get_instance(); |
Kojto | 4:15b58c119a0a | 66 | if (_cc3000_module == NULL) { |
Kojto | 4:15b58c119a0a | 67 | error("Endpoint constructor error: no cc3000 instance available!\r\n"); |
Kojto | 4:15b58c119a0a | 68 | } |
Kojto | 4:15b58c119a0a | 69 | reset_address(); |
Kojto | 4:15b58c119a0a | 70 | } |
Kojto | 4:15b58c119a0a | 71 | Endpoint::~Endpoint() {} |
Kojto | 4:15b58c119a0a | 72 | |
Kojto | 4:15b58c119a0a | 73 | void Endpoint::reset_address(void) { |
Kojto | 4:15b58c119a0a | 74 | _ipAddress[0] = '\0'; |
Kojto | 5:245ac5b73132 | 75 | std::memset(&_remote_host, 0, sizeof(sockaddr_in)); |
Kojto | 4:15b58c119a0a | 76 | } |
Kojto | 4:15b58c119a0a | 77 | |
Kojto | 4:15b58c119a0a | 78 | int Endpoint::set_address(const char* host, const int port) { |
Kojto | 4:15b58c119a0a | 79 | reset_address(); |
Kojto | 4:15b58c119a0a | 80 | |
Kojto | 4:15b58c119a0a | 81 | char address[5]; |
Kojto | 4:15b58c119a0a | 82 | char *p_address = address; |
Kojto | 4:15b58c119a0a | 83 | |
Kojto | 4:15b58c119a0a | 84 | signed int add[5]; |
Kojto | 4:15b58c119a0a | 85 | |
Kojto | 4:15b58c119a0a | 86 | // Dot-decimal notation |
Kojto | 4:15b58c119a0a | 87 | int result = std::sscanf(host, "%3u.%3u.%3u.%3u", &add[0], &add[1], &add[2], &add[3]); |
Kojto | 4:15b58c119a0a | 88 | for (int i=0;i<4;i++) { |
Kojto | 4:15b58c119a0a | 89 | address[i] = add[i]; |
Kojto | 4:15b58c119a0a | 90 | } |
Kojto | 5:245ac5b73132 | 91 | std::memset(_ipAddress,0,sizeof(_ipAddress)); |
Kojto | 4:15b58c119a0a | 92 | |
Kojto | 4:15b58c119a0a | 93 | if (result != 4) { |
Kojto | 4:15b58c119a0a | 94 | //Resolve DNS address or populate hard-coded IP address |
Kojto | 4:15b58c119a0a | 95 | uint32_t address_integer; |
Kojto | 4:15b58c119a0a | 96 | _cc3000_module->_socket.get_host_by_name((uint8_t *)host, strlen(host) , &address_integer); |
Kojto | 4:15b58c119a0a | 97 | |
Kojto | 8:4c42ca37ba33 | 98 | uint32_t ip = 0; |
Kojto | 8:4c42ca37ba33 | 99 | ip = (ip | (address_integer >> 24)); |
Kojto | 8:4c42ca37ba33 | 100 | ip = (ip | ((address_integer & 0x00FF0000) >> 8)); |
Kojto | 8:4c42ca37ba33 | 101 | ip = (ip | ((address_integer & 0x0000FF00) << 8)); |
Kojto | 8:4c42ca37ba33 | 102 | ip = (ip | ((address_integer & 0x000000FF) << 24)); |
Kojto | 8:4c42ca37ba33 | 103 | _remote_host.sin_addr.s_addr = ip; |
Kojto | 4:15b58c119a0a | 104 | inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); |
Kojto | 4:15b58c119a0a | 105 | } else { |
Kojto | 4:15b58c119a0a | 106 | std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4); |
Kojto | 4:15b58c119a0a | 107 | } |
Kojto | 4:15b58c119a0a | 108 | |
Kojto | 5:245ac5b73132 | 109 | _remote_host.sin_family = AF_INET; |
Kojto | 5:245ac5b73132 | 110 | _remote_host.sin_port = htons(port); |
Kojto | 5:245ac5b73132 | 111 | |
Kojto | 4:15b58c119a0a | 112 | #if (CC3000_DEBUG == 1) |
Kojto | 8:4c42ca37ba33 | 113 | printf("DEBUG: remote host address (string): %s\n",get_address()); |
Kojto | 4:15b58c119a0a | 114 | printf("DEBUG: remote host address from s_addr : %d.%d.%d.%d\n", |
Kojto | 4:15b58c119a0a | 115 | int(_remote_host.sin_addr.s_addr & 0xFF), |
Kojto | 5:245ac5b73132 | 116 | int((_remote_host.sin_addr.s_addr & 0xFF00) >> 8), |
Kojto | 5:245ac5b73132 | 117 | int((_remote_host.sin_addr.s_addr & 0xFF0000) >> 16), |
Kojto | 5:245ac5b73132 | 118 | int((_remote_host.sin_addr.s_addr & 0xFF000000) >> 24)); |
Kojto | 5:245ac5b73132 | 119 | printf("DEBUG: port: %x \n", _remote_host.sin_port); |
Kojto | 4:15b58c119a0a | 120 | #endif |
Kojto | 4:15b58c119a0a | 121 | return 0; |
Kojto | 4:15b58c119a0a | 122 | } |
Kojto | 4:15b58c119a0a | 123 | |
Kojto | 4:15b58c119a0a | 124 | char* Endpoint::get_address() { |
Kojto | 4:15b58c119a0a | 125 | if ((_ipAddress[0] == '\0') && (_remote_host.sin_addr.s_addr != 0)) |
Kojto | 4:15b58c119a0a | 126 | inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); |
Kojto | 4:15b58c119a0a | 127 | return _ipAddress; |
Kojto | 4:15b58c119a0a | 128 | } |
Kojto | 4:15b58c119a0a | 129 | |
Kojto | 4:15b58c119a0a | 130 | |
Kojto | 4:15b58c119a0a | 131 | int Endpoint::get_port() { |
Kojto | 4:15b58c119a0a | 132 | return ntohs(_remote_host.sin_port); |
Kojto | 4:15b58c119a0a | 133 | } |