udpsocket_posix.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <udpsocket.hpp>
  2. #include <cstdint>
  3. constexpr std::uint32_t sha256_k[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  4. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  5. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  6. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  7. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  8. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  9. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  10. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  11. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  12. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  13. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  14. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  15. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  16. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  17. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  18. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
  19. class SHA256{
  20. protected:
  21. typedef unsigned char uint8;
  22. typedef unsigned int uint32;
  23. typedef unsigned long long uint64;
  24. static const unsigned int SHA224_256_BLOCK_SIZE = (512/8);
  25. public:
  26. void init();
  27. void update(const unsigned char *message, unsigned int len);
  28. void final(unsigned char *digest);
  29. static const unsigned int DIGEST_SIZE = ( 256 / 8);
  30. protected:
  31. void transform(const unsigned char *message, unsigned int block_nb);
  32. unsigned int m_tot_len;
  33. unsigned int m_len;
  34. unsigned char m_block[2*SHA224_256_BLOCK_SIZE];
  35. uint32 m_h[8];
  36. };
  37. #define SHA2_SHFR(x, n) (x >> n)
  38. #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
  39. #define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
  40. #define SHA2_CH(x, y, z) ((x & y) ^ (~x & z))
  41. #define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  42. #define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22))
  43. #define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25))
  44. #define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3))
  45. #define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10))
  46. #define SHA2_UNPACK32(x, str) \
  47. { \
  48. *((str) + 3) = (uint8) ((x) ); \
  49. *((str) + 2) = (uint8) ((x) >> 8); \
  50. *((str) + 1) = (uint8) ((x) >> 16); \
  51. *((str) + 0) = (uint8) ((x) >> 24); \
  52. }
  53. #define SHA2_PACK32(str, x) \
  54. { \
  55. *(x) = ((uint32) *((str) + 3) ) \
  56. | ((uint32) *((str) + 2) << 8) \
  57. | ((uint32) *((str) + 1) << 16) \
  58. | ((uint32) *((str) + 0) << 24); \
  59. }
  60. void sha256(std::uint64_t*, const std::string&);
  61. void SHA256::transform(const unsigned char *message, unsigned int block_nb){
  62. uint32 w[64];
  63. uint32 wv[8];
  64. uint32 t1, t2;
  65. const unsigned char *sub_block;
  66. int i;
  67. int j;
  68. for (i = 0; i < (int) block_nb; i++) {
  69. sub_block = message + (i << 6);
  70. for (j = 0; j < 16; j++) {
  71. SHA2_PACK32(&sub_block[j << 2], &w[j]);
  72. }
  73. for (j = 16; j < 64; j++) {
  74. w[j] = SHA256_F4(w[j - 2]) + w[j - 7] + SHA256_F3(w[j - 15]) + w[j - 16];
  75. }
  76. for (j = 0; j < 8; j++) {
  77. wv[j] = m_h[j];
  78. }
  79. for (j = 0; j < 64; j++) {
  80. t1 = wv[7] + SHA256_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6])
  81. + sha256_k[j] + w[j];
  82. t2 = SHA256_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]);
  83. wv[7] = wv[6];
  84. wv[6] = wv[5];
  85. wv[5] = wv[4];
  86. wv[4] = wv[3] + t1;
  87. wv[3] = wv[2];
  88. wv[2] = wv[1];
  89. wv[1] = wv[0];
  90. wv[0] = t1 + t2;
  91. }
  92. for (j = 0; j < 8; j++) {
  93. m_h[j] += wv[j];
  94. }
  95. }
  96. }
  97. void SHA256::init(){
  98. m_h[0] = 0x6a09e667;
  99. m_h[1] = 0xbb67ae85;
  100. m_h[2] = 0x3c6ef372;
  101. m_h[3] = 0xa54ff53a;
  102. m_h[4] = 0x510e527f;
  103. m_h[5] = 0x9b05688c;
  104. m_h[6] = 0x1f83d9ab;
  105. m_h[7] = 0x5be0cd19;
  106. m_len = 0;
  107. m_tot_len = 0;
  108. }
  109. void SHA256::update(const unsigned char *message, unsigned int len){
  110. unsigned int block_nb;
  111. unsigned int new_len, rem_len, tmp_len;
  112. const unsigned char *shifted_message;
  113. tmp_len = SHA224_256_BLOCK_SIZE - m_len;
  114. rem_len = len < tmp_len ? len : tmp_len;
  115. memcpy(&m_block[m_len], message, rem_len);
  116. if (m_len + len < SHA224_256_BLOCK_SIZE) {
  117. m_len += len;
  118. return;
  119. }
  120. new_len = len - rem_len;
  121. block_nb = new_len / SHA224_256_BLOCK_SIZE;
  122. shifted_message = message + rem_len;
  123. transform(m_block, 1);
  124. transform(shifted_message, block_nb);
  125. rem_len = new_len % SHA224_256_BLOCK_SIZE;
  126. memcpy(m_block, &shifted_message[block_nb << 6], rem_len);
  127. m_len = rem_len;
  128. m_tot_len += (block_nb + 1) << 6;
  129. }
  130. void SHA256::final(unsigned char *digest){
  131. unsigned int block_nb;
  132. unsigned int pm_len;
  133. unsigned int len_b;
  134. int i;
  135. block_nb = (1 + ((SHA224_256_BLOCK_SIZE - 9)
  136. < (m_len % SHA224_256_BLOCK_SIZE)));
  137. len_b = (m_tot_len + m_len) << 3;
  138. pm_len = block_nb << 6;
  139. memset(m_block + m_len, 0, pm_len - m_len);
  140. m_block[m_len] = 0x80;
  141. SHA2_UNPACK32(len_b, m_block + pm_len - 4);
  142. transform(m_block, block_nb);
  143. for (i = 0 ; i < 8; i++) {
  144. SHA2_UNPACK32(m_h[i], &digest[i << 2]);
  145. }
  146. }
  147. void sha256(uint64_t* dest, const std::string& input){
  148. unsigned char digest[SHA256::DIGEST_SIZE];
  149. memset(digest,0,SHA256::DIGEST_SIZE);
  150. SHA256 ctx = SHA256();
  151. ctx.init();
  152. ctx.update((const unsigned char*)input.c_str(), input.length());
  153. ctx.final(digest);
  154. unsigned char* cdest = (unsigned char*) dest;
  155. memcpy(dest, digest, SHA256::DIGEST_SIZE);
  156. }
  157. packet::packet(const std::string& content, const std::string& dest, int port) : m_port(port){
  158. hostent *server_host;
  159. errno = 0;
  160. server_host = gethostbyname(dest.c_str());
  161. if(errno){
  162. throw std::logic_error("Could not resolve hostname " + dest + ": " + strerror(errno));
  163. }
  164. /* configure the server address */
  165. struct sockaddr_in server_addr;
  166. server_addr.sin_family = AF_INET; // IPv4
  167. memcpy(&server_addr.sin_addr, server_host->h_addr,
  168. sizeof(struct in_addr)); server_addr.sin_port = htons(port);
  169. this->content = content;
  170. sha256(checksum, this->content);
  171. }
  172. void packet::setContent(const std::string& content){
  173. this->content = content;
  174. sha256(checksum, this->content);
  175. }
  176. udpsocket::udpsocket(int port) : m_port(port){
  177. if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  178. throw std::logic_error(std::string("Socket creation failed: ") + strerror(errno));
  179. }
  180. std::memset((char*)&addr, 0, sizeof(addr));
  181. addr.sin_family = AF_INET;
  182. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  183. addr.sin_port = htons(this->port());
  184. if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  185. throw std::logic_error(std::string("Socket binding failed: ") + strerror(errno));
  186. }
  187. }
  188. udpsocket::udpsocket(udpsocket&& o) : addr(o.addr),s(o.s),m_port(o.m_port) {
  189. o.s = 0;
  190. o.m_port = 0;
  191. }
  192. udpsocket& udpsocket::operator=(udpsocket&& o){
  193. addr = o.addr;
  194. s = o.s;
  195. m_port = o.m_port;
  196. o.s = 0;
  197. o.m_port = 0;
  198. return *this;
  199. }
  200. void udpsocket::write(const std::string& msg, const std::string& dest, int port)const{
  201. std::vector<char> _msg(msg.begin(), msg.end());
  202. write(_msg, dest, port);
  203. }
  204. void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int port)const{
  205. hostent *server_host;
  206. errno = 0;
  207. server_host = gethostbyname(dest.c_str());
  208. if(errno){
  209. throw std::logic_error("Could not resolve hostname " + dest + ": " + strerror(errno));
  210. }
  211. /* configure the server address */
  212. struct sockaddr_in server_addr;
  213. server_addr.sin_family = AF_INET; // IPv4
  214. memcpy(&server_addr.sin_addr, server_host->h_addr,
  215. sizeof(struct in_addr)); server_addr.sin_port = htons(port);
  216. /* send a message */
  217. if(sendto(s, msg.data(), msg.size(), 0,(const sockaddr*)&server_addr, sizeof(server_addr)) < 0){
  218. throw std::logic_error(std::string("Could not send packet: ") + strerror(errno));
  219. }
  220. }
  221. std::vector<char> udpsocket::receive()const{
  222. std::vector<char> ret(1024);
  223. int l;
  224. if ((l = read(s, ret.data(), 1024)) <= 0) {
  225. throw std::logic_error(std::string("Could not receive packet: ") + strerror(errno));
  226. }
  227. ret.resize(l);
  228. return ret;
  229. }
  230. void udpsocket::close(){
  231. shutdown(s, 2);
  232. }
  233. int udpsocket::port()const {
  234. return this->m_port;
  235. }