geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

298 lines
7.7 KiB

  1. /*
  2. * Sample showing how to do SFTP non-blocking transfers.
  3. *
  4. * The sample code has default values for host name, user name, password
  5. * and path to copy, but you can specify them on the command line like:
  6. *
  7. * "sftp_nonblock 192.168.0.1 user password /tmp/secrets"
  8. */
  9. #include "libssh2_config.h"
  10. #include <libssh2.h>
  11. #include <libssh2_sftp.h>
  12. #ifdef HAVE_WINSOCK2_H
  13. # include <winsock2.h>
  14. #endif
  15. #ifdef HAVE_SYS_SOCKET_H
  16. # include <sys/socket.h>
  17. #endif
  18. #ifdef HAVE_NETINET_IN_H
  19. # include <netinet/in.h>
  20. #endif
  21. #ifdef HAVE_SYS_SELECT_H
  22. # include <sys/select.h>
  23. #endif
  24. # ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #ifdef HAVE_ARPA_INET_H
  28. # include <arpa/inet.h>
  29. #endif
  30. #ifdef HAVE_SYS_TIME_H
  31. # include <sys/time.h>
  32. #endif
  33. #include <sys/types.h>
  34. #include <fcntl.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #ifdef HAVE_GETTIMEOFDAY
  39. /* diff in ms */
  40. static long tvdiff(struct timeval newer, struct timeval older)
  41. {
  42. return (newer.tv_sec-older.tv_sec)*1000+
  43. (newer.tv_usec-older.tv_usec)/1000;
  44. }
  45. #endif
  46. static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
  47. {
  48. struct timeval timeout;
  49. int rc;
  50. fd_set fd;
  51. fd_set *writefd = NULL;
  52. fd_set *readfd = NULL;
  53. int dir;
  54. timeout.tv_sec = 10;
  55. timeout.tv_usec = 0;
  56. FD_ZERO(&fd);
  57. FD_SET(socket_fd, &fd);
  58. /* now make sure we wait in the correct direction */
  59. dir = libssh2_session_block_directions(session);
  60. if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
  61. readfd = &fd;
  62. if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
  63. writefd = &fd;
  64. rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
  65. return rc;
  66. }
  67. int main(int argc, char *argv[])
  68. {
  69. unsigned long hostaddr;
  70. int sock, i, auth_pw = 1;
  71. struct sockaddr_in sin;
  72. const char *fingerprint;
  73. LIBSSH2_SESSION *session;
  74. const char *username = "username";
  75. const char *password = "password";
  76. const char *sftppath = "/tmp/TEST";
  77. #ifdef HAVE_GETTIMEOFDAY
  78. struct timeval start;
  79. struct timeval end;
  80. long time_ms;
  81. #endif
  82. int rc;
  83. int total = 0;
  84. int spin = 0;
  85. LIBSSH2_SFTP *sftp_session;
  86. LIBSSH2_SFTP_HANDLE *sftp_handle;
  87. #ifdef WIN32
  88. WSADATA wsadata;
  89. int err;
  90. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  91. if(err != 0) {
  92. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  93. return 1;
  94. }
  95. #endif
  96. if(argc > 1) {
  97. hostaddr = inet_addr(argv[1]);
  98. }
  99. else {
  100. hostaddr = htonl(0x7F000001);
  101. }
  102. if(argc > 2) {
  103. username = argv[2];
  104. }
  105. if(argc > 3) {
  106. password = argv[3];
  107. }
  108. if(argc > 4) {
  109. sftppath = argv[4];
  110. }
  111. rc = libssh2_init(0);
  112. if(rc != 0) {
  113. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  114. return 1;
  115. }
  116. /*
  117. * The application code is responsible for creating the socket
  118. * and establishing the connection
  119. */
  120. sock = socket(AF_INET, SOCK_STREAM, 0);
  121. sin.sin_family = AF_INET;
  122. sin.sin_port = htons(22);
  123. sin.sin_addr.s_addr = hostaddr;
  124. if(connect(sock, (struct sockaddr*)(&sin),
  125. sizeof(struct sockaddr_in)) != 0) {
  126. fprintf(stderr, "failed to connect!\n");
  127. return -1;
  128. }
  129. /* Create a session instance */
  130. session = libssh2_session_init();
  131. if(!session)
  132. return -1;
  133. /* Since we have set non-blocking, tell libssh2 we are non-blocking */
  134. libssh2_session_set_blocking(session, 0);
  135. #ifdef HAVE_GETTIMEOFDAY
  136. gettimeofday(&start, NULL);
  137. #endif
  138. /* ... start it up. This will trade welcome banners, exchange keys,
  139. * and setup crypto, compression, and MAC layers
  140. */
  141. while((rc = libssh2_session_handshake(session, sock)) ==
  142. LIBSSH2_ERROR_EAGAIN);
  143. if(rc) {
  144. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  145. return -1;
  146. }
  147. /* At this point we havn't yet authenticated. The first thing to do
  148. * is check the hostkey's fingerprint against our known hosts Your app
  149. * may have it hard coded, may go to a file, may present it to the
  150. * user, that's your call
  151. */
  152. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  153. fprintf(stderr, "Fingerprint: ");
  154. for(i = 0; i < 20; i++) {
  155. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  156. }
  157. fprintf(stderr, "\n");
  158. if(auth_pw) {
  159. /* We could authenticate via password */
  160. while((rc = libssh2_userauth_password(session, username, password))
  161. == LIBSSH2_ERROR_EAGAIN);
  162. if(rc) {
  163. fprintf(stderr, "Authentication by password failed.\n");
  164. goto shutdown;
  165. }
  166. }
  167. else {
  168. /* Or by public key */
  169. while((rc =
  170. libssh2_userauth_publickey_fromfile(session, username,
  171. "/home/username/"
  172. ".ssh/id_rsa.pub",
  173. "/home/username/"
  174. ".ssh/id_rsa",
  175. password)) ==
  176. LIBSSH2_ERROR_EAGAIN);
  177. if(rc) {
  178. fprintf(stderr, "\tAuthentication by public key failed\n");
  179. goto shutdown;
  180. }
  181. }
  182. #if 0
  183. libssh2_trace(session, LIBSSH2_TRACE_CONN);
  184. #endif
  185. fprintf(stderr, "libssh2_sftp_init()!\n");
  186. do {
  187. sftp_session = libssh2_sftp_init(session);
  188. if(!sftp_session) {
  189. if(libssh2_session_last_errno(session) ==
  190. LIBSSH2_ERROR_EAGAIN) {
  191. fprintf(stderr, "non-blocking init\n");
  192. waitsocket(sock, session); /* now we wait */
  193. }
  194. else {
  195. fprintf(stderr, "Unable to init SFTP session\n");
  196. goto shutdown;
  197. }
  198. }
  199. } while(!sftp_session);
  200. fprintf(stderr, "libssh2_sftp_open()!\n");
  201. /* Request a file via SFTP */
  202. do {
  203. sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
  204. LIBSSH2_FXF_READ, 0);
  205. if(!sftp_handle) {
  206. if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
  207. fprintf(stderr, "Unable to open file with SFTP\n");
  208. goto shutdown;
  209. }
  210. else {
  211. fprintf(stderr, "non-blocking open\n");
  212. waitsocket(sock, session); /* now we wait */
  213. }
  214. }
  215. } while(!sftp_handle);
  216. fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
  217. do {
  218. char mem[1024*24];
  219. /* loop until we fail */
  220. while((rc = libssh2_sftp_read(sftp_handle, mem,
  221. sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) {
  222. spin++;
  223. waitsocket(sock, session); /* now we wait */
  224. }
  225. if(rc > 0) {
  226. total += rc;
  227. write(1, mem, rc);
  228. }
  229. else {
  230. break;
  231. }
  232. } while(1);
  233. #ifdef HAVE_GETTIMEOFDAY
  234. gettimeofday(&end, NULL);
  235. time_ms = tvdiff(end, start);
  236. fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n",
  237. total,
  238. time_ms, total/(time_ms/1000.0), spin);
  239. #else
  240. fprintf(stderr, "Got %d bytes spin: %d\n", total, spin);
  241. #endif
  242. libssh2_sftp_close(sftp_handle);
  243. libssh2_sftp_shutdown(sftp_session);
  244. shutdown:
  245. fprintf(stderr, "libssh2_session_disconnect\n");
  246. while(libssh2_session_disconnect(session,
  247. "Normal Shutdown, Thank you") ==
  248. LIBSSH2_ERROR_EAGAIN);
  249. libssh2_session_free(session);
  250. #ifdef WIN32
  251. closesocket(sock);
  252. #else
  253. close(sock);
  254. #endif
  255. fprintf(stderr, "all done\n");
  256. libssh2_exit();
  257. return 0;
  258. }