SocketAddressMapper.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-11-18
  6. * Changed: 2022-12-26
  7. *
  8. * */
  9. #include <ls_std/os/specification.hpp>
  10. #include <ls_std/network/socket/SocketAddressMapper.hpp>
  11. #include <ls_std/network/core/ConvertedProtocolFamily.hpp>
  12. #include <ls_std/network/core/ProtocolFamilyMapper.hpp>
  13. #if LS_STD_UNIX_PLATFORM
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16. #endif
  17. ls::std::network::SocketAddressMapper::SocketAddressMapper() : ls::std::core::Class("SocketAddressMapper")
  18. {}
  19. ls::std::network::ConvertedSocketAddress ls::std::network::SocketAddressMapper::from(const ls::std::network::SocketAddressMapperParameter &_parameter)
  20. {
  21. ls::std::network::ConvertedSocketAddress convertedSocketAddress{};
  22. #if LS_STD_UNIX_PLATFORM
  23. convertedSocketAddress.socketAddressUnix = ls::std::network::SocketAddressMapper::_toSockAddressUnix(_parameter);
  24. convertedSocketAddress.addressLength = sizeof(convertedSocketAddress.socketAddressUnix);
  25. #endif
  26. return convertedSocketAddress;
  27. }
  28. #if LS_STD_UNIX_PLATFORM
  29. ::sockaddr_in ls::std::network::SocketAddressMapper::_toSockAddressUnix(const ls::std::network::SocketAddressMapperParameter &_parameter)
  30. {
  31. ::sockaddr_in socketAddressUnix{};
  32. socketAddressUnix.sin_port = ::htons(_parameter.socketAddress.port);
  33. ls::std::network::ConvertedProtocolFamily convertedProtocolFamily = ls::std::network::ProtocolFamilyMapper::from(_parameter.protocolFamilyType);
  34. socketAddressUnix.sin_family = convertedProtocolFamily.unixDomain;
  35. if (!_parameter.socketAddress.ipAddress.empty())
  36. {
  37. ::inet_aton(_parameter.socketAddress.ipAddress.c_str(), &socketAddressUnix.sin_addr);
  38. }
  39. else
  40. {
  41. socketAddressUnix.sin_addr.s_addr = INADDR_ANY;
  42. }
  43. return socketAddressUnix;
  44. }
  45. #endif