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