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