diff --git a/.travis.yml b/.travis.yml index c44ae3a..c60c729 100644 --- a/.travis.yml +++ b/.travis.yml @@ -102,7 +102,7 @@ install: script: - | if [ "$B" = "style" ]; then - perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] + perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] include/*.h example/*.c fi - | if [ "$B" = "configure" ]; then diff --git a/Makefile.am b/Makefile.am index 3c428d6..6411e8a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -149,4 +149,4 @@ $(VCPROJ): win32/vc8proj.head win32/vc8proj.foot Makefile.am awk '{printf("%s\r\n", gensub("\r", "", "g"))}' > $@ ) checksrc: - perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] + perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] include/*.h example/*.c diff --git a/example/direct_tcpip.c b/example/direct_tcpip.c index fc3b5bf..4e1d90f 100644 --- a/example/direct_tcpip.c +++ b/example/direct_tcpip.c @@ -72,8 +72,8 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } @@ -82,49 +82,50 @@ int main(int argc, char *argv[]) int listensock = -1, forwardsock = -1; #endif - if (argc > 1) + if(argc > 1) server_ip = argv[1]; - if (argc > 2) + if(argc > 2) username = argv[2]; - if (argc > 3) + if(argc > 3) password = argv[3]; - if (argc > 4) + if(argc > 4) local_listenip = argv[4]; - if (argc > 5) + if(argc > 5) local_listenport = atoi(argv[5]); - if (argc > 6) + if(argc > 6) remote_desthost = argv[6]; - if (argc > 7) + if(argc > 7) remote_destport = atoi(argv[7]); - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } /* Connect to SSH server */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #ifdef WIN32 - if (sock == INVALID_SOCKET) { + if(sock == INVALID_SOCKET) { fprintf(stderr, "failed to open socket!\n"); return -1; } #else - if (sock == -1) { + if(sock == -1) { perror("socket"); return -1; } #endif sin.sin_family = AF_INET; - if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { + sin.sin_addr.s_addr = inet_addr(server_ip); + if(INADDR_NONE == sin.sin_addr.s_addr) { perror("inet_addr"); return -1; } sin.sin_port = htons(22); - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -159,44 +160,46 @@ int main(int argc, char *argv[]) /* check what authentication methods are available */ userauthlist = libssh2_userauth_list(session, username, strlen(username)); fprintf(stderr, "Authentication methods: %s\n", userauthlist); - if (strstr(userauthlist, "password")) + if(strstr(userauthlist, "password")) auth |= AUTH_PASSWORD; - if (strstr(userauthlist, "publickey")) + if(strstr(userauthlist, "publickey")) auth |= AUTH_PUBLICKEY; /* check for options */ if(argc > 8) { - if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) + if((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) auth = AUTH_PASSWORD; - if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) + if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) auth = AUTH_PUBLICKEY; } - if (auth & AUTH_PASSWORD) { - if (libssh2_userauth_password(session, username, password)) { + if(auth & AUTH_PASSWORD) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else if (auth & AUTH_PUBLICKEY) { - if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + } + else if(auth & AUTH_PUBLICKEY) { + if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } fprintf(stderr, "\tAuthentication by public key succeeded.\n"); - } else { + } + else { fprintf(stderr, "No supported authentication methods found!\n"); goto shutdown; } listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #ifdef WIN32 - if (listensock == INVALID_SOCKET) { + if(listensock == INVALID_SOCKET) { fprintf(stderr, "failed to open listen socket!\n"); return -1; } #else - if (listensock == -1) { + if(listensock == -1) { perror("socket"); return -1; } @@ -204,18 +207,20 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(local_listenport); - if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_listenip))) { + sin.sin_addr.s_addr = inet_addr(local_listenip); + if(INADDR_NONE == sin.sin_addr.s_addr) { perror("inet_addr"); goto shutdown; } sockopt = 1; - setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)); - sinlen=sizeof(sin); - if (-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) { + setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, + sizeof(sockopt)); + sinlen = sizeof(sin); + if(-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) { perror("bind"); goto shutdown; } - if (-1 == listen(listensock, 2)) { + if(-1 == listen(listensock, 2)) { perror("listen"); goto shutdown; } @@ -225,12 +230,12 @@ int main(int argc, char *argv[]) forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen); #ifdef WIN32 - if (forwardsock == INVALID_SOCKET) { + if(forwardsock == INVALID_SOCKET) { fprintf(stderr, "failed to accept forward socket!\n"); goto shutdown; } #else - if (forwardsock == -1) { + if(forwardsock == -1) { perror("accept"); goto shutdown; } @@ -244,7 +249,7 @@ int main(int argc, char *argv[]) channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost, remote_destport, shost, sport); - if (!channel) { + if(!channel) { fprintf(stderr, "Could not open the direct-tcpip channel!\n" "(Note that this can be a problem at the server!" " Please review the server logs.)\n"); @@ -254,22 +259,23 @@ int main(int argc, char *argv[]) /* Must use non-blocking IO hereafter due to the current libssh2 API */ libssh2_session_set_blocking(session, 0); - while (1) { + while(1) { FD_ZERO(&fds); FD_SET(forwardsock, &fds); tv.tv_sec = 0; tv.tv_usec = 100000; rc = select(forwardsock + 1, &fds, NULL, NULL, &tv); - if (-1 == rc) { + if(-1 == rc) { perror("select"); goto shutdown; } - if (rc && FD_ISSET(forwardsock, &fds)) { + if(rc && FD_ISSET(forwardsock, &fds)) { len = recv(forwardsock, buf, sizeof(buf), 0); - if (len < 0) { + if(len < 0) { perror("read"); goto shutdown; - } else if (0 == len) { + } + else if(0 == len) { fprintf(stderr, "The client at %s:%d disconnected!\n", shost, sport); goto shutdown; @@ -277,34 +283,34 @@ int main(int argc, char *argv[]) wr = 0; while(wr < len) { i = libssh2_channel_write(channel, buf + wr, len - wr); - if (LIBSSH2_ERROR_EAGAIN == i) { + if(LIBSSH2_ERROR_EAGAIN == i) { continue; } - if (i < 0) { + if(i < 0) { fprintf(stderr, "libssh2_channel_write: %d\n", i); goto shutdown; } wr += i; } } - while (1) { + while(1) { len = libssh2_channel_read(channel, buf, sizeof(buf)); - if (LIBSSH2_ERROR_EAGAIN == len) + if(LIBSSH2_ERROR_EAGAIN == len) break; - else if (len < 0) { + else if(len < 0) { fprintf(stderr, "libssh2_channel_read: %d", (int)len); goto shutdown; } wr = 0; - while (wr < len) { + while(wr < len) { i = send(forwardsock, buf + wr, len - wr, 0); - if (i <= 0) { + if(i <= 0) { perror("write"); goto shutdown; } wr += i; } - if (libssh2_channel_eof(channel)) { + if(libssh2_channel_eof(channel)) { fprintf(stderr, "The server at %s:%d disconnected!\n", remote_desthost, remote_destport); goto shutdown; @@ -320,7 +326,7 @@ shutdown: close(forwardsock); close(listensock); #endif - if (channel) + if(channel) libssh2_channel_free(channel); libssh2_session_disconnect(session, "Client disconnecting normally"); libssh2_session_free(session); diff --git a/example/scp.c b/example/scp.c index e8e4217..9ad1e7d 100644 --- a/example/scp.c +++ b/example/scp.c @@ -38,9 +38,9 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel; - const char *username="username"; - const char *password="password"; - const char *scppath="/tmp/TEST"; + const char *username = "username"; + const char *password = "password"; + const char *scppath = "/tmp/TEST"; libssh2_struct_stat fileinfo; int rc; libssh2_struct_stat_size got = 0; @@ -49,31 +49,32 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } - if (argc > 4) { + if(argc > 4) { scppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -86,8 +87,8 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -119,18 +120,20 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { +#define HOME_DIR "/home/username/" + if(libssh2_userauth_publickey_fromfile(session, username, + HOME_DIR ".ssh/id_rsa.pub", + HOME_DIR ".ssh/id_rsa", + password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -139,7 +142,7 @@ int main(int argc, char *argv[]) /* Request a file via SCP */ channel = libssh2_scp_recv2(session, scppath, &fileinfo); - if (!channel) { + if(!channel) { fprintf(stderr, "Unable to open a session: %d\n", libssh2_session_last_errno(session)); goto shutdown; @@ -148,7 +151,7 @@ int main(int argc, char *argv[]) while(got < fileinfo.st_size) { char mem[1024]; - int amount=sizeof(mem); + int amount = sizeof(mem); if((fileinfo.st_size -got) < amount) { amount = (int)(fileinfo.st_size -got); @@ -170,7 +173,8 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/scp_nonblock.c b/example/scp_nonblock.c index 1f8682d..bc5bdb3 100644 --- a/example/scp_nonblock.c +++ b/example/scp_nonblock.c @@ -85,9 +85,9 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel; - const char *username="username"; - const char *password="password"; - const char *scppath="/tmp/TEST"; + const char *username = "username"; + const char *password = "password"; + const char *scppath = "/tmp/TEST"; libssh2_struct_stat fileinfo; #ifdef HAVE_GETTIMEOFDAY struct timeval start; @@ -103,31 +103,32 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } - if (argc > 4) { + if(argc > 4) { scppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -140,14 +141,14 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } /* Create a session instance */ session = libssh2_session_init(); - if (!session) + if(!session) return -1; /* Since we have set non-blocking, tell libssh2 we are non-blocking */ @@ -160,9 +161,9 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = libssh2_session_handshake(session, sock)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; } @@ -179,24 +180,25 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - while ((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/" - ".ssh/id_rsa.pub", - "/home/username/" - ".ssh/id_rsa", - password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/username/" + ".ssh/id_rsa.pub", + "/home/username/" + ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -211,7 +213,7 @@ int main(int argc, char *argv[]) do { channel = libssh2_scp_recv2(session, scppath, &fileinfo); - if (!channel) { + if(!channel) { if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { char *err_msg; @@ -224,7 +226,7 @@ int main(int argc, char *argv[]) waitsocket(sock, session); } } - } while (!channel); + } while(!channel); fprintf(stderr, "libssh2_scp_recv() is done, now receive data!\n"); while(got < fileinfo.st_size) { @@ -232,22 +234,22 @@ int main(int argc, char *argv[]) int rc; do { - int amount=sizeof(mem); + int amount = sizeof(mem); - if ((fileinfo.st_size -got) < amount) { + if((fileinfo.st_size -got) < amount) { amount = (int)(fileinfo.st_size - got); } /* loop until we block */ rc = libssh2_channel_read(channel, mem, amount); - if (rc > 0) { + if(rc > 0) { write(1, mem, rc); got += rc; total += rc; } - } while (rc > 0); + } while(rc > 0); - if ((rc == LIBSSH2_ERROR_EAGAIN) && (got < fileinfo.st_size)) { + if((rc == LIBSSH2_ERROR_EAGAIN) && (got < fileinfo.st_size)) { /* this is due to blocking that would occur otherwise so we loop on this condition */ @@ -262,8 +264,9 @@ int main(int argc, char *argv[]) gettimeofday(&end, NULL); time_ms = tvdiff(end, start); - fprintf(stderr, "Got %ld bytes in %ld ms = %.1f bytes/sec spin: %d\n", (long)total, - time_ms, total/(time_ms/1000.0), spin); + fprintf(stderr, "Got %ld bytes in %ld ms = %.1f bytes/sec spin: %d\n", + (long)total, + time_ms, total/(time_ms/1000.0), spin); #else fprintf(stderr, "Got %ld bytes spin: %d\n", (long)total, spin); #endif diff --git a/example/scp_write.c b/example/scp_write.c index eef6e81..030232e 100644 --- a/example/scp_write.c +++ b/example/scp_write.c @@ -38,10 +38,10 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session = NULL; LIBSSH2_CHANNEL *channel; - const char *username="username"; - const char *password="password"; - const char *loclfile="scp_write.c"; - const char *scppath="/tmp/TEST"; + const char *username = "username"; + const char *password = "password"; + const char *loclfile = "scp_write.c"; + const char *scppath = "/tmp/TEST"; FILE *local; int rc; char mem[1024]; @@ -53,39 +53,40 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } if(argc > 4) { loclfile = argv[4]; } - if (argc > 5) { + if(argc > 5) { scppath = argv[5]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } local = fopen(loclfile, "rb"); - if (!local) { + if(!local) { fprintf(stderr, "Can't open local file %s\n", loclfile); return -1; } @@ -105,8 +106,8 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -138,18 +139,20 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { +#define HOME "/home/username/" + if(libssh2_userauth_publickey_fromfile(session, username, + HOME ".ssh/id_rsa.pub", + HOME ".ssh/id_rsa", + password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -159,7 +162,7 @@ int main(int argc, char *argv[]) channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777, (unsigned long)fileinfo.st_size); - if (!channel) { + if(!channel) { char *errmsg; int errlen; int err = libssh2_session_last_error(session, &errmsg, &errlen, 0); @@ -170,7 +173,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "SCP session waiting to send file\n"); do { nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { + if(nread <= 0) { /* end of file */ break; } @@ -179,7 +182,7 @@ int main(int argc, char *argv[]) do { /* write the same data over and over, until error or completion */ rc = libssh2_channel_write(channel, ptr, nread); - if (rc < 0) { + if(rc < 0) { fprintf(stderr, "ERROR %d\n", rc); break; } @@ -188,9 +191,9 @@ int main(int argc, char *argv[]) ptr += rc; nread -= rc; } - } while (nread); + } while(nread); - } while (1); + } while(1); fprintf(stderr, "Sending EOF\n"); libssh2_channel_send_eof(channel); @@ -207,7 +210,7 @@ int main(int argc, char *argv[]) shutdown: if(session) { - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); } #ifdef WIN32 @@ -215,7 +218,7 @@ int main(int argc, char *argv[]) #else close(sock); #endif - if (local) + if(local) fclose(local); fprintf(stderr, "all done\n"); diff --git a/example/scp_write_nonblock.c b/example/scp_write_nonblock.c index bb8e39d..9226322 100644 --- a/example/scp_write_nonblock.c +++ b/example/scp_write_nonblock.c @@ -73,10 +73,10 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session = NULL; LIBSSH2_CHANNEL *channel; - const char *username="username"; - const char *password="password"; - const char *loclfile="scp_write.c"; - const char *scppath="/tmp/TEST"; + const char *username = "username"; + const char *password = "password"; + const char *loclfile = "scp_write.c"; + const char *scppath = "/tmp/TEST"; FILE *local; int rc; char mem[1024*100]; @@ -92,39 +92,40 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } if(argc > 4) { loclfile = argv[4]; } - if (argc > 5) { + if(argc > 5) { scppath = argv[5]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } local = fopen(loclfile, "rb"); - if (!local) { + if(!local) { fprintf(stderr, "Can't local file %s\n", loclfile); return -1; } @@ -140,8 +141,8 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -158,8 +159,8 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) - == LIBSSH2_ERROR_EAGAIN); + while((rc = libssh2_session_handshake(session, sock)) + == LIBSSH2_ERROR_EAGAIN); if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; @@ -177,21 +178,24 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - while ((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { +#define HOME "/home/username/" + while((rc = libssh2_userauth_publickey_fromfile(session, username, + HOME ".ssh/id_rsa.pub", + HOME ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -202,21 +206,21 @@ int main(int argc, char *argv[]) channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777, (unsigned long)fileinfo.st_size); - if ((!channel) && (libssh2_session_last_errno(session) != - LIBSSH2_ERROR_EAGAIN)) { + if((!channel) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { char *err_msg; libssh2_session_last_error(session, &err_msg, NULL, 0); fprintf(stderr, "%s\n", err_msg); goto shutdown; } - } while (!channel); + } while(!channel); fprintf(stderr, "SCP session waiting to send file\n"); start = time(NULL); do { nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { + if(nread <= 0) { /* end of file */ break; } @@ -226,12 +230,12 @@ int main(int argc, char *argv[]) prev = 0; do { - while ((rc = libssh2_channel_write(channel, ptr, nread)) == - LIBSSH2_ERROR_EAGAIN) { + while((rc = libssh2_channel_write(channel, ptr, nread)) == + LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); prev = 0; } - if (rc < 0) { + if(rc < 0) { fprintf(stderr, "ERROR %d total %ld / %d prev %d\n", rc, total, (int)nread, (int)prev); break; @@ -243,8 +247,8 @@ int main(int argc, char *argv[]) nread -= rc; ptr += rc; } - } while (nread); - } while (!nread); /* only continue if nread was drained */ + } while(nread); + } while(!nread); /* only continue if nread was drained */ duration = (int)(time(NULL)-start); @@ -252,22 +256,22 @@ int main(int argc, char *argv[]) total, duration, total/(double)duration); fprintf(stderr, "Sending EOF\n"); - while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN); + while(libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN); fprintf(stderr, "Waiting for EOF\n"); - while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN); + while(libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN); fprintf(stderr, "Waiting for channel to close\n"); - while (libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN); + while(libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN); libssh2_channel_free(channel); channel = NULL; shutdown: - while (libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing") == - LIBSSH2_ERROR_EAGAIN); + while(libssh2_session_disconnect(session, + "Normal Shutdown,") == + LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp.c b/example/sftp.c index 0feb534..8f67244 100644 --- a/example/sftp.c +++ b/example/sftp.c @@ -37,18 +37,19 @@ #include -const char *keyfile1="~/.ssh/id_rsa.pub"; -const char *keyfile2="~/.ssh/id_rsa"; -const char *username="username"; -const char *password="password"; -const char *sftppath="/tmp/TEST"; +const char *keyfile1 = "~/.ssh/id_rsa.pub"; +const char *keyfile2 = "~/.ssh/id_rsa"; +const char *username = "username"; +const char *password = "password"; +const char *sftppath = "/tmp/TEST"; -static void kbd_callback(const char *name, int name_len, - const char *instruction, int instruction_len, int num_prompts, - const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, - LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, - void **abstract) +static void kbd_callback(const char *name, int name_len, + const char *instruction, int instruction_len, + int num_prompts, + const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, + LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, + void **abstract) { int i; size_t n; @@ -67,7 +68,7 @@ static void kbd_callback(const char *name, int name_len, fprintf(stderr, "Number of prompts: %d\n\n", num_prompts); - for (i = 0; i < num_prompts; i++) { + for(i = 0; i < num_prompts; i++) { fprintf(stderr, "Prompt %d from server: '", i); fwrite(prompts[i].text, 1, prompts[i].length, stderr); fprintf(stderr, "'\n"); @@ -75,7 +76,7 @@ static void kbd_callback(const char *name, int name_len, fprintf(stderr, "Please type response: "); fgets(buf, sizeof(buf), stdin); n = strlen(buf); - while (n > 0 && strchr("\r\n", buf[n - 1])) + while(n > 0 && strchr("\r\n", buf[n - 1])) n--; buf[n] = 0; @@ -108,16 +109,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -131,8 +133,8 @@ int main(int argc, char *argv[]) sftppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { + rc = libssh2_init(0); + if(rc != 0) { fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -146,8 +148,8 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -185,54 +187,61 @@ int main(int argc, char *argv[]) /* check what authentication methods are available */ userauthlist = libssh2_userauth_list(session, username, strlen(username)); fprintf(stderr, "Authentication methods: %s\n", userauthlist); - if (strstr(userauthlist, "password") != NULL) { + if(strstr(userauthlist, "password") != NULL) { auth_pw |= 1; } - if (strstr(userauthlist, "keyboard-interactive") != NULL) { + if(strstr(userauthlist, "keyboard-interactive") != NULL) { auth_pw |= 2; } - if (strstr(userauthlist, "publickey") != NULL) { + if(strstr(userauthlist, "publickey") != NULL) { auth_pw |= 4; } - /* if we got an 4. argument we set this option if supported */ + /* if we got an 4. argument we set this option if supported */ if(argc > 5) { - if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { + if((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { auth_pw = 1; } - if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { + if((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { auth_pw = 2; } - if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { + if((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { auth_pw = 4; } } - if (auth_pw & 1) { + if(auth_pw & 1) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else if (auth_pw & 2) { + } + else if(auth_pw & 2) { /* Or via keyboard-interactive */ - if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) { + if(libssh2_userauth_keyboard_interactive(session, username, + &kbd_callback) ) { fprintf(stderr, "\tAuthentication by keyboard-interactive failed!\n"); goto shutdown; - } else { + } + else { fprintf(stderr, "\tAuthentication by keyboard-interactive succeeded.\n"); } - } else if (auth_pw & 4) { + } + else if(auth_pw & 4) { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; - } else { + } + else { fprintf(stderr, "\tAuthentication by public key succeeded.\n"); } - } else { + } + else { fprintf(stderr, "No supported authentication methods found!\n"); goto shutdown; } @@ -240,7 +249,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_init()!\n"); sftp_session = libssh2_sftp_init(session); - if (!sftp_session) { + if(!sftp_session) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } @@ -250,7 +259,7 @@ int main(int argc, char *argv[]) sftp_handle = libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); - if (!sftp_handle) { + if(!sftp_handle) { fprintf(stderr, "Unable to open file with SFTP: %ld\n", libssh2_sftp_last_error(sftp_session)); goto shutdown; @@ -262,19 +271,20 @@ int main(int argc, char *argv[]) /* loop until we fail */ fprintf(stderr, "libssh2_sftp_read()!\n"); rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); - if (rc > 0) { + if(rc > 0) { write(1, mem, rc); - } else { + } + else { break; } - } while (1); + } while(1); libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp_RW_nonblock.c b/example/sftp_RW_nonblock.c index 797c75c..70d87db 100644 --- a/example/sftp_RW_nonblock.c +++ b/example/sftp_RW_nonblock.c @@ -79,10 +79,10 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *sftppath="/tmp/TEST"; /* source path */ - const char *dest="/tmp/TEST2"; /* destination path */ + const char *username = "username"; + const char *password = "password"; + const char *sftppath = "/tmp/TEST"; /* source path */ + const char *dest = "/tmp/TEST2"; /* destination path */ int rc; LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP_HANDLE *sftp_handle; @@ -96,16 +96,16 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -118,7 +118,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = htonl(0x7F000001); - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -172,25 +172,26 @@ int main(int argc, char *argv[]) goto shutdown; } - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) + while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - while ((rc = - libssh2_userauth_publickey_fromfile(session, username, - "/home/username/" - ".ssh/id_rsa.pub", - "/home/username/" - ".ssh/id_rsa", - password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = + libssh2_userauth_publickey_fromfile(session, username, + "/home/username/" + ".ssh/id_rsa.pub", + "/home/username/" + ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -210,15 +211,15 @@ int main(int argc, char *argv[]) goto shutdown; } } - } while (!sftp_session); + } while(!sftp_session); /* Request a file via SFTP */ do { sftp_handle = libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); - if (!sftp_handle) { - if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { + if(!sftp_handle) { + if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } @@ -227,7 +228,7 @@ int main(int argc, char *argv[]) waitsocket(sock, session); /* now we wait */ } } - } while (!sftp_handle); + } while(!sftp_handle); fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); do { @@ -243,7 +244,7 @@ int main(int argc, char *argv[]) /* write to temporary storage area */ fwrite(mem, rc, 1, tempstorage); } - } while (rc > 0); + } while(rc > 0); if(rc != LIBSSH2_ERROR_EAGAIN) { /* error or end of file */ @@ -259,7 +260,7 @@ int main(int argc, char *argv[]) FD_SET(sock, &fd2); /* wait for readable or writeable */ - rc = select(sock+1, &fd, &fd2, NULL, &timeout); + rc = select(sock + 1, &fd, &fd2, NULL, &timeout); if(rc <= 0) { /* negative is error 0 is timeout */ @@ -267,7 +268,7 @@ int main(int argc, char *argv[]) break; } - } while (1); + } while(1); libssh2_sftp_close(sftp_handle); fclose(tempstorage); @@ -303,7 +304,7 @@ int main(int argc, char *argv[]) nread); ptr += rc; nread -= nread; - } while (rc >= 0); + } while(rc >= 0); if(rc != LIBSSH2_ERROR_EAGAIN) { /* error or end of file */ @@ -319,7 +320,7 @@ int main(int argc, char *argv[]) FD_SET(sock, &fd2); /* wait for readable or writeable */ - rc = select(sock+1, &fd, &fd2, NULL, &timeout); + rc = select(sock + 1, &fd, &fd2, NULL, &timeout); if(rc <= 0) { /* negative is error 0 is timeout */ @@ -327,7 +328,7 @@ int main(int argc, char *argv[]) rc); break; } - } while (1); + } while(1); fprintf(stderr, "SFTP upload done!\n"); } else { @@ -339,7 +340,7 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 @@ -347,7 +348,7 @@ int main(int argc, char *argv[]) #else close(sock); #endif - if (tempstorage) + if(tempstorage) fclose(tempstorage); fprintf(stderr, "all done\n"); diff --git a/example/sftp_append.c b/example/sftp_append.c index 788c51f..bfea1f7 100644 --- a/example/sftp_append.c +++ b/example/sftp_append.c @@ -40,10 +40,10 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *loclfile="sftp_write.c"; - const char *sftppath="/tmp/TEST"; + const char *username = "username"; + const char *password = "password"; + const char *loclfile = "sftp_write.c"; + const char *sftppath = "/tmp/TEST"; int rc; FILE *local; LIBSSH2_SFTP *sftp_session; @@ -57,16 +57,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -83,14 +84,14 @@ int main(int argc, char *argv[]) sftppath = argv[5]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } local = fopen(loclfile, "rb"); - if (!local) { + if(!local) { fprintf(stderr, "Can't open local file %s\n", loclfile); return -1; } @@ -104,8 +105,8 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -140,18 +141,20 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { +#define HOME "/home/username/" + if(libssh2_userauth_publickey_fromfile(session, username, + HOME ".ssh/id_rsa.pub", + HOME ".ssh/id_rsa", + password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -160,7 +163,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_init()!\n"); sftp_session = libssh2_sftp_init(session); - if (!sftp_session) { + if(!sftp_session) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } @@ -173,7 +176,7 @@ int main(int argc, char *argv[]) LIBSSH2_FXF_WRITE|LIBSSH2_FXF_READ, LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); - if (!sftp_handle) { + if(!sftp_handle) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } @@ -188,14 +191,14 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_open() a handle for APPEND\n"); - if (!sftp_handle) { + if(!sftp_handle) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); do { nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { + if(nread <= 0) { /* end of file */ break; } @@ -208,9 +211,9 @@ int main(int argc, char *argv[]) break; ptr += rc; nread -= rc; - } while (nread); + } while(nread); - } while (rc > 0); + } while(rc > 0); libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); @@ -225,7 +228,7 @@ shutdown: #else close(sock); #endif - if (local) + if(local) fclose(local); fprintf(stderr, "all done\n"); diff --git a/example/sftp_mkdir.c b/example/sftp_mkdir.c index 1270adb..2347abe 100644 --- a/example/sftp_mkdir.c +++ b/example/sftp_mkdir.c @@ -40,9 +40,9 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *sftppath="/tmp/sftp_mkdir"; + const char *username = "username"; + const char *password = "password"; + const char *sftppath = "/tmp/sftp_mkdir"; int rc; LIBSSH2_SFTP *sftp_session; @@ -50,16 +50,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -73,9 +74,9 @@ int main(int argc, char *argv[]) sftppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -88,7 +89,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -121,15 +122,16 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, + if(libssh2_userauth_publickey_fromfile(session, username, "/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa", password)) { @@ -140,7 +142,7 @@ int main(int argc, char *argv[]) sftp_session = libssh2_sftp_init(session); - if (!sftp_session) { + if(!sftp_session) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } @@ -161,7 +163,7 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp_mkdir_nonblock.c b/example/sftp_mkdir_nonblock.c index db366d2..217cc4b 100644 --- a/example/sftp_mkdir_nonblock.c +++ b/example/sftp_mkdir_nonblock.c @@ -40,9 +40,9 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *sftppath="/tmp/sftp_mkdir_nonblock"; + const char *username = "username"; + const char *password = "password"; + const char *sftppath = "/tmp/sftp_mkdir_nonblock"; int rc; LIBSSH2_SFTP *sftp_session; @@ -50,16 +50,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -73,9 +74,9 @@ int main(int argc, char *argv[]) sftppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -88,7 +89,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -121,15 +122,16 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, + if(libssh2_userauth_publickey_fromfile(session, username, "/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa", password)) { @@ -141,7 +143,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_init()!\n"); sftp_session = libssh2_sftp_init(session); - if (!sftp_session) { + if(!sftp_session) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } @@ -151,7 +153,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_mkdirnb()!\n"); /* Make a directory via SFTP */ - while (libssh2_sftp_mkdir(sftp_session, sftppath, + while(libssh2_sftp_mkdir(sftp_session, sftppath, LIBSSH2_SFTP_S_IRWXU| LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP| LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH) @@ -161,7 +163,7 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp_nonblock.c b/example/sftp_nonblock.c index 0db0cb6..8ef091e 100644 --- a/example/sftp_nonblock.c +++ b/example/sftp_nonblock.c @@ -85,9 +85,9 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *sftppath="/tmp/TEST"; + const char *username = "username"; + const char *password = "password"; + const char *sftppath = "/tmp/TEST"; #ifdef HAVE_GETTIMEOFDAY struct timeval start; struct timeval end; @@ -103,32 +103,33 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } - if (argc > 4) { + if(argc > 4) { sftppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -141,7 +142,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -149,7 +150,7 @@ int main(int argc, char *argv[]) /* Create a session instance */ session = libssh2_session_init(); - if (!session) + if(!session) return -1; /* Since we have set non-blocking, tell libssh2 we are non-blocking */ @@ -162,9 +163,9 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) == + while((rc = libssh2_session_handshake(session, sock)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; } @@ -181,25 +182,26 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) + while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - while ((rc = - libssh2_userauth_publickey_fromfile(session, username, - "/home/username/" - ".ssh/id_rsa.pub", - "/home/username/" - ".ssh/id_rsa", - password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = + libssh2_userauth_publickey_fromfile(session, username, + "/home/username/" + ".ssh/id_rsa.pub", + "/home/username/" + ".ssh/id_rsa", + password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -222,7 +224,7 @@ int main(int argc, char *argv[]) goto shutdown; } } - } while (!sftp_session); + } while(!sftp_session); fprintf(stderr, "libssh2_sftp_open()!\n"); /* Request a file via SFTP */ @@ -230,8 +232,8 @@ int main(int argc, char *argv[]) sftp_handle = libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); - if (!sftp_handle) { - if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { + if(!sftp_handle) { + if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } @@ -240,31 +242,33 @@ int main(int argc, char *argv[]) waitsocket(sock, session); /* now we wait */ } } - } while (!sftp_handle); + } while(!sftp_handle); fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); do { char mem[1024*24]; /* loop until we fail */ - while ((rc = libssh2_sftp_read(sftp_handle, mem, + while((rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) { spin++; waitsocket(sock, session); /* now we wait */ } - if (rc > 0) { + if(rc > 0) { total += rc; write(1, mem, rc); - } else { + } + else { break; } - } while (1); + } while(1); #ifdef HAVE_GETTIMEOFDAY gettimeofday(&end, NULL); time_ms = tvdiff(end, start); - fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total, - time_ms, total/(time_ms/1000.0), spin ); + fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", + total, + time_ms, total/(time_ms/1000.0), spin); #else fprintf(stderr, "Got %d bytes spin: %d\n", total, spin); #endif @@ -275,7 +279,7 @@ int main(int argc, char *argv[]) shutdown: fprintf(stderr, "libssh2_session_disconnect\n"); - while (libssh2_session_disconnect(session, + while(libssh2_session_disconnect(session, "Normal Shutdown, Thank you") == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); diff --git a/example/sftp_write.c b/example/sftp_write.c index 7afc187..c1350e9 100644 --- a/example/sftp_write.c +++ b/example/sftp_write.c @@ -40,10 +40,10 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *loclfile="sftp_write.c"; - const char *sftppath="/tmp/TEST"; + const char *username = "username"; + const char *password = "password"; + const char *loclfile = "sftp_write.c"; + const char *sftppath = "/tmp/TEST"; int rc; FILE *local; LIBSSH2_SFTP *sftp_session; @@ -56,16 +56,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -82,14 +83,14 @@ int main(int argc, char *argv[]) sftppath = argv[5]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } local = fopen(loclfile, "rb"); - if (!local) { + if(!local) { fprintf(stderr, "Can't open local file %s\n", loclfile); return -1; } @@ -103,7 +104,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -139,18 +140,20 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa.pub"; + if(libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -159,7 +162,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_init()!\n"); sftp_session = libssh2_sftp_init(session); - if (!sftp_session) { + if(!sftp_session) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } @@ -172,14 +175,14 @@ int main(int argc, char *argv[]) LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); - if (!sftp_handle) { + if(!sftp_handle) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); do { nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { + if(nread <= 0) { /* end of file */ break; } @@ -192,9 +195,9 @@ int main(int argc, char *argv[]) break; ptr += rc; nread -= rc; - } while (nread); + } while(nread); - } while (rc > 0); + } while(rc > 0); libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); @@ -209,7 +212,7 @@ shutdown: #else close(sock); #endif - if (local) + if(local) fclose(local); fprintf(stderr, "all done\n"); diff --git a/example/sftp_write_nonblock.c b/example/sftp_write_nonblock.c index 2e22395..934749e 100644 --- a/example/sftp_write_nonblock.c +++ b/example/sftp_write_nonblock.c @@ -4,7 +4,7 @@ * The sample code has default values for host name, user name, password * and path to copy, but you can specify them on the command line like: * - * "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c" + * "sftp 192.168.0.1 user password thisfile /tmp/storehere" */ #include "libssh2_config.h" @@ -77,10 +77,10 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *loclfile="sftp_write_nonblock.c"; - const char *sftppath="/tmp/sftp_write_nonblock.c"; + const char *username = "username"; + const char *password = "password"; + const char *loclfile = "sftp_write_nonblock.c"; + const char *sftppath = "/tmp/sftp_write_nonblock.c"; int rc; FILE *local; LIBSSH2_SFTP *sftp_session; @@ -96,40 +96,41 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } - if (argc > 4) { + if(argc > 4) { loclfile = argv[4]; } - if (argc > 5) { + if(argc > 5) { sftppath = argv[5]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } local = fopen(loclfile, "rb"); - if (!local) { + if(!local) { fprintf(stderr, "Can't open local file %s\n", loclfile); return -1; } @@ -143,7 +144,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -152,7 +153,7 @@ int main(int argc, char *argv[]) /* Create a session instance */ session = libssh2_session_init(); - if (!session) + if(!session) return -1; /* Since we have set non-blocking, tell libssh2 we are non-blocking */ @@ -161,9 +162,9 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) + while((rc = libssh2_session_handshake(session, sock)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; } @@ -180,22 +181,24 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == + while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - while ((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; + while((rc = libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -205,28 +208,28 @@ int main(int argc, char *argv[]) do { sftp_session = libssh2_sftp_init(session); - if (!sftp_session && + if(!sftp_session && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } - } while (!sftp_session); + } while(!sftp_session); fprintf(stderr, "libssh2_sftp_open()!\n"); /* Request a file via SFTP */ do { sftp_handle = - libssh2_sftp_open(sftp_session, sftppath, - LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, - LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| - LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); - - if (!sftp_handle && - (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { + libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT| + LIBSSH2_FXF_TRUNC, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + if(!sftp_handle && + (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } - } while (!sftp_handle); + } while(!sftp_handle); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); @@ -234,7 +237,7 @@ int main(int argc, char *argv[]) do { nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { + if(nread <= 0) { /* end of file */ break; } @@ -244,7 +247,7 @@ int main(int argc, char *argv[]) do { /* write data in a loop until we block */ - while ((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) == + while((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); } @@ -253,8 +256,8 @@ int main(int argc, char *argv[]) ptr += rc; nread -= rc; - } while (nread); - } while (rc > 0); + } while(nread); + } while(rc > 0); duration = (int)(time(NULL)-start); @@ -268,7 +271,7 @@ int main(int argc, char *argv[]) shutdown: - while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing") + while(libssh2_session_disconnect(session, "Normal Shutdown") == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); diff --git a/example/sftp_write_sliding.c b/example/sftp_write_sliding.c index 19fe851..9a72140 100644 --- a/example/sftp_write_sliding.c +++ b/example/sftp_write_sliding.c @@ -4,7 +4,7 @@ * The sample code has default values for host name, user name, password * and path to copy, but you can specify them on the command line like: * - * "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c" + * "sftp 192.168.0.1 user password file /tmp/storehere" */ #include "libssh2_config.h" @@ -77,10 +77,10 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *loclfile="sftp_write_nonblock.c"; - const char *sftppath="/tmp/sftp_write_nonblock.c"; + const char *username = "username"; + const char *password = "password"; + const char *loclfile = "sftp_write_nonblock.c"; + const char *sftppath = "/tmp/sftp_write_nonblock.c"; int rc; FILE *local; LIBSSH2_SFTP *sftp_session; @@ -96,40 +96,41 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } - if (argc > 4) { + if(argc > 4) { loclfile = argv[4]; } - if (argc > 5) { + if(argc > 5) { sftppath = argv[5]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } local = fopen(loclfile, "rb"); - if (!local) { + if(!local) { fprintf(stderr, "Can't open local file %s\n", loclfile); return -1; } @@ -143,7 +144,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -152,7 +153,7 @@ int main(int argc, char *argv[]) /* Create a session instance */ session = libssh2_session_init(); - if (!session) + if(!session) return -1; /* Since we have set non-blocking, tell libssh2 we are non-blocking */ @@ -161,9 +162,9 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) + while((rc = libssh2_session_handshake(session, sock)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; } @@ -180,22 +181,24 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == + while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - while ((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { +#define PUBKEY "/home/username/.ssh/id_rsa.pub" +#define PRIVKEY "/home/username/.ssh/id_rsa" + while((rc = libssh2_userauth_publickey_fromfile(session, username, + PUBKEY, PRIVKEY, + password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -205,28 +208,29 @@ int main(int argc, char *argv[]) do { sftp_session = libssh2_sftp_init(session); - if (!sftp_session && + if(!sftp_session && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } - } while (!sftp_session); + } while(!sftp_session); fprintf(stderr, "libssh2_sftp_open()!\n"); /* Request a file via SFTP */ do { sftp_handle = - libssh2_sftp_open(sftp_session, sftppath, - LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, - LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| - LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT| + LIBSSH2_FXF_TRUNC, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); - if (!sftp_handle && - (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { + if(!sftp_handle && + (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } - } while (!sftp_handle); + } while(!sftp_handle); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); @@ -235,9 +239,9 @@ int main(int argc, char *argv[]) memuse = 0; /* it starts blank */ do { nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local); - if (nread <= 0) { + if(nread <= 0) { /* end of file */ - if (memuse > 0) + if(memuse > 0) /* the previous sending is not finished */ nread = 0; else @@ -247,7 +251,7 @@ int main(int argc, char *argv[]) total += nread; /* write data in a loop until we block */ - while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) == + while((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); } @@ -263,7 +267,7 @@ int main(int argc, char *argv[]) /* 'mem' was consumed fully */ memuse = 0; - } while (rc > 0); + } while(rc > 0); duration = (int)(time(NULL)-start); @@ -277,8 +281,8 @@ int main(int argc, char *argv[]) shutdown: - while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing") - == LIBSSH2_ERROR_EAGAIN); + while(libssh2_session_disconnect(session, "Normal Shutdown") + == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftpdir.c b/example/sftpdir.c index 3f38b48..8fb16b1 100644 --- a/example/sftpdir.c +++ b/example/sftpdir.c @@ -42,10 +42,10 @@ #define __FILESIZE "llu" #endif -const char *keyfile1="~/.ssh/id_rsa.pub"; -const char *keyfile2="~/.ssh/id_rsa"; -const char *username="username"; -const char *password="password"; +const char *keyfile1 = "~/.ssh/id_rsa.pub"; +const char *keyfile2 = "~/.ssh/id_rsa"; +const char *username = "username"; +const char *password = "password"; static void kbd_callback(const char *name, int name_len, const char *instruction, int instruction_len, @@ -58,7 +58,7 @@ static void kbd_callback(const char *name, int name_len, (void)name_len; (void)instruction; (void)instruction_len; - if (num_prompts == 1) { + if(num_prompts == 1) { responses[0].text = strdup(password); responses[0].length = strlen(password); } @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) const char *fingerprint; char *userauthlist; LIBSSH2_SESSION *session; - const char *sftppath="/tmp/secretdir"; + const char *sftppath = "/tmp/secretdir"; LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP_HANDLE *sftp_handle; @@ -82,16 +82,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -105,9 +106,9 @@ int main(int argc, char *argv[]) sftppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -120,8 +121,8 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -156,58 +157,64 @@ int main(int argc, char *argv[]) /* check what authentication methods are available */ userauthlist = libssh2_userauth_list(session, username, strlen(username)); fprintf(stderr, "Authentication methods: %s\n", userauthlist); - if (strstr(userauthlist, "password") != NULL) { + if(strstr(userauthlist, "password") != NULL) { auth_pw |= 1; } - if (strstr(userauthlist, "keyboard-interactive") != NULL) { + if(strstr(userauthlist, "keyboard-interactive") != NULL) { auth_pw |= 2; } - if (strstr(userauthlist, "publickey") != NULL) { + if(strstr(userauthlist, "publickey") != NULL) { auth_pw |= 4; } /* if we got an 5. argument we set this option if supported */ if(argc > 5) { - if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { + if((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { auth_pw = 1; } - if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { + if((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { auth_pw = 2; } - if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { + if((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { auth_pw = 4; } } - if (auth_pw & 1) { + if(auth_pw & 1) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "\tAuthentication by password failed!\n"); goto shutdown; - } else { + } + else { fprintf(stderr, "\tAuthentication by password succeeded.\n"); } - } else if (auth_pw & 2) { + } + else if(auth_pw & 2) { /* Or via keyboard-interactive */ - if (libssh2_userauth_keyboard_interactive(session, username, - &kbd_callback) ) { + if(libssh2_userauth_keyboard_interactive(session, username, + &kbd_callback) ) { fprintf(stderr, - "\tAuthentication by keyboard-interactive failed!\n"); + "\tAuthentication by keyboard-interactive failed!\n"); goto shutdown; - } else { - fprintf(stderr, - "\tAuthentication by keyboard-interactive succeeded.\n"); } - } else if (auth_pw & 4) { + else { + fprintf(stderr, + "\tAuthentication by keyboard-interactive succeeded.\n"); + } + } + else if(auth_pw & 4) { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; - } else { + } + else { fprintf(stderr, "\tAuthentication by public key succeeded.\n"); } - } else { + } + else { fprintf(stderr, "No supported authentication methods found!\n"); goto shutdown; } @@ -215,7 +222,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_init()!\n"); sftp_session = libssh2_sftp_init(session); - if (!sftp_session) { + if(!sftp_session) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } @@ -227,7 +234,7 @@ int main(int argc, char *argv[]) /* Request a dir listing via SFTP */ sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); - if (!sftp_handle) { + if(!sftp_handle) { fprintf(stderr, "Unable to open dir with SFTP\n"); goto shutdown; } @@ -244,9 +251,10 @@ int main(int argc, char *argv[]) /* rc is the length of the file name in the mem buffer */ - if (longentry[0] != '\0') { + if(longentry[0] != '\0') { printf("%s\n", longentry); - } else { + } + else { if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { /* this should check what permissions it is and print the output accordingly */ @@ -273,14 +281,14 @@ int main(int argc, char *argv[]) else break; - } while (1); + } while(1); libssh2_sftp_closedir(sftp_handle); libssh2_sftp_shutdown(sftp_session); shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftpdir_nonblock.c b/example/sftpdir_nonblock.c index 64c868e..e9498d9 100644 --- a/example/sftpdir_nonblock.c +++ b/example/sftpdir_nonblock.c @@ -49,9 +49,11 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; - const char *username="username"; - const char *password="password"; - const char *sftppath="/tmp/secretdir"; + const char *username = "username"; + const char *password = "password"; + const char *sftppath = "/tmp/secretdir"; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; int rc; LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP_HANDLE *sftp_handle; @@ -60,16 +62,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -83,9 +86,9 @@ int main(int argc, char *argv[]) sftppath = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -98,8 +101,8 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -116,8 +119,8 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) == - LIBSSH2_ERROR_EAGAIN); + while((rc = libssh2_session_handshake(session, sock)) == + LIBSSH2_ERROR_EAGAIN); if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; @@ -135,21 +138,22 @@ int main(int argc, char *argv[]) } fprintf(stderr, "\n"); - if (auth_pw) { + if(auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else { + } + else { /* Or by public key */ - while ((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -159,24 +163,24 @@ int main(int argc, char *argv[]) do { sftp_session = libssh2_sftp_init(session); - if ((!sftp_session) && (libssh2_session_last_errno(session) != - LIBSSH2_ERROR_EAGAIN)) { + if((!sftp_session) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } - } while (!sftp_session); + } while(!sftp_session); fprintf(stderr, "libssh2_sftp_opendir()!\n"); /* Request a dir listing via SFTP */ do { sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); - if ((!sftp_handle) && (libssh2_session_last_errno(session) != - LIBSSH2_ERROR_EAGAIN)) { + if((!sftp_handle) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to open dir with SFTP\n"); goto shutdown; } - } while (!sftp_handle); + } while(!sftp_handle); fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n"); do { @@ -184,8 +188,8 @@ int main(int argc, char *argv[]) LIBSSH2_SFTP_ATTRIBUTES attrs; /* loop until we fail */ - while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem), - &attrs)) == LIBSSH2_ERROR_EAGAIN) { + while((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem), + &attrs)) == LIBSSH2_ERROR_EAGAIN) { ; } if(rc > 0) { @@ -196,13 +200,15 @@ int main(int argc, char *argv[]) /* this should check what permissions it is and print the output accordingly */ printf("--fix----- "); - } else { + } + else { printf("---------- "); } if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid); - } else { + } + else { printf(" - - "); } @@ -212,21 +218,22 @@ int main(int argc, char *argv[]) printf("%s\n", mem); } - else if (rc == LIBSSH2_ERROR_EAGAIN) { + else if(rc == LIBSSH2_ERROR_EAGAIN) { /* blocking */ fprintf(stderr, "Blocking\n"); - } else { + } + else { break; } - } while (1); + } while(1); libssh2_sftp_closedir(sftp_handle); libssh2_sftp_shutdown(sftp_session); shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/ssh2.c b/example/ssh2.c index f9ee68a..fa86f55 100644 --- a/example/ssh2.c +++ b/example/ssh2.c @@ -37,10 +37,10 @@ #include -const char *keyfile1="~/.ssh/id_rsa.pub"; -const char *keyfile2="~/.ssh/id_rsa"; -const char *username="username"; -const char *password="password"; +const char *keyfile1 = "~/.ssh/id_rsa.pub"; +const char *keyfile2 = "~/.ssh/id_rsa"; +const char *username = "username"; +const char *password = "password"; static void kbd_callback(const char *name, int name_len, @@ -54,7 +54,7 @@ static void kbd_callback(const char *name, int name_len, (void)name_len; (void)instruction; (void)instruction_len; - if (num_prompts == 1) { + if(num_prompts == 1) { responses[0].text = strdup(password); responses[0].length = strlen(password); } @@ -77,16 +77,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -97,9 +98,9 @@ int main(int argc, char *argv[]) password = argv[3]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -111,7 +112,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -121,7 +122,7 @@ int main(int argc, char *argv[]) * banners, exchange keys, and setup crypto, compression, and MAC layers */ session = libssh2_session_init(); - if (libssh2_session_handshake(session, sock)) { + if(libssh2_session_handshake(session, sock)) { fprintf(stderr, "Failure establishing SSH session\n"); return -1; } @@ -141,64 +142,71 @@ int main(int argc, char *argv[]) /* check what authentication methods are available */ userauthlist = libssh2_userauth_list(session, username, strlen(username)); fprintf(stderr, "Authentication methods: %s\n", userauthlist); - if (strstr(userauthlist, "password") != NULL) { + if(strstr(userauthlist, "password") != NULL) { auth_pw |= 1; } - if (strstr(userauthlist, "keyboard-interactive") != NULL) { + if(strstr(userauthlist, "keyboard-interactive") != NULL) { auth_pw |= 2; } - if (strstr(userauthlist, "publickey") != NULL) { + if(strstr(userauthlist, "publickey") != NULL) { auth_pw |= 4; } /* if we got an 4. argument we set this option if supported */ if(argc > 4) { - if ((auth_pw & 1) && !strcasecmp(argv[4], "-p")) { + if((auth_pw & 1) && !strcasecmp(argv[4], "-p")) { auth_pw = 1; } - if ((auth_pw & 2) && !strcasecmp(argv[4], "-i")) { + if((auth_pw & 2) && !strcasecmp(argv[4], "-i")) { auth_pw = 2; } - if ((auth_pw & 4) && !strcasecmp(argv[4], "-k")) { + if((auth_pw & 4) && !strcasecmp(argv[4], "-k")) { auth_pw = 4; } } - if (auth_pw & 1) { + if(auth_pw & 1) { /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "\tAuthentication by password failed!\n"); goto shutdown; - } else { + } + else { fprintf(stderr, "\tAuthentication by password succeeded.\n"); } - } else if (auth_pw & 2) { + } + else if(auth_pw & 2) { /* Or via keyboard-interactive */ - if (libssh2_userauth_keyboard_interactive(session, username, - &kbd_callback) ) { + if(libssh2_userauth_keyboard_interactive(session, username, + &kbd_callback) ) { fprintf(stderr, - "\tAuthentication by keyboard-interactive failed!\n"); + "\tAuthentication by keyboard-interactive failed!\n"); goto shutdown; - } else { - fprintf(stderr, - "\tAuthentication by keyboard-interactive succeeded.\n"); } - } else if (auth_pw & 4) { + else { + fprintf(stderr, + "\tAuthentication by keyboard-interactive succeeded.\n"); + } + } + else if(auth_pw & 4) { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; - } else { + } + else { fprintf(stderr, "\tAuthentication by public key succeeded.\n"); } - } else { + } + else { fprintf(stderr, "No supported authentication methods found!\n"); goto shutdown; } /* Request a shell */ - if (!(channel = libssh2_channel_open_session(session))) { + channel = libssh2_channel_open_session(session); + if(!channel) { fprintf(stderr, "Unable to open a session\n"); goto shutdown; } @@ -211,13 +219,13 @@ int main(int argc, char *argv[]) /* Request a terminal with 'vanilla' terminal emulation * See /etc/termcap for more options */ - if (libssh2_channel_request_pty(channel, "vanilla")) { + if(libssh2_channel_request_pty(channel, "vanilla")) { fprintf(stderr, "Failed requesting pty\n"); goto skip_shell; } /* Open a SHELL on that pty */ - if (libssh2_channel_shell(channel)) { + if(libssh2_channel_shell(channel)) { fprintf(stderr, "Unable to request shell on allocated pty\n"); goto shutdown; } @@ -236,7 +244,7 @@ int main(int argc, char *argv[]) */ skip_shell: - if (channel) { + if(channel) { libssh2_channel_free(channel); channel = NULL; } diff --git a/example/ssh2_agent.c b/example/ssh2_agent.c index 912b925..1cc5084 100644 --- a/example/ssh2_agent.c +++ b/example/ssh2_agent.c @@ -36,7 +36,7 @@ #include #include -const char *username="username"; +const char *username = "username"; int main(int argc, char *argv[]) { @@ -54,16 +54,17 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) { + if(argc > 1) { hostaddr = inet_addr(argv[1]); - } else { + } + else { hostaddr = htonl(0x7F000001); } @@ -71,9 +72,9 @@ int main(int argc, char *argv[]) username = argv[2]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -81,7 +82,7 @@ int main(int argc, char *argv[]) * responsible for creating the socket establishing the connection */ sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == -1) { + if(sock == -1) { fprintf(stderr, "failed to create socket!\n"); rc = 1; goto shutdown; @@ -90,7 +91,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); goto shutdown; @@ -100,7 +101,7 @@ int main(int argc, char *argv[]) * banners, exchange keys, and setup crypto, compression, and MAC layers */ session = libssh2_session_init(); - if (libssh2_session_handshake(session, sock)) { + if(libssh2_session_handshake(session, sock)) { fprintf(stderr, "Failure establishing SSH session\n"); return 1; } @@ -120,43 +121,44 @@ int main(int argc, char *argv[]) /* check what authentication methods are available */ userauthlist = libssh2_userauth_list(session, username, strlen(username)); fprintf(stderr, "Authentication methods: %s\n", userauthlist); - if (strstr(userauthlist, "publickey") == NULL) { + if(strstr(userauthlist, "publickey") == NULL) { fprintf(stderr, "\"publickey\" authentication is not supported\n"); goto shutdown; } /* Connect to the ssh-agent */ agent = libssh2_agent_init(session); - if (!agent) { + if(!agent) { fprintf(stderr, "Failure initializing ssh-agent support\n"); rc = 1; goto shutdown; } - if (libssh2_agent_connect(agent)) { + if(libssh2_agent_connect(agent)) { fprintf(stderr, "Failure connecting to ssh-agent\n"); rc = 1; goto shutdown; } - if (libssh2_agent_list_identities(agent)) { + if(libssh2_agent_list_identities(agent)) { fprintf(stderr, "Failure requesting identities to ssh-agent\n"); rc = 1; goto shutdown; } - while (1) { + while(1) { rc = libssh2_agent_get_identity(agent, &identity, prev_identity); - if (rc == 1) + if(rc == 1) break; - if (rc < 0) { + if(rc < 0) { fprintf(stderr, "Failure obtaining identity from ssh-agent support\n"); rc = 1; goto shutdown; } - if (libssh2_agent_userauth(agent, username, identity)) { + if(libssh2_agent_userauth(agent, username, identity)) { fprintf(stderr, "\tAuthentication with username %s and " "public key %s failed!\n", username, identity->comment); - } else { + } + else { fprintf(stderr, "\tAuthentication with username %s and " "public key %s succeeded!\n", username, identity->comment); @@ -164,7 +166,7 @@ int main(int argc, char *argv[]) } prev_identity = identity; } - if (rc) { + if(rc) { fprintf(stderr, "Couldn't continue authentication\n"); goto shutdown; } @@ -172,7 +174,8 @@ int main(int argc, char *argv[]) /* We're authenticated now. */ /* Request a shell */ - if (!(channel = libssh2_channel_open_session(session))) { + channel = libssh2_channel_open_session(session); + if(!channel) { fprintf(stderr, "Unable to open a session\n"); goto shutdown; } @@ -185,13 +188,13 @@ int main(int argc, char *argv[]) /* Request a terminal with 'vanilla' terminal emulation * See /etc/termcap for more options */ - if (libssh2_channel_request_pty(channel, "vanilla")) { + if(libssh2_channel_request_pty(channel, "vanilla")) { fprintf(stderr, "Failed requesting pty\n"); goto skip_shell; } /* Open a SHELL on that pty */ - if (libssh2_channel_shell(channel)) { + if(libssh2_channel_shell(channel)) { fprintf(stderr, "Unable to request shell on allocated pty\n"); goto shutdown; } @@ -210,7 +213,7 @@ int main(int argc, char *argv[]) */ skip_shell: - if (channel) { + if(channel) { libssh2_channel_free(channel); channel = NULL; } @@ -223,18 +226,18 @@ int main(int argc, char *argv[]) shutdown: - if (agent) { + if(agent) { libssh2_agent_disconnect(agent); libssh2_agent_free(agent); } - + if(session) { libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_free(session); } - if (sock != -1) { + if(sock != -1) { #ifdef WIN32 closesocket(sock); #else diff --git a/example/ssh2_echo.c b/example/ssh2_echo.c index 782930d..eae4165 100644 --- a/example/ssh2_echo.c +++ b/example/ssh2_echo.c @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) LIBSSH2_CHANNEL *channel; int rc; int exitcode = 0; - char *exitsignal=(char *)"none"; + char *exitsignal = (char *)"none"; size_t len; LIBSSH2_KNOWNHOSTS *nh; int type; @@ -96,27 +96,27 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) + if(argc > 1) /* must be ip address only */ hostname = argv[1]; - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -131,7 +131,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -139,7 +139,7 @@ int main(int argc, char *argv[]) /* Create a session instance */ session = libssh2_session_init(); - if (!session) + if(!session) return -1; /* tell libssh2 we want it all done non-blocking */ @@ -148,9 +148,9 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) == + while((rc = libssh2_session_handshake(session, sock)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; } @@ -193,11 +193,11 @@ int main(int argc, char *argv[]) } libssh2_knownhost_free(nh); - if ( strlen(password) != 0 ) { + if(strlen(password) != 0) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == - LIBSSH2_ERROR_EAGAIN); - if (rc) { + while((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); exit(1); } @@ -206,22 +206,22 @@ int main(int argc, char *argv[]) libssh2_trace(session, LIBSSH2_TRACE_SOCKET); /* Exec non-blocking on the remove host */ - while( (channel = libssh2_channel_open_session(session)) == NULL && - libssh2_session_last_error(session,NULL,NULL,0) == - LIBSSH2_ERROR_EAGAIN ) { + while((channel = libssh2_channel_open_session(session)) == NULL && + libssh2_session_last_error(session, NULL, NULL, 0) == + LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); } - if( channel == NULL ) { - fprintf(stderr,"Error\n"); - exit( 1 ); + if(channel == NULL) { + fprintf(stderr, "Error\n"); + exit(1); } - while( (rc = libssh2_channel_exec(channel, commandline)) == - LIBSSH2_ERROR_EAGAIN ) + while((rc = libssh2_channel_exec(channel, commandline)) == + LIBSSH2_ERROR_EAGAIN) waitsocket(sock, session); - if( rc != 0 ) { + if(rc != 0) { fprintf(stderr, "exec error\n"); - exit( 1 ); + exit(1); } else { LIBSSH2_POLLFD *fds = NULL; @@ -236,10 +236,11 @@ int main(int argc, char *argv[]) int rewrites = 0; int i; - for (i = 0; i < BUFSIZE; i++) + for(i = 0; i < BUFSIZE; i++) buffer[i] = 'A'; - if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { + fds = malloc(sizeof (LIBSSH2_POLLFD)); + if(!fds) { fprintf(stderr, "malloc failed\n"); exit(1); } @@ -252,18 +253,18 @@ int main(int argc, char *argv[]) int rc = (libssh2_poll(fds, 1, 10)); int act = 0; - if (rc < 1) + if(rc < 1) continue; - if (fds[0].revents & LIBSSH2_POLLFD_POLLIN) { + if(fds[0].revents & LIBSSH2_POLLFD_POLLIN) { int n = libssh2_channel_read(channel, buffer, sizeof(buffer)); act++; - if (n == LIBSSH2_ERROR_EAGAIN) { + if(n == LIBSSH2_ERROR_EAGAIN) { rereads++; fprintf(stderr, "will read again\n"); } - else if (n < 0) { + else if(n < 0) { fprintf(stderr, "read failed\n"); exit(1); } @@ -274,20 +275,20 @@ int main(int argc, char *argv[]) } } - if (fds[0].revents & LIBSSH2_POLLFD_POLLOUT) { + if(fds[0].revents & LIBSSH2_POLLFD_POLLOUT) { act++; - if (totwritten < totsize) { + if(totwritten < totsize) { /* we have not written all data yet */ int left = totsize - totwritten; int size = (left < bufsize) ? left : bufsize; int n = libssh2_channel_write_ex(channel, 0, buffer, size); - if (n == LIBSSH2_ERROR_EAGAIN) { + if(n == LIBSSH2_ERROR_EAGAIN) { rewrites++; fprintf(stderr, "will write again\n"); } - else if (n < 0) { + else if(n < 0) { fprintf(stderr, "write failed\n"); exit(1); } @@ -295,20 +296,21 @@ int main(int argc, char *argv[]) totwritten += n; fprintf(stderr, "wrote %d bytes (%d in total)", n, totwritten); - if (left >= bufsize && n != bufsize) { + if(left >= bufsize && n != bufsize) { partials++; fprintf(stderr, " PARTIAL"); } fprintf(stderr, "\n"); } - } else { + } + else { /* all data written, send EOF */ rc = libssh2_channel_send_eof(channel); - if (rc == LIBSSH2_ERROR_EAGAIN) { + if(rc == LIBSSH2_ERROR_EAGAIN) { fprintf(stderr, "will send eof again\n"); } - else if (rc < 0) { + else if(rc < 0) { fprintf(stderr, "send eof failed\n"); exit(1); } @@ -320,23 +322,23 @@ int main(int argc, char *argv[]) } } - if (fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) { - if (!act) /* don't leave loop until we have read all data */ + if(fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) { + if(!act) /* don't leave loop until we have read all data */ running = 0; } } while(running); exitcode = 127; - while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) + while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) waitsocket(sock, session); - if( rc == 0 ) { - exitcode = libssh2_channel_get_exit_status( channel ); + if(rc == 0) { + exitcode = libssh2_channel_get_exit_status(channel); libssh2_channel_get_exit_signal(channel, &exitsignal, NULL, NULL, NULL, NULL, NULL); } - if (exitsignal) + if(exitsignal) fprintf(stderr, "\nGot signal: %s\n", exitsignal); libssh2_channel_free(channel); @@ -345,7 +347,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "\nrereads: %d rewrites: %d totwritten %d\n", rereads, rewrites, totwritten); - if (totwritten != totread) { + if(totwritten != totread) { fprintf(stderr, "\n*** FAIL bytes written: %d bytes " "read: %d ***\n", totwritten, totread); exit(1); diff --git a/example/ssh2_exec.c b/example/ssh2_exec.c index c83f0bc..d33c6d9 100644 --- a/example/ssh2_exec.c +++ b/example/ssh2_exec.c @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) LIBSSH2_CHANNEL *channel; int rc; int exitcode; - char *exitsignal=(char *)"none"; + char *exitsignal = (char *)"none"; int bytecount = 0; size_t len; LIBSSH2_KNOWNHOSTS *nh; @@ -97,30 +97,30 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } #endif - if (argc > 1) + if(argc > 1) /* must be ip address only */ hostname = argv[1]; - if (argc > 2) { + if(argc > 2) { username = argv[2]; } - if (argc > 3) { + if(argc > 3) { password = argv[3]; } - if (argc > 4) { + if(argc > 4) { commandline = argv[4]; } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } @@ -135,7 +135,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -143,7 +143,7 @@ int main(int argc, char *argv[]) /* Create a session instance */ session = libssh2_session_init(); - if (!session) + if(!session) return -1; /* tell libssh2 we want it all done non-blocking */ @@ -152,9 +152,9 @@ int main(int argc, char *argv[]) /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_handshake(session, sock)) == + while((rc = libssh2_session_handshake(session, sock)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; } @@ -206,104 +206,95 @@ int main(int argc, char *argv[]) } libssh2_knownhost_free(nh); - if ( strlen(password) != 0 ) { + if(strlen(password) != 0) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == + while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } } else { /* Or by public key */ - while ((rc = libssh2_userauth_publickey_fromfile(session, username, + while((rc = libssh2_userauth_publickey_fromfile(session, username, "/home/user/" ".ssh/id_rsa.pub", "/home/user/" ".ssh/id_rsa", password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } } #if 0 - libssh2_trace(session, ~0 ); + libssh2_trace(session, ~0); #endif /* Exec non-blocking on the remove host */ - while( (channel = libssh2_channel_open_session(session)) == NULL && - libssh2_session_last_error(session,NULL,NULL,0) == - LIBSSH2_ERROR_EAGAIN ) - { + while((channel = libssh2_channel_open_session(session)) == NULL && + libssh2_session_last_error(session, NULL, NULL, 0) == + LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); } - if( channel == NULL ) - { - fprintf(stderr,"Error\n"); - exit( 1 ); + if(channel == NULL) { + fprintf(stderr, "Error\n"); + exit(1); } - while( (rc = libssh2_channel_exec(channel, commandline)) == - LIBSSH2_ERROR_EAGAIN ) - { + while((rc = libssh2_channel_exec(channel, commandline)) == + LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); } - if( rc != 0 ) - { - fprintf(stderr,"Error\n"); - exit( 1 ); + if(rc != 0) { + fprintf(stderr, "Error\n"); + exit(1); } - for( ;; ) - { + for(;;) { /* loop until we block */ int rc; - do - { + do { char buffer[0x4000]; - rc = libssh2_channel_read( channel, buffer, sizeof(buffer) ); - if( rc > 0 ) - { + rc = libssh2_channel_read(channel, buffer, sizeof(buffer) ); + if(rc > 0) { int i; bytecount += rc; fprintf(stderr, "We read:\n"); - for( i=0; i < rc; ++i ) - fputc( buffer[i], stderr); + for(i = 0; i < rc; ++i) + fputc(buffer[i], stderr); fprintf(stderr, "\n"); } else { - if( rc != LIBSSH2_ERROR_EAGAIN ) + if(rc != LIBSSH2_ERROR_EAGAIN) /* no need to output this for the EAGAIN case */ fprintf(stderr, "libssh2_channel_read returned %d\n", rc); } } - while( rc > 0 ); + while(rc > 0); /* this is due to blocking that would occur otherwise so we loop on this condition */ - if( rc == LIBSSH2_ERROR_EAGAIN ) - { + if(rc == LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); } else break; } exitcode = 127; - while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) + while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) waitsocket(sock, session); - if( rc == 0 ) - { - exitcode = libssh2_channel_get_exit_status( channel ); + if(rc == 0) { + exitcode = libssh2_channel_get_exit_status(channel); libssh2_channel_get_exit_signal(channel, &exitsignal, NULL, NULL, NULL, NULL, NULL); } - if (exitsignal) + if(exitsignal) fprintf(stderr, "\nGot signal: %s\n", exitsignal); - else + else fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount); libssh2_channel_free(channel); diff --git a/example/subsystem_netconf.c b/example/subsystem_netconf.c index 82c4941..cef25fe 100644 --- a/example/subsystem_netconf.c +++ b/example/subsystem_netconf.c @@ -57,12 +57,12 @@ static int netconf_write(LIBSSH2_CHANNEL *channel, const char *buf, size_t len) do { i = libssh2_channel_write(channel, buf, len); - if (i < 0) { + if(i < 0) { fprintf(stderr, "libssh2_channel_write: %d\n", i); return -1; } wr += i; - } while (i > 0 && wr < (ssize_t)len); + } while(i > 0 && wr < (ssize_t)len); return 0; } @@ -78,9 +78,9 @@ static int netconf_read_until(LIBSSH2_CHANNEL *channel, const char *endtag, do { len = libssh2_channel_read(channel, buf + rd, buflen - rd); - if (LIBSSH2_ERROR_EAGAIN == len) + if(LIBSSH2_ERROR_EAGAIN == len) continue; - else if (len < 0) { + else if(len < 0) { fprintf(stderr, "libssh2_channel_read: %d\n", (int)len); return -1; } @@ -92,13 +92,14 @@ static int netconf_read_until(LIBSSH2_CHANNEL *channel, const char *endtag, /* really, this MUST be replaced with proper XML parsing! */ endreply = strstr(buf, endtag); - if (endreply) + if(endreply) specialsequence = strstr(endreply, "]]>]]>"); - } while (!specialsequence && rd < buflen); + } while(!specialsequence && rd < buflen); - if (!specialsequence) { - fprintf(stderr, "%s: ]]>]]> not found! read buffer too small?\n", __func__); + if(!specialsequence) { + fprintf(stderr, "%s: ]]>]]> not found! read buffer too small?\n", + __func__); return -1; } @@ -125,8 +126,8 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } @@ -134,40 +135,41 @@ int main(int argc, char *argv[]) int sock = -1; #endif - if (argc > 1) + if(argc > 1) server_ip = argv[1]; - if (argc > 2) + if(argc > 2) username = argv[2]; - if (argc > 3) + if(argc > 3) password = argv[3]; - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } /* Connect to SSH server */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #ifdef WIN32 - if (sock == INVALID_SOCKET) { + if(sock == INVALID_SOCKET) { fprintf(stderr, "failed to open socket!\n"); return -1; } #else - if (sock == -1) { + if(sock == -1) { perror("socket"); return -1; } #endif sin.sin_family = AF_INET; - if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { + sin.sin_addr.s_addr = inet_addr(server_ip); + if(INADDR_NONE == sin.sin_addr.s_addr) { fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip); return -1; } sin.sin_port = htons(830); - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr)); return -1; @@ -203,39 +205,41 @@ int main(int argc, char *argv[]) /* check what authentication methods are available */ userauthlist = libssh2_userauth_list(session, username, strlen(username)); fprintf(stderr, "Authentication methods: %s\n", userauthlist); - if (strstr(userauthlist, "password")) + if(strstr(userauthlist, "password")) auth |= AUTH_PASSWORD; - if (strstr(userauthlist, "publickey")) + if(strstr(userauthlist, "publickey")) auth |= AUTH_PUBLICKEY; /* check for options */ if(argc > 4) { - if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[4], "-p")) + if((auth & AUTH_PASSWORD) && !strcasecmp(argv[4], "-p")) auth = AUTH_PASSWORD; - if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[4], "-k")) + if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[4], "-k")) auth = AUTH_PUBLICKEY; } - if (auth & AUTH_PASSWORD) { - if (libssh2_userauth_password(session, username, password)) { + if(auth & AUTH_PASSWORD) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else if (auth & AUTH_PUBLICKEY) { - if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, + } + else if(auth & AUTH_PUBLICKEY) { + if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) { fprintf(stderr, "Authentication by public key failed!\n"); goto shutdown; } fprintf(stderr, "Authentication by public key succeeded.\n"); - } else { + } + else { fprintf(stderr, "No supported authentication methods found!\n"); goto shutdown; } /* open a channel */ channel = libssh2_channel_open_session(session); - if (!channel) { + if(!channel) { fprintf(stderr, "Could not open the channel!\n" "(Note that this can be a problem at the server!" " Please review the server logs.)\n"); @@ -243,7 +247,7 @@ int main(int argc, char *argv[]) } /* execute the subsystem on our channel */ - if (libssh2_channel_subsystem(channel, "netconf")) { + if(libssh2_channel_subsystem(channel, "netconf")) { fprintf(stderr, "Could not execute the \"netconf\" subsystem!\n" "(Note that this can be a problem at the server!" " Please review the server logs.)\n"); @@ -261,15 +265,16 @@ int main(int argc, char *argv[]) "" "\n" "]]>]]>\n%n", (int *)&len); - if (-1 == netconf_write(channel, buf, len)) + if(-1 == netconf_write(channel, buf, len)) goto shutdown; fprintf(stderr, "Reading NETCONF server \n"); len = netconf_read_until(channel, "", buf, sizeof(buf)); - if (-1 == len) + if(-1 == len) goto shutdown; - fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf); + fprintf(stderr, "Got %d bytes:\n----------------------\n%s", + (int)len, buf); fprintf(stderr, "Sending NETCONF \n"); snprintf(buf, sizeof(buf), @@ -278,18 +283,19 @@ int main(int argc, char *argv[]) "" "\n" "]]>]]>\n%n", (int *)&len); - if (-1 == netconf_write(channel, buf, len)) + if(-1 == netconf_write(channel, buf, len)) goto shutdown; fprintf(stderr, "Reading NETCONF \n"); len = netconf_read_until(channel, "", buf, sizeof(buf)); - if (-1 == len) + if(-1 == len) goto shutdown; - fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf); + fprintf(stderr, "Got %d bytes:\n----------------------\n%s", + (int)len, buf); shutdown: - if (channel) + if(channel) libssh2_channel_free(channel); libssh2_session_disconnect(session, "Client disconnecting normally"); libssh2_session_free(session); diff --git a/example/tcpip-forward.c b/example/tcpip-forward.c index 2351368..51ca17a 100644 --- a/example/tcpip-forward.c +++ b/example/tcpip-forward.c @@ -70,8 +70,8 @@ int main(int argc, char *argv[]) WSADATA wsadata; int err; - err = WSAStartup(MAKEWORD(2,0), &wsadata); - if (err != 0) { + err = WSAStartup(MAKEWORD(2, 0), &wsadata); + if(err != 0) { fprintf(stderr, "WSAStartup failed with error: %d\n", err); return 1; } @@ -79,48 +79,49 @@ int main(int argc, char *argv[]) int sock = -1, forwardsock = -1; #endif - if (argc > 1) + if(argc > 1) server_ip = argv[1]; - if (argc > 2) + if(argc > 2) username = argv[2]; - if (argc > 3) + if(argc > 3) password = argv[3]; - if (argc > 4) + if(argc > 4) remote_listenhost = argv[4]; - if (argc > 5) + if(argc > 5) remote_wantport = atoi(argv[5]); - if (argc > 6) + if(argc > 6) local_destip = argv[6]; - if (argc > 7) + if(argc > 7) local_destport = atoi(argv[7]); - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } /* Connect to SSH server */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #ifdef WIN32 - if (sock == INVALID_SOCKET) { + if(sock == INVALID_SOCKET) { fprintf(stderr, "failed to open socket!\n"); return -1; } #else - if (sock == -1) { + if(sock == -1) { perror("socket"); return -1; } #endif sin.sin_family = AF_INET; - if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { + sin.sin_addr.s_addr = inet_addr(server_ip); + if(INADDR_NONE == sin.sin_addr.s_addr) { perror("inet_addr"); return -1; } sin.sin_port = htons(22); - if (connect(sock, (struct sockaddr*)(&sin), + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -156,32 +157,34 @@ int main(int argc, char *argv[]) /* check what authentication methods are available */ userauthlist = libssh2_userauth_list(session, username, strlen(username)); fprintf(stderr, "Authentication methods: %s\n", userauthlist); - if (strstr(userauthlist, "password")) + if(strstr(userauthlist, "password")) auth |= AUTH_PASSWORD; - if (strstr(userauthlist, "publickey")) + if(strstr(userauthlist, "publickey")) auth |= AUTH_PUBLICKEY; /* check for options */ if(argc > 8) { - if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) + if((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) auth = AUTH_PASSWORD; - if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) + if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) auth = AUTH_PUBLICKEY; } - if (auth & AUTH_PASSWORD) { - if (libssh2_userauth_password(session, username, password)) { + if(auth & AUTH_PASSWORD) { + if(libssh2_userauth_password(session, username, password)) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } - } else if (auth & AUTH_PUBLICKEY) { - if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + } + else if(auth & AUTH_PUBLICKEY) { + if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } fprintf(stderr, "\tAuthentication by public key succeeded.\n"); - } else { + } + else { fprintf(stderr, "No supported authentication methods found!\n"); goto shutdown; } @@ -191,7 +194,7 @@ int main(int argc, char *argv[]) listener = libssh2_channel_forward_listen_ex(session, remote_listenhost, remote_wantport, &remote_listenport, 1); - if (!listener) { + if(!listener) { fprintf(stderr, "Could not start the tcpip-forward listener!\n" "(Note that this can be a problem at the server!" " Please review the server logs.)\n"); @@ -203,7 +206,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Waiting for remote connection\n"); channel = libssh2_channel_forward_accept(listener); - if (!channel) { + if(!channel) { fprintf(stderr, "Could not accept connection!\n" "(Note that this can be a problem at the server!" " Please review the server logs.)\n"); @@ -215,12 +218,12 @@ int main(int argc, char *argv[]) local_destip, local_destport); forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #ifdef WIN32 - if (forwardsock == INVALID_SOCKET) { + if(forwardsock == INVALID_SOCKET) { fprintf(stderr, "failed to open forward socket!\n"); goto shutdown; } #else - if (forwardsock == -1) { + if(forwardsock == -1) { perror("socket"); goto shutdown; } @@ -228,11 +231,12 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(local_destport); - if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_destip))) { + sin.sin_addr.s_addr = inet_addr(local_destip); + if(INADDR_NONE == sin.sin_addr.s_addr) { perror("inet_addr"); goto shutdown; } - if (-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) { + if(-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) { perror("connect"); goto shutdown; } @@ -243,22 +247,23 @@ int main(int argc, char *argv[]) /* Must use non-blocking IO hereafter due to the current libssh2 API */ libssh2_session_set_blocking(session, 0); - while (1) { + while(1) { FD_ZERO(&fds); FD_SET(forwardsock, &fds); tv.tv_sec = 0; tv.tv_usec = 100000; rc = select(forwardsock + 1, &fds, NULL, NULL, &tv); - if (-1 == rc) { + if(-1 == rc) { perror("select"); goto shutdown; } - if (rc && FD_ISSET(forwardsock, &fds)) { + if(rc && FD_ISSET(forwardsock, &fds)) { len = recv(forwardsock, buf, sizeof(buf), 0); - if (len < 0) { + if(len < 0) { perror("read"); goto shutdown; - } else if (0 == len) { + } + else if(0 == len) { fprintf(stderr, "The local server at %s:%d disconnected!\n", local_destip, local_destport); goto shutdown; @@ -266,31 +271,31 @@ int main(int argc, char *argv[]) wr = 0; do { i = libssh2_channel_write(channel, buf, len); - if (i < 0) { + if(i < 0) { fprintf(stderr, "libssh2_channel_write: %d\n", i); goto shutdown; } wr += i; } while(i > 0 && wr < len); } - while (1) { + while(1) { len = libssh2_channel_read(channel, buf, sizeof(buf)); - if (LIBSSH2_ERROR_EAGAIN == len) + if(LIBSSH2_ERROR_EAGAIN == len) break; - else if (len < 0) { + else if(len < 0) { fprintf(stderr, "libssh2_channel_read: %d", (int)len); goto shutdown; } wr = 0; - while (wr < len) { + while(wr < len) { i = send(forwardsock, buf + wr, len - wr, 0); - if (i <= 0) { + if(i <= 0) { perror("write"); goto shutdown; } wr += i; } - if (libssh2_channel_eof(channel)) { + if(libssh2_channel_eof(channel)) { fprintf(stderr, "The remote client at %s:%d disconnected!\n", remote_listenhost, remote_listenport); goto shutdown; @@ -304,9 +309,9 @@ shutdown: #else close(forwardsock); #endif - if (channel) + if(channel) libssh2_channel_free(channel); - if (listener) + if(listener) libssh2_channel_forward_cancel(listener); libssh2_session_disconnect(session, "Client disconnecting normally"); libssh2_session_free(session); diff --git a/example/x11.c b/example/x11.c index 5c8c777..c49b64e 100644 --- a/example/x11.c +++ b/example/x11.c @@ -48,14 +48,14 @@ static void remove_node(struct chan_X11_list *elem) current_node = gp_x11_chan; - if (gp_x11_chan == elem) { + if(gp_x11_chan == elem) { gp_x11_chan = gp_x11_chan->next; free(current_node); return; } - while (current_node->next != NULL) { - if (current_node->next == elem) { + while(current_node->next != NULL) { + if(current_node->next == elem) { current_node->next = current_node->next->next; current_node = current_node->next; free(current_node); @@ -78,7 +78,7 @@ static int _raw_mode(void) struct termios tio; rc = tcgetattr(fileno(stdin), &tio); - if (rc != -1) { + if(rc != -1) { _saved_tio = tio; /* do the equivalent of cfmakeraw() manually, to build on Solaris */ tio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); @@ -106,9 +106,9 @@ static int _normal_mode(void) static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, char *shost, int sport, void **abstract) { - const char * display = NULL; - char * ptr = NULL; - char * temp_buff = NULL; + const char *display = NULL; + char *ptr = NULL; + char *temp_buff = NULL; int display_port = 0; int sock = 0; int rc = 0; @@ -124,22 +124,22 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, * Inspired by x11_connect_display in openssh */ display = getenv("DISPLAY"); - if ( display != NULL) { - if (strncmp( display, "unix:", 5) == 0 || + if(display != NULL) { + if(strncmp(display, "unix:", 5) == 0 || display[0] == ':') { /* Connect to the local unix domain */ ptr = strrchr(display, ':'); - temp_buff = (char *) calloc(strlen(ptr+1), sizeof(char)); - if (!temp_buff) { + temp_buff = (char *) calloc(strlen(ptr + 1), sizeof(char)); + if(!temp_buff) { perror("calloc"); return; } - memcpy(temp_buff, ptr+1, strlen(ptr+1)); + memcpy(temp_buff, ptr + 1, strlen(ptr + 1)); display_port = atoi(temp_buff); free(temp_buff); sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock < 0) + if(sock < 0) return; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; @@ -147,9 +147,9 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, _PATH_UNIX_X, display_port); rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); - if (rc != -1){ + if(rc != -1) { /* Connection Successfull */ - if (gp_x11_chan == NULL) { + if(gp_x11_chan == NULL) { /* Calloc ensure that gp_X11_chan is full of 0 */ gp_x11_chan = (struct chan_X11_list *) calloc(1, sizeof(struct chan_X11_list)); @@ -159,7 +159,7 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, } else { chan_iter = gp_x11_chan; - while (chan_iter->next != NULL) + while(chan_iter->next != NULL) chan_iter = chan_iter->next; /* Create the new Node */ new = (struct chan_X11_list *) @@ -183,10 +183,10 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, */ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) { - char * buf = NULL; - int bufsize = 8192; - int rc = 0; - int nfds = 1; + char *buf = NULL; + int bufsize = 8192; + int rc = 0; + int nfds = 1; LIBSSH2_POLLFD *fds = NULL; fd_set set; struct timeval timeval_out; @@ -195,12 +195,14 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) FD_ZERO(&set); - FD_SET(sock,&set); + FD_SET(sock, &set); - if ((buf = calloc (bufsize, sizeof(char))) == NULL) + buf = calloc(bufsize, sizeof(char)); + if(!buf) return 0; - if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { + fds = malloc(sizeof (LIBSSH2_POLLFD)); + if(!fds) { free(buf); return 0; } @@ -211,18 +213,18 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) fds[0].revents = LIBSSH2_POLLFD_POLLIN; rc = libssh2_poll(fds, nfds, 0); - if (rc >0) { + if(rc >0) { rc = libssh2_channel_read(channel, buf, bufsize); write(sock, buf, rc); } - rc = select(sock+1, &set, NULL, NULL, &timeval_out); - if (rc > 0) { + rc = select(sock + 1, &set, NULL, NULL, &timeval_out); + if(rc > 0) { memset((void *)buf, 0, bufsize); /* Data in sock*/ rc = read(sock, buf, bufsize); - if (rc > 0) { + if(rc > 0) { libssh2_channel_write(channel, buf, rc); } else { @@ -233,7 +235,7 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) free(fds); free(buf); - if (libssh2_channel_eof(channel) == 1) { + if(libssh2_channel_eof(channel) == 1) { return -1; } return 0; @@ -273,10 +275,10 @@ main (int argc, char *argv[]) timeval_out.tv_usec = 10; - if (argc > 3) { - hostaddr = inet_addr(argv[1]); - username = argv[2]; - password = argv[3]; + if(argc > 3) { + hostaddr = inet_addr(argv[1]); + username = argv[2]; + password = argv[3]; } else { fprintf(stderr, "Usage: %s destination username password", @@ -284,42 +286,42 @@ main (int argc, char *argv[]) return -1; } - if (argc > 4) { + if(argc > 4) { set_debug_on = 1; - fprintf (stderr, "DEBUG is ON: %d\n", set_debug_on); + fprintf(stderr, "DEBUG is ON: %d\n", set_debug_on); } - rc = libssh2_init (0); - if (rc != 0) { - fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); + rc = libssh2_init(0); + if(rc != 0) { + fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); return 1; } - sock = socket (AF_INET, SOCK_STREAM, 0); - if (sock == -1) { + sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock == -1) { perror("socket"); return -1; } sin.sin_family = AF_INET; - sin.sin_port = htons (22); + sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; rc = connect(sock, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)); - if (rc != 0) { - fprintf (stderr, "Failed to established connection!\n"); + if(rc != 0) { + fprintf(stderr, "Failed to established connection!\n"); return -1; } /* Open a session */ session = libssh2_session_init(); rc = libssh2_session_handshake(session, sock); - if (rc != 0) { + if(rc != 0) { fprintf(stderr, "Failed Start the SSH session\n"); return -1; } - if (set_debug_on == 1) + if(set_debug_on == 1) libssh2_trace(session, LIBSSH2_TRACE_CONN); /* ignore pedantic warnings by gcc on the callback argument */ @@ -332,7 +334,7 @@ main (int argc, char *argv[]) /* Authenticate via password */ rc = libssh2_userauth_password(session, username, password); - if (rc != 0) { + if(rc != 0) { fprintf(stderr, "Failed to authenticate\n"); session_shutdown(session); close(sock); @@ -341,7 +343,7 @@ main (int argc, char *argv[]) /* Open a channel */ channel = libssh2_channel_open_session(session); - if ( channel == NULL ) { + if(channel == NULL) { fprintf(stderr, "Failed to open a new channel\n"); session_shutdown(session); close(sock); @@ -350,8 +352,8 @@ main (int argc, char *argv[]) /* Request a PTY */ - rc = libssh2_channel_request_pty( channel, "xterm"); - if (rc != 0) { + rc = libssh2_channel_request_pty(channel, "xterm"); + if(rc != 0) { fprintf(stderr, "Failed to request a pty\n"); session_shutdown(session); close(sock); @@ -359,8 +361,8 @@ main (int argc, char *argv[]) } /* Request X11 */ - rc = libssh2_channel_x11_req(channel,0); - if(rc!=0) { + rc = libssh2_channel_x11_req(channel, 0); + if(rc != 0) { fprintf(stderr, "Failed to request X11 forwarding\n"); session_shutdown(session); close(sock); @@ -369,7 +371,7 @@ main (int argc, char *argv[]) /* Request a shell */ rc = libssh2_channel_shell(channel); - if (rc!=0) { + if(rc != 0) { fprintf(stderr, "Failed to open a shell\n"); session_shutdown(session); close(sock); @@ -377,7 +379,7 @@ main (int argc, char *argv[]) } rc = _raw_mode(); - if (rc != 0) { + if(rc != 0) { fprintf(stderr, "Failed to entered in raw mode\n"); session_shutdown(session); close(sock); @@ -387,15 +389,15 @@ main (int argc, char *argv[]) memset(&w_size, 0, sizeof(struct winsize)); memset(&w_size_bck, 0, sizeof(struct winsize)); - while (1) { + while(1) { FD_ZERO(&set); - FD_SET(fileno(stdin),&set); + FD_SET(fileno(stdin), &set); /* Search if a resize pty has to be send */ ioctl(fileno(stdin), TIOCGWINSZ, &w_size); - if ((w_size.ws_row != w_size_bck.ws_row) || - (w_size.ws_col != w_size_bck.ws_col)) { + if((w_size.ws_row != w_size_bck.ws_row) || + (w_size.ws_col != w_size_bck.ws_col)) { w_size_bck = w_size; libssh2_channel_request_pty_size(channel, @@ -403,10 +405,12 @@ main (int argc, char *argv[]) w_size.ws_row); } - if ((buf = calloc (bufsiz, sizeof(char))) == NULL) + buf = calloc(bufsiz, sizeof(char)); + if(buf == NULL) break; - if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { + fds = malloc(sizeof (LIBSSH2_POLLFD)); + if(fds == NULL) { free(buf); break; } @@ -417,25 +421,25 @@ main (int argc, char *argv[]) fds[0].revents = LIBSSH2_POLLFD_POLLIN; rc = libssh2_poll(fds, nfds, 0); - if (rc >0) { + if(rc >0) { libssh2_channel_read(channel, buf, sizeof(buf)); fprintf(stdout, "%s", buf); fflush(stdout); } /* Looping on X clients */ - if (gp_x11_chan != NULL) { + if(gp_x11_chan != NULL) { current_node = gp_x11_chan; } else current_node = NULL; - while (current_node != NULL) { + while(current_node != NULL) { struct chan_X11_list *next_node; rc = x11_send_receive(current_node->chan, current_node->sock); next_node = current_node->next; - if (rc == -1){ - shutdown(current_node->sock,SHUT_RDWR); + if(rc == -1) { + shutdown(current_node->sock, SHUT_RDWR); close(current_node->sock); remove_node(current_node); } @@ -444,25 +448,25 @@ main (int argc, char *argv[]) } - rc = select(fileno(stdin)+1,&set,NULL,NULL,&timeval_out); - if (rc > 0) { + rc = select(fileno(stdin) + 1, &set, NULL, NULL, &timeval_out); + if(rc > 0) { /* Data in stdin*/ - rc = read(fileno(stdin), buf,1); - if (rc > 0) - libssh2_channel_write(channel,buf, sizeof(buf)); + rc = read(fileno(stdin), buf, 1); + if(rc > 0) + libssh2_channel_write(channel, buf, sizeof(buf)); } - free (fds); - free (buf); + free(fds); + free(buf); - if (libssh2_channel_eof (channel) == 1) { - break; + if(libssh2_channel_eof (channel) == 1) { + break; } } - if (channel) { - libssh2_channel_free (channel); - channel = NULL; + if(channel) { + libssh2_channel_free(channel); + channel = NULL; } _normal_mode(); diff --git a/include/libssh2.h b/include/libssh2.h index c17c30c..a4fd0c8 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -213,7 +213,8 @@ typedef off_t libssh2_struct_stat_size; #ifndef LIBSSH2_STRUCT_STAT_SIZE_FORMAT # ifdef __VMS -/* We have to roll our own format here because %z is a C99-ism we don't have. */ +/* We have to roll our own format here because %z is a C99-ism we don't + have. */ # if __USE_OFF64_T || __USING_STD_STAT # define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%Ld" # else @@ -229,11 +230,11 @@ typedef off_t libssh2_struct_stat_size; /* Part of every banner, user specified or not */ #define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION -/* We *could* add a comment here if we so chose */ -#define LIBSSH2_SSH_DEFAULT_BANNER LIBSSH2_SSH_BANNER -#define LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF LIBSSH2_SSH_DEFAULT_BANNER "\r\n" +#define LIBSSH2_SSH_DEFAULT_BANNER LIBSSH2_SSH_BANNER +#define LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF LIBSSH2_SSH_DEFAULT_BANNER "\r\n" -/* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */ +/* Default generate and safe prime sizes for + diffie-hellman-group-exchange-sha1 */ #define LIBSSH2_DH_GEX_MINGROUP 1024 #define LIBSSH2_DH_GEX_OPTGROUP 1536 #define LIBSSH2_DH_GEX_MAXGROUP 2048 @@ -269,14 +270,14 @@ typedef off_t libssh2_struct_stat_size; typedef struct _LIBSSH2_USERAUTH_KBDINT_PROMPT { - char* text; + char *text; unsigned int length; unsigned char echo; } LIBSSH2_USERAUTH_KBDINT_PROMPT; typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE { - char* text; + char *text; unsigned int length; } LIBSSH2_USERAUTH_KBDINT_RESPONSE; @@ -287,10 +288,10 @@ typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE /* 'keyboard-interactive' authentication callback */ #define LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(name_) \ - void name_(const char* name, int name_len, const char* instruction, \ + void name_(const char *name, int name_len, const char *instruction, \ int instruction_len, int num_prompts, \ - const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, \ - LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, void **abstract) + const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, \ + LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, void **abstract) /* Callbacks for special SSH packets */ #define LIBSSH2_IGNORE_FUNC(name) \ @@ -324,12 +325,14 @@ typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE LIBSSH2_CHANNEL *channel, void **channel_abstract) /* I/O callbacks */ -#define LIBSSH2_RECV_FUNC(name) ssize_t name(libssh2_socket_t socket, \ - void *buffer, size_t length, \ - int flags, void **abstract) -#define LIBSSH2_SEND_FUNC(name) ssize_t name(libssh2_socket_t socket, \ - const void *buffer, size_t length,\ - int flags, void **abstract) +#define LIBSSH2_RECV_FUNC(name) \ + ssize_t name(libssh2_socket_t socket, \ + void *buffer, size_t length, \ + int flags, void **abstract) +#define LIBSSH2_SEND_FUNC(name) \ + ssize_t name(libssh2_socket_t socket, \ + const void *buffer, size_t length, \ + int flags, void **abstract) /* libssh2_session_callback_set() constants */ #define LIBSSH2_CALLBACK_IGNORE 0 @@ -468,7 +471,8 @@ typedef struct _LIBSSH2_POLLFD { #define LIBSSH2_ERROR_FILE -16 #define LIBSSH2_ERROR_METHOD_NONE -17 #define LIBSSH2_ERROR_AUTHENTICATION_FAILED -18 -#define LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED LIBSSH2_ERROR_AUTHENTICATION_FAILED +#define LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED \ + LIBSSH2_ERROR_AUTHENTICATION_FAILED #define LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED -19 #define LIBSSH2_ERROR_CHANNEL_OUTOFORDER -20 #define LIBSSH2_ERROR_CHANNEL_FAILURE -21 @@ -548,7 +552,7 @@ LIBSSH2_API void libssh2_free(LIBSSH2_SESSION *session, void *ptr); */ LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session, int method_type, - const char*** algs); + const char ***algs); /* Session API */ LIBSSH2_API LIBSSH2_SESSION * @@ -596,7 +600,7 @@ LIBSSH2_API int libssh2_session_last_error(LIBSSH2_SESSION *session, LIBSSH2_API int libssh2_session_last_errno(LIBSSH2_SESSION *session); LIBSSH2_API int libssh2_session_set_last_error(LIBSSH2_SESSION* session, int errcode, - const char* errmsg); + const char *errmsg); LIBSSH2_API int libssh2_session_block_directions(LIBSSH2_SESSION *session); LIBSSH2_API int libssh2_session_flag(LIBSSH2_SESSION *session, int flag, @@ -609,12 +613,14 @@ LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session, unsigned int username_len); LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session); -LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session, - const char *username, - unsigned int username_len, - const char *password, - unsigned int password_len, - LIBSSH2_PASSWD_CHANGEREQ_FUNC((*passwd_change_cb))); +LIBSSH2_API int +libssh2_userauth_password_ex(LIBSSH2_SESSION *session, + const char *username, + unsigned int username_len, + const char *password, + unsigned int password_len, + LIBSSH2_PASSWD_CHANGEREQ_FUNC + ((*passwd_change_cb))); #define libssh2_userauth_password(session, username, password) \ libssh2_userauth_password_ex((session), (username), \ @@ -641,7 +647,8 @@ libssh2_userauth_publickey(LIBSSH2_SESSION *session, const char *username, const unsigned char *pubkeydata, size_t pubkeydata_len, - LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)), + LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC + ((*sign_callback)), void **abstract); LIBSSH2_API int @@ -733,7 +740,8 @@ libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION *session, const char *host, LIBSSH2_API LIBSSH2_LISTENER * libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, const char *host, - int port, int *bound_port, int queue_maxsize); + int port, int *bound_port, + int queue_maxsize); #define libssh2_channel_forward_listen(session, port) \ libssh2_channel_forward_listen_ex((session), NULL, (port), NULL, 16) @@ -764,15 +772,17 @@ LIBSSH2_API int libssh2_channel_request_pty_ex(LIBSSH2_CHANNEL *channel, libssh2_channel_request_pty_ex((channel), (term), \ (unsigned int)strlen(term), \ NULL, 0, \ - LIBSSH2_TERM_WIDTH, LIBSSH2_TERM_HEIGHT, \ - LIBSSH2_TERM_WIDTH_PX, LIBSSH2_TERM_HEIGHT_PX) + LIBSSH2_TERM_WIDTH, \ + LIBSSH2_TERM_HEIGHT, \ + LIBSSH2_TERM_WIDTH_PX, \ + LIBSSH2_TERM_HEIGHT_PX) LIBSSH2_API int libssh2_channel_request_pty_size_ex(LIBSSH2_CHANNEL *channel, int width, int height, int width_px, int height_px); #define libssh2_channel_request_pty_size(channel, width, height) \ - libssh2_channel_request_pty_size_ex( (channel), (width), (height), 0, 0) + libssh2_channel_request_pty_size_ex((channel), (width), (height), 0, 0) LIBSSH2_API int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel, int single_connection, @@ -834,8 +844,9 @@ LIBSSH2_API ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel, #define libssh2_channel_write(channel, buf, buflen) \ libssh2_channel_write_ex((channel), 0, (buf), (buflen)) -#define libssh2_channel_write_stderr(channel, buf, buflen) \ - libssh2_channel_write_ex((channel), SSH_EXTENDED_DATA_STDERR, (buf), (buflen)) +#define libssh2_channel_write_stderr(channel, buf, buflen) \ + libssh2_channel_write_ex((channel), SSH_EXTENDED_DATA_STDERR, \ + (buf), (buflen)) LIBSSH2_API unsigned long libssh2_channel_window_write_ex(LIBSSH2_CHANNEL *channel, @@ -872,7 +883,7 @@ LIBSSH2_API int libssh2_channel_handle_extended_data2(LIBSSH2_CHANNEL *channel, libssh2_channel_handle_extended_data((channel), \ (ignore) ? \ LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE : \ - LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL ) + LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL) #define LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA -1 #define LIBSSH2_CHANNEL_FLUSH_ALL -2 @@ -1268,9 +1279,9 @@ libssh2_agent_free(LIBSSH2_AGENT *agent); * Note that non-blocking applications are responsible for sending the * keepalive messages using libssh2_keepalive_send(). */ -LIBSSH2_API void libssh2_keepalive_config (LIBSSH2_SESSION *session, - int want_reply, - unsigned interval); +LIBSSH2_API void libssh2_keepalive_config(LIBSSH2_SESSION *session, + int want_reply, + unsigned interval); /* * libssh2_keepalive_send() @@ -1280,8 +1291,8 @@ LIBSSH2_API void libssh2_keepalive_config (LIBSSH2_SESSION *session, * it again. Returns 0 on success, or LIBSSH2_ERROR_SOCKET_SEND on * I/O errors. */ -LIBSSH2_API int libssh2_keepalive_send (LIBSSH2_SESSION *session, - int *seconds_to_next); +LIBSSH2_API int libssh2_keepalive_send(LIBSSH2_SESSION *session, + int *seconds_to_next); /* NOTE NOTE NOTE libssh2_trace() has no function in builds that aren't built with debug @@ -1299,11 +1310,11 @@ LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask); #define LIBSSH2_TRACE_SOCKET (1<<9) typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*, - void*, + void *, const char *, size_t); LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session, - void* context, + void *context, libssh2_trace_handler_func callback); #ifdef __cplusplus diff --git a/include/libssh2_publickey.h b/include/libssh2_publickey.h index 0979e23..5dbdcf9 100644 --- a/include/libssh2_publickey.h +++ b/include/libssh2_publickey.h @@ -81,16 +81,18 @@ extern "C" { #endif /* Publickey Subsystem */ -LIBSSH2_API LIBSSH2_PUBLICKEY *libssh2_publickey_init(LIBSSH2_SESSION *session); +LIBSSH2_API LIBSSH2_PUBLICKEY * +libssh2_publickey_init(LIBSSH2_SESSION *session); -LIBSSH2_API int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, - const unsigned char *name, - unsigned long name_len, - const unsigned char *blob, - unsigned long blob_len, char overwrite, - unsigned long num_attrs, - const libssh2_publickey_attribute attrs[]); -#define libssh2_publickey_add(pkey, name, blob, blob_len, overwrite, \ +LIBSSH2_API int +libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, + const unsigned char *name, + unsigned long name_len, + const unsigned char *blob, + unsigned long blob_len, char overwrite, + unsigned long num_attrs, + const libssh2_publickey_attribute attrs[]); +#define libssh2_publickey_add(pkey, name, blob, blob_len, overwrite, \ num_attrs, attrs) \ libssh2_publickey_add_ex((pkey), (name), strlen(name), (blob), (blob_len), \ (overwrite), (num_attrs), (attrs)) @@ -107,8 +109,9 @@ LIBSSH2_API int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, unsigned long *num_keys, libssh2_publickey_list **pkey_list); -LIBSSH2_API void libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey, - libssh2_publickey_list *pkey_list); +LIBSSH2_API void +libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey, + libssh2_publickey_list *pkey_list); LIBSSH2_API int libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY *pkey); diff --git a/include/libssh2_sftp.h b/include/libssh2_sftp.h index 4eea32c..f451bd3 100644 --- a/include/libssh2_sftp.h +++ b/include/libssh2_sftp.h @@ -224,12 +224,13 @@ LIBSSH2_API unsigned long libssh2_sftp_last_error(LIBSSH2_SFTP *sftp); LIBSSH2_API LIBSSH2_CHANNEL *libssh2_sftp_get_channel(LIBSSH2_SFTP *sftp); /* File / Directory Ops */ -LIBSSH2_API LIBSSH2_SFTP_HANDLE *libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, - const char *filename, - unsigned int filename_len, - unsigned long flags, - long mode, int open_type); -#define libssh2_sftp_open(sftp, filename, flags, mode) \ +LIBSSH2_API LIBSSH2_SFTP_HANDLE * +libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, + const char *filename, + unsigned int filename_len, + unsigned long flags, + long mode, int open_type); +#define libssh2_sftp_open(sftp, filename, flags, mode) \ libssh2_sftp_open_ex((sftp), (filename), strlen(filename), (flags), \ (mode), LIBSSH2_SFTP_OPENFILE) #define libssh2_sftp_opendir(sftp, path) \ @@ -331,7 +332,8 @@ LIBSSH2_API int libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp, const char *path, unsigned int path_len, char *target, - unsigned int target_len, int link_type); + unsigned int target_len, + int link_type); #define libssh2_sftp_symlink(sftp, orig, linkpath) \ libssh2_sftp_symlink_ex((sftp), (orig), strlen(orig), (linkpath), \ strlen(linkpath), LIBSSH2_SFTP_SYMLINK)