conn.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* NOCW */
  2. /* demos/eay/conn.c */
  3. /* A minimal program to connect to a port using the sock4a protocol.
  4. *
  5. * cc -I../../include conn.c -L../.. -lcrypto
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <openssl/err.h>
  10. #include <openssl/bio.h>
  11. /* #include "proxy.h" */
  12. extern int errno;
  13. int main(argc,argv)
  14. int argc;
  15. char *argv[];
  16. {
  17. PROXY *pxy;
  18. char *host;
  19. char buf[1024*10],*p;
  20. BIO *bio;
  21. int i,len,off,ret=1;
  22. if (argc <= 1)
  23. host="localhost:4433";
  24. else
  25. host=argv[1];
  26. /* Lets get nice error messages */
  27. ERR_load_crypto_strings();
  28. /* First, configure proxy settings */
  29. pxy=PROXY_new();
  30. PROXY_add_server(pxy,PROXY_PROTOCOL_SOCKS,"gromit:1080");
  31. bio=BIO_new(BIO_s_socks4a_connect());
  32. BIO_set_conn_hostname(bio,host);
  33. BIO_set_proxies(bio,pxy);
  34. BIO_set_socks_userid(bio,"eay");
  35. BIO_set_nbio(bio,1);
  36. p="GET / HTTP/1.0\r\n\r\n";
  37. len=strlen(p);
  38. off=0;
  39. for (;;)
  40. {
  41. i=BIO_write(bio,&(p[off]),len);
  42. if (i <= 0)
  43. {
  44. if (BIO_should_retry(bio))
  45. {
  46. fprintf(stderr,"write DELAY\n");
  47. sleep(1);
  48. continue;
  49. }
  50. else
  51. {
  52. goto err;
  53. }
  54. }
  55. off+=i;
  56. len-=i;
  57. if (len <= 0) break;
  58. }
  59. for (;;)
  60. {
  61. i=BIO_read(bio,buf,sizeof(buf));
  62. if (i == 0) break;
  63. if (i < 0)
  64. {
  65. if (BIO_should_retry(bio))
  66. {
  67. fprintf(stderr,"read DELAY\n");
  68. sleep(1);
  69. continue;
  70. }
  71. goto err;
  72. }
  73. fwrite(buf,1,i,stdout);
  74. }
  75. ret=1;
  76. if (0)
  77. {
  78. err:
  79. if (ERR_peek_error() == 0) /* system call error */
  80. {
  81. fprintf(stderr,"errno=%d ",errno);
  82. perror("error");
  83. }
  84. else
  85. ERR_print_errors_fp(stderr);
  86. }
  87. BIO_free_all(bio);
  88. if (pxy != NULL) PROXY_free(pxy);
  89. exit(!ret);
  90. return(ret);
  91. }