SocketAddressMapper.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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-12
  7. *
  8. * */
  9. #include <ls_std/network/socket/SocketAddressMapper.hpp>
  10. #include <ls_std/network/core/ConvertedProtocolFamily.hpp>
  11. #include <ls_std/network/core/ProtocolFamilyMapper.hpp>
  12. #if defined(unix) || defined(__APPLE__)
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #endif
  16. ls::std::network::SocketAddressMapper::SocketAddressMapper() : ls::std::core::Class("SocketAddressMapper")
  17. {}
  18. ls::std::network::ConvertedSocketAddress ls::std::network::SocketAddressMapper::from(const ls::std::network::SocketAddressMapperParameter &_parameter)
  19. {
  20. ls::std::network::ConvertedSocketAddress convertedSocketAddress{};
  21. #if defined(unix) || defined(__APPLE__)
  22. convertedSocketAddress.socketAddressUnix = ls::std::network::SocketAddressMapper::_toSockAddressUnix(_parameter);
  23. convertedSocketAddress.addressLength = sizeof(convertedSocketAddress.socketAddressUnix);
  24. #endif
  25. return convertedSocketAddress;
  26. }
  27. #if defined(unix) || defined(__APPLE__)
  28. ::sockaddr_in ls::std::network::SocketAddressMapper::_toSockAddressUnix(const ls::std::network::SocketAddressMapperParameter &_parameter)
  29. {
  30. ::sockaddr_in socketAddressUnix{};
  31. socketAddressUnix.sin_port = ::htons(_parameter.socketAddress.port);
  32. ls::std::network::ConvertedProtocolFamily convertedProtocolFamily = ls::std::network::ProtocolFamilyMapper::from(_parameter.protocolFamilyType);
  33. socketAddressUnix.sin_family = convertedProtocolFamily.unixDomain;
  34. ::inet_aton(_parameter.socketAddress.ipAddress.c_str(), &socketAddressUnix.sin_addr);
  35. return socketAddressUnix;
  36. }
  37. #endif