input 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if (SSL_set_fd(ssl, sockfd) != SSL_SUCCESS)
  37. err_sys("can't set ssl fd");
  38. if (SSL_connect(ssl) != SSL_SUCCESS) err_sys("SSL_connect failed");
  39. while (fgets(send, sizeof(send), fin)) {
  40. int sendSz = strlen(send) + 1;
  41. if (SSL_write(ssl, send, sendSz) != sendSz)
  42. err_sys("SSL_write failed");
  43. if (strncmp(send, "quit", 4) == 0) {
  44. fputs("sending server shutdown command: quit!\n", fout);
  45. break;
  46. }
  47. if (SSL_read(ssl, reply, sizeof(reply)) > 0)
  48. fputs(reply, fout);
  49. }
  50. SSL_shutdown(ssl);
  51. SSL_free(ssl);
  52. SSL_CTX_free(ctx);
  53. fflush(fout);
  54. if (inCreated) fclose(fin);
  55. if (outCreated) fclose(fout);
  56. #ifdef _WIN32
  57. closesocket(sockfd);
  58. #else
  59. close(sockfd);
  60. #endif
  61. return 0;
  62. }