1 /* Copyright (C) 2012 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 #include "Socket/Socket.h"
19 #include "Socket/Endpoint.h"
23 Endpoint::Endpoint() {
26 Endpoint::~Endpoint() {}
28 void Endpoint::reset_address(void) {
29 std::memset(&_remoteHost, 0, sizeof(struct sockaddr_in));
35 int Endpoint::set_address(const char* host, const int port) {
40 char *p_address = address;
42 // Dot-decimal notation
43 int result = std::sscanf(host, "%3u.%3u.%3u.%3u",
44 (unsigned int*)&address[0], (unsigned int*)&address[1],
45 (unsigned int*)&address[2], (unsigned int*)&address[3]);
48 // Resolve address with DNS
49 struct hostent *host_address = lwip_gethostbyname(host);
50 if (host_address == NULL)
51 return -1; //Could not resolve address
52 p_address = (char*)host_address->h_addr_list[0];
54 std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4);
57 _remoteHost.sin_family = AF_INET;
60 _remoteHost.sin_port = htons(port);
65 char* Endpoint::get_address() {
66 if ((_ipAddress[0] == '\0') && (_remoteHost.sin_addr.s_addr != 0))
67 inet_ntoa_r(_remoteHost.sin_addr, _ipAddress, sizeof(_ipAddress));
71 int Endpoint::get_port() {
72 return ntohs(_remoteHost.sin_port);