Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Endpoint.cpp 2.5KB

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