Host library for controlling a WiConnect enabled Wi-Fi module.
Dependents: wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more
Endpoint.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 #include "api/types/Socket/Socket.h" 00019 #include "api/types/Socket/Endpoint.h" 00020 #include <cstring> 00021 #include <cstdio> 00022 00023 00024 00025 /*************************************************************************************************/ 00026 Endpoint::Endpoint() 00027 { 00028 reset_address(); 00029 } 00030 00031 /*************************************************************************************************/ 00032 Endpoint::~Endpoint() {} 00033 00034 /*************************************************************************************************/ 00035 void Endpoint::reset_address(void) 00036 { 00037 std::memset(&_remoteHost, 0, sizeof(struct sockaddr_in)); 00038 _ipAddress[0] = '\0'; 00039 } 00040 00041 /*************************************************************************************************/ 00042 int Endpoint::set_address(const char* host, const int port) 00043 { 00044 reset_address(); 00045 00046 // IP Address 00047 char address[5]; 00048 char *p_address = address; 00049 00050 // Dot-decimal notation 00051 int result = std::sscanf(host, "%3u.%3u.%3u.%3u", 00052 (unsigned int*)&address[0], (unsigned int*)&address[1], 00053 (unsigned int*)&address[2], (unsigned int*)&address[3]); 00054 00055 if (result != 4) 00056 { 00057 // Resolve address with DNS 00058 struct hostent *host_address = gethostbyname(host); 00059 if (host_address == NULL) 00060 return -1; //Could not resolve address 00061 p_address = (char*)host_address->h_addr_list[0]; 00062 } 00063 std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4); 00064 00065 // Address family 00066 _remoteHost.sin_family = AF_INET; 00067 00068 // Set port 00069 _remoteHost.sin_port = htons(port); 00070 00071 return 0; 00072 } 00073 00074 /*************************************************************************************************/ 00075 char* Endpoint::get_address() 00076 { 00077 if ((_ipAddress[0] == '\0') && (_remoteHost.sin_addr.s_addr != 0)) 00078 inet_ntoa_r(_remoteHost.sin_addr, _ipAddress, sizeof(_ipAddress)); 00079 return _ipAddress; 00080 } 00081 00082 /*************************************************************************************************/ 00083 int Endpoint::get_port() 00084 { 00085 return ntohs(_remoteHost.sin_port); 00086 }
Generated on Tue Jul 12 2022 17:35:58 by 1.7.2