Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <netinet/in.h>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <openssl/bn.h>
  6. using namespace std;
  7. int main() {
  8. // Set up
  9. int result;
  10. int opt_val = 1;
  11. int port = 12345;
  12.  
  13. int listen_sock;
  14. int peer_sock;
  15.  
  16. struct sockaddr_in address;
  17.  
  18. listen_sock = socket(AF_INET, SOCK_STREAM, 0);
  19.  
  20. memset(&address, 0, sizeof(address));
  21.  
  22. address.sin_family = AF_INET;
  23. address.sin_port = htons(port);
  24. address.sin_addr.s_addr = INADDR_ANY;
  25.  
  26. setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val));
  27.  
  28. result = bind(listen_sock, (struct sockaddr*)&address, sizeof(address));
  29. if (result != 0) {
  30. cerr << "bind() failed." << endl;
  31. exit(result);
  32. }
  33.  
  34. result = listen(listen_sock, 5);
  35. if (result != 0) {
  36. cerr << "listen() failed." << endl;
  37. exit(result);
  38. }
  39.  
  40. size_t size = sizeof(address);
  41. peer_sock = accept(listen_sock, (struct sockaddr*)&address, (socklen_t *)&size);
  42. if (peer_sock < 0) {
  43. cerr << "accept() failed." << endl;
  44. exit(result);
  45. }
  46.  
  47. ////////////////////////////////
  48. // Prepare BIGNUM
  49. BIGNUM* bn = BN_new();
  50. BN_rand(bn, 1024, 0, 0);
  51. size = BN_num_bytes((const BIGNUM*)bn);
  52.  
  53. // Send using fixed array
  54. unsigned char fixed[size];
  55. BN_bn2bin((const BIGNUM*)bn, fixed);
  56.  
  57. // Send using dynamic array
  58. unsigned char *dynamic;
  59. dynamic = new unsigned char[size];
  60. BN_bn2bin((const BIGNUM*)bn, dynamic);
  61.  
  62. // First let's compare
  63. if ( memcmp(fixed, dynamic, size) != 0) {
  64. cout << "Fixed and dynamic do not equal" << endl;
  65. exit(-1);
  66. }
  67.  
  68. // Then let's send two arrays
  69. send(peer_sock, &size, sizeof(size), MSG_MORE);
  70. send(peer_sock, fixed, size, MSG_MORE);
  71. send(peer_sock, dynamic, size, MSG_MORE);
  72.  
  73. }
  74.  
  75. #include <netinet/in.h>
  76. #include <iostream>
  77. #include <netdb.h>
  78. #include <cstdlib>
  79. #include <arpa/inet.h>
  80. #include <openssl/bn.h>
  81. #include <cstring>
  82.  
  83. using namespace std;
  84.  
  85. int main() {
  86.  
  87. struct addrinfo* res;
  88. struct sockaddr_in address;
  89. int peer_port = 12345;
  90. int self_sock;
  91. string peer_ip = "127.0.0.1";
  92. address.sin_family = AF_INET;
  93. address.sin_port = htons(peer_port);
  94.  
  95. int result = getaddrinfo((const char*)peer_ip.c_str(), NULL, NULL, &res);
  96. if (result != 0) {
  97. std::cerr << "Peer hostname invalid." << std::endl;
  98. exit(-1);
  99. }
  100. freeaddrinfo(res);
  101. inet_pton(AF_INET, (const char*)peer_ip.c_str(), &(address.sin_addr));
  102. self_sock = socket(AF_INET, SOCK_STREAM, 0);
  103. if (::connect(self_sock, (struct sockaddr*)&address, sizeof(address)) != 0) {
  104. std::cerr << "connect() failed." << std::endl;
  105. exit(-1);
  106. }
  107.  
  108.  
  109. int size;
  110. recv(self_sock, &size, sizeof(size), 0);
  111. // Receive fixed array
  112. unsigned char fixed[size];
  113. unsigned char* dynamic = new unsigned char[size];
  114. recv(self_sock, fixed, size, 0);
  115. recv(self_sock, dynamic, size, 0);
  116.  
  117. if (memcmp(fixed, dynamic, size) != 0) {
  118. cerr << "client: fixed and dynamic are different!" << endl;
  119. exit(-1);
  120. }
  121.  
  122. BIGNUM* bn_fixed = BN_new();
  123. BIGNUM* bn_dynamic = BN_new();
  124. BN_bin2bn((const unsigned char*)fixed, size, bn_fixed);
  125. BN_bin2bn((const unsigned char*)dynamic, size, bn_dynamic);
  126. if (BN_cmp((const BIGNUM*)bn_fixed, (const BIGNUM*)bn_dynamic) != 0) {
  127. cerr << "bn_fixed and bn_dynamic are different!" << endl;
  128. exit(-1);
  129. }
  130.  
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement