input 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* echoclient.c */
  2. #include "openssl/ssl.h"
  3. #include "../test.h"
  4. int main(int argc, char** argv)
  5. {
  6. SOCKET_T sockfd = 0;
  7. FILE* fin = stdin;
  8. FILE* fout = stdout;
  9. int inCreated = 0;
  10. int outCreated = 0;
  11. char send[1024];
  12. char reply[1024];
  13. SSL_METHOD* method = 0;
  14. SSL_CTX* ctx = 0;
  15. SSL* ssl = 0;
  16. #ifdef _WIN32
  17. WSADATA wsd;
  18. WSAStartup(0x0002, &wsd);
  19. #endif
  20. if (argc >= 2) {
  21. fin = fopen(argv[1], "r");
  22. inCreated = 1;
  23. }
  24. if (argc >= 3) {
  25. fout = fopen(argv[2], "w");
  26. outCreated = 1;
  27. }
  28. if (!fin) err_sys("can't open input file");
  29. if (!fout) err_sys("can't open output file");
  30. tcp_connect(&sockfd);
  31. method = SSLv3_client_method();
  32. ctx = SSL_CTX_new(method);
  33. if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
  34. err_sys("can't load ca file");
  35. ssl = SSL_new(ctx);
  36. SSL_set_fd(ssl, sockfd);
  37. if (SSL_connect(ssl) != SSL_SUCCESS) err_sys("SSL_connect failed");
  38. while (fgets(send, sizeof(send), fin)) {
  39. int sendSz = strlen(send) + 1;
  40. if (SSL_write(ssl, send, sendSz) != sendSz)
  41. err_sys("SSL_write failed");
  42. if (strncmp(send, "quit", 4) == 0) {
  43. fputs("sending server shutdown command: quit!\n", fout);
  44. break;
  45. }
  46. if (SSL_read(ssl, reply, sizeof(reply)) > 0)
  47. fputs(reply, fout);
  48. }
  49. SSL_shutdown(ssl);
  50. SSL_free(ssl);
  51. SSL_CTX_free(ctx);
  52. fflush(fout);
  53. if (inCreated) fclose(fin);
  54. if (outCreated) fclose(fout);
  55. #ifdef _WIN32
  56. closesocket(sockfd);
  57. #else
  58. close(sockfd);
  59. #endif
  60. return 0;
  61. }