udpsocket_posix.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <udpsocket.hpp>
  2. #include <cstdint>
  3. #include <hash.hpp>
  4. #include <algorithm>
  5. packet::packet(const std::vector<char>& content, const std::string& dest, int port) : m_port(port){
  6. hostent *server_host;
  7. errno = 0;
  8. server_host = gethostbyname(dest.c_str());
  9. if(errno != 0){
  10. throw std::logic_error("Could not resolve hostname " + dest + ": " + strerror(errno));
  11. }
  12. /* configure the server address */
  13. addr.sin_family = AF_INET; // IPv4
  14. memcpy(&addr.sin_addr, server_host->h_addr,
  15. sizeof(struct in_addr));
  16. addr.sin_port = htons(port);
  17. this->content = content;
  18. hash_checksum(checksum, this->content);
  19. }
  20. void packet::setContent(const std::vector<char>& _content){
  21. this->content = _content;
  22. hash_checksum(checksum, this->content);
  23. }
  24. packet::packet(const std::string& content, const std::string& dest, int port) : packet(std::vector<char>(content.begin(), content.end()),dest,port){}
  25. udpsocket::udpsocket(int port) : m_port(port){
  26. if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  27. throw std::logic_error(std::string("Socket creation failed: ") + strerror(errno));
  28. }
  29. std::memset((char*)&addr, 0, sizeof(addr));
  30. addr.sin_family = AF_INET;
  31. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  32. addr.sin_port = htons(this->port());
  33. if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  34. throw std::logic_error(std::string("Socket binding failed: ") + strerror(errno));
  35. }
  36. }
  37. udpsocket::udpsocket(udpsocket&& o) : addr(o.addr),s(o.s),m_port(o.m_port) {
  38. o.s = 0;
  39. o.m_port = 0;
  40. }
  41. udpsocket& udpsocket::operator=(udpsocket&& o){
  42. addr = o.addr;
  43. s = o.s;
  44. m_port = o.m_port;
  45. o.s = 0;
  46. o.m_port = 0;
  47. return *this;
  48. }
  49. void udpsocket::write(const std::string& msg, const std::string& dest, int port)const{
  50. std::vector<char> _msg(msg.begin(), msg.end());
  51. write(_msg, dest, port);
  52. }
  53. void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int port)const{
  54. hostent *server_host;
  55. errno = 0;
  56. server_host = gethostbyname(dest.c_str());
  57. if(errno){
  58. throw std::logic_error("Could not resolve hostname " + dest + ": " + strerror(errno));
  59. }
  60. /* configure the server address */
  61. struct sockaddr_in server_addr;
  62. server_addr.sin_family = AF_INET; // IPv4
  63. memcpy(&server_addr.sin_addr, server_host->h_addr,
  64. sizeof(struct in_addr)); server_addr.sin_port = htons(port);
  65. /* send a message */
  66. if(sendto(s, msg.data(), msg.size(), 0,(const sockaddr*)&server_addr, sizeof(server_addr)) < 0){
  67. throw std::logic_error(std::string("Could not send packet: ") + strerror(errno));
  68. }
  69. }
  70. void udpsocket::write(const packet& pack)const{
  71. if(sendto(s, pack.content.data(), pack.content.size(), 0,(const sockaddr*)&pack.addr, sizeof(pack.addr)) < 0){
  72. throw std::logic_error(std::string("Could not send packet: ") + strerror(errno));
  73. }
  74. }
  75. std::vector<char> udpsocket::receive()const{
  76. std::vector<char> ret(1024);
  77. int l;
  78. if ((l = read(s, ret.data(), 1024)) <= 0) {
  79. throw std::logic_error(std::string("Could not receive packet: ") + strerror(errno));
  80. }
  81. ret.resize(l);
  82. return ret;
  83. }
  84. void udpsocket::close(){
  85. shutdown(s, 2);
  86. }
  87. int udpsocket::port()const {
  88. return this->m_port;
  89. }
  90. int main(){
  91. udpsocket sock(18000);
  92. packet pack("Hhallo", "127.0.0.1", 15666);
  93. while(true)
  94. sock.write(pack);
  95. return 0;
  96. }