fdsf

Dependents:   sisk_proj_stat MQTT Hello_FXOS8700Q WireFSHandControl ... more

Committer:
grzemich
Date:
Wed Dec 07 23:47:50 2016 +0000
Revision:
0:d7bd7384a37c
dgd

Who changed what in which revision?

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