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