Onenet
Dependents: K64F_eCompass_OneNET_JW
Socket/Endpoint.cpp@0:b2805b6888dc, 2016-06-20 (annotated)
- Committer:
- robert_jw
- Date:
- Mon Jun 20 01:40:20 2016 +0000
- Revision:
- 0:b2805b6888dc
ADS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robert_jw | 0:b2805b6888dc | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
robert_jw | 0:b2805b6888dc | 2 | * |
robert_jw | 0:b2805b6888dc | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
robert_jw | 0:b2805b6888dc | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
robert_jw | 0:b2805b6888dc | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
robert_jw | 0:b2805b6888dc | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
robert_jw | 0:b2805b6888dc | 7 | * furnished to do so, subject to the following conditions: |
robert_jw | 0:b2805b6888dc | 8 | * |
robert_jw | 0:b2805b6888dc | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
robert_jw | 0:b2805b6888dc | 10 | * substantial portions of the Software. |
robert_jw | 0:b2805b6888dc | 11 | * |
robert_jw | 0:b2805b6888dc | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
robert_jw | 0:b2805b6888dc | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
robert_jw | 0:b2805b6888dc | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
robert_jw | 0:b2805b6888dc | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
robert_jw | 0:b2805b6888dc | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
robert_jw | 0:b2805b6888dc | 17 | */ |
robert_jw | 0:b2805b6888dc | 18 | #include "Socket/Socket.h" |
robert_jw | 0:b2805b6888dc | 19 | #include "Socket/Endpoint.h" |
robert_jw | 0:b2805b6888dc | 20 | #include <cstring> |
robert_jw | 0:b2805b6888dc | 21 | #include <cstdio> |
robert_jw | 0:b2805b6888dc | 22 | |
robert_jw | 0:b2805b6888dc | 23 | Endpoint::Endpoint() { |
robert_jw | 0:b2805b6888dc | 24 | reset_address(); |
robert_jw | 0:b2805b6888dc | 25 | } |
robert_jw | 0:b2805b6888dc | 26 | Endpoint::~Endpoint() {} |
robert_jw | 0:b2805b6888dc | 27 | |
robert_jw | 0:b2805b6888dc | 28 | void Endpoint::reset_address(void) { |
robert_jw | 0:b2805b6888dc | 29 | std::memset(&_remoteHost, 0, sizeof(struct sockaddr_in)); |
robert_jw | 0:b2805b6888dc | 30 | _ipAddress[0] = '\0'; |
robert_jw | 0:b2805b6888dc | 31 | } |
robert_jw | 0:b2805b6888dc | 32 | |
robert_jw | 0:b2805b6888dc | 33 | #include "stdio.h" |
robert_jw | 0:b2805b6888dc | 34 | |
robert_jw | 0:b2805b6888dc | 35 | int Endpoint::set_address(const char* host, const int port) { |
robert_jw | 0:b2805b6888dc | 36 | reset_address(); |
robert_jw | 0:b2805b6888dc | 37 | |
robert_jw | 0:b2805b6888dc | 38 | // IP Address |
robert_jw | 0:b2805b6888dc | 39 | char address[5]; |
robert_jw | 0:b2805b6888dc | 40 | char *p_address = address; |
robert_jw | 0:b2805b6888dc | 41 | |
robert_jw | 0:b2805b6888dc | 42 | // Dot-decimal notation |
robert_jw | 0:b2805b6888dc | 43 | int result = std::sscanf(host, "%3u.%3u.%3u.%3u", |
robert_jw | 0:b2805b6888dc | 44 | (unsigned int*)&address[0], (unsigned int*)&address[1], |
robert_jw | 0:b2805b6888dc | 45 | (unsigned int*)&address[2], (unsigned int*)&address[3]); |
robert_jw | 0:b2805b6888dc | 46 | |
robert_jw | 0:b2805b6888dc | 47 | if (result != 4) { |
robert_jw | 0:b2805b6888dc | 48 | // Resolve address with DNS |
robert_jw | 0:b2805b6888dc | 49 | struct hostent *host_address = lwip_gethostbyname(host); |
robert_jw | 0:b2805b6888dc | 50 | if (host_address == NULL) |
robert_jw | 0:b2805b6888dc | 51 | return -1; //Could not resolve address |
robert_jw | 0:b2805b6888dc | 52 | p_address = (char*)host_address->h_addr_list[0]; |
robert_jw | 0:b2805b6888dc | 53 | } |
robert_jw | 0:b2805b6888dc | 54 | std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4); |
robert_jw | 0:b2805b6888dc | 55 | |
robert_jw | 0:b2805b6888dc | 56 | // Address family |
robert_jw | 0:b2805b6888dc | 57 | _remoteHost.sin_family = AF_INET; |
robert_jw | 0:b2805b6888dc | 58 | |
robert_jw | 0:b2805b6888dc | 59 | // Set port |
robert_jw | 0:b2805b6888dc | 60 | _remoteHost.sin_port = htons(port); |
robert_jw | 0:b2805b6888dc | 61 | |
robert_jw | 0:b2805b6888dc | 62 | return 0; |
robert_jw | 0:b2805b6888dc | 63 | } |
robert_jw | 0:b2805b6888dc | 64 | |
robert_jw | 0:b2805b6888dc | 65 | char* Endpoint::get_address() { |
robert_jw | 0:b2805b6888dc | 66 | if ((_ipAddress[0] == '\0') && (_remoteHost.sin_addr.s_addr != 0)) |
robert_jw | 0:b2805b6888dc | 67 | inet_ntoa_r(_remoteHost.sin_addr, _ipAddress, sizeof(_ipAddress)); |
robert_jw | 0:b2805b6888dc | 68 | return _ipAddress; |
robert_jw | 0:b2805b6888dc | 69 | } |
robert_jw | 0:b2805b6888dc | 70 | |
robert_jw | 0:b2805b6888dc | 71 | int Endpoint::get_port() { |
robert_jw | 0:b2805b6888dc | 72 | return ntohs(_remoteHost.sin_port); |
robert_jw | 0:b2805b6888dc | 73 | } |