]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/net/lwip/Socket/Endpoint.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / net / lwip / Socket / Endpoint.cpp
1 /* Copyright (C) 2012 mbed.org, MIT License
2  *
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:
8  *
9  * The above copyright notice and this permission notice shall be included in all copies or
10  * substantial portions of the Software.
11  *
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.
17  */
18 #include "Socket/Socket.h"
19 #include "Socket/Endpoint.h"
20 #include <cstring>
21 #include <cstdio>
22
23 Endpoint::Endpoint()  {
24     reset_address();
25 }
26 Endpoint::~Endpoint() {}
27
28 void Endpoint::reset_address(void) {
29     std::memset(&_remoteHost, 0, sizeof(struct sockaddr_in));
30     _ipAddress[0] = '\0';
31 }
32
33 #include "stdio.h"
34
35 int Endpoint::set_address(const char* host, const int port) {
36     reset_address();
37     
38     // IP Address
39     char address[5];
40     char *p_address = address;
41     
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]);
46     
47     if (result != 4) {
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];
53     }
54     std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4);
55     
56     // Address family
57     _remoteHost.sin_family = AF_INET;
58     
59     // Set port
60     _remoteHost.sin_port = htons(port);
61     
62     return 0;
63 }
64
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));
68     return _ipAddress;
69 }
70
71 int   Endpoint::get_port() {
72     return ntohs(_remoteHost.sin_port);
73 }