| 1 |
/* |
| 2 |
|
| 3 |
libftp 1.0 (stable): this is an FTP library to simplify the work to a Developer |
| 4 |
who want to work with FTP servers (RFC 959). |
| 5 |
Copyright (C) 2007/2008 omnipresent - omnipresent[at]email.it |
| 6 |
|
| 7 |
This program is free software: you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation, either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/time.h> |
| 29 |
#include <sys/socket.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <netdb.h> |
| 33 |
#include <fcntl.h> |
| 34 |
|
| 35 |
#include "ftp_main.h" |
| 36 |
|
| 37 |
/*** |
| 38 |
ftp_noop: |
| 39 |
- int sck => Socket Descriptor |
| 40 |
- int verbose => Verbose? |
| 41 |
. 0 Do not Print |
| 42 |
. 1 Print |
| 43 |
- ret => Success? |
| 44 |
. 0 Success |
| 45 |
.-1 Error |
| 46 |
***/ |
| 47 |
|
| 48 |
|
| 49 |
int ftp_noop (int sck, int verbose) |
| 50 |
{ |
| 51 |
char *buf; |
| 52 |
int n = 0; |
| 53 |
struct timeval tm; //tv_sec - tv_usec |
| 54 |
fd_set readfds; |
| 55 |
int OK = 0; |
| 56 |
|
| 57 |
|
| 58 |
buf = (char *)malloc(1024*sizeof(char)); |
| 59 |
|
| 60 |
memset(buf, 0x0, 1024); |
| 61 |
|
| 62 |
if (write(sck, "NOOP\n", strlen("NOOP\n")) == -1) { |
| 63 |
|
| 64 |
if (verbose) { |
| 65 |
if (errno == EBADF) { |
| 66 |
fprintf(stderr, "Socket Descriptor Not Valid!\n"); |
| 67 |
free(buf); |
| 68 |
|
| 69 |
return -1; |
| 70 |
} else if (errno == EIO) { |
| 71 |
fprintf(stderr, "I/O Error!\n"); |
| 72 |
free(buf); |
| 73 |
|
| 74 |
return -1; |
| 75 |
} else if (errno == EINTR) { |
| 76 |
fprintf(stderr, "Signal Interrupted The write() Function\n"); |
| 77 |
free(buf); |
| 78 |
|
| 79 |
return -1; |
| 80 |
} |
| 81 |
} else { |
| 82 |
if (errno == EBADF) { |
| 83 |
free(buf); |
| 84 |
|
| 85 |
return -1; |
| 86 |
} else if (errno == EIO) { |
| 87 |
free(buf); |
| 88 |
|
| 89 |
return -1; |
| 90 |
} else if (errno == EINTR) { |
| 91 |
free(buf); |
| 92 |
|
| 93 |
return -1; |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
free(buf); |
| 99 |
|
| 100 |
return -1; |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
tm.tv_sec = 3; |
| 105 |
tm.tv_usec = 0; |
| 106 |
|
| 107 |
FD_ZERO(&readfds); |
| 108 |
|
| 109 |
FD_SET(sck, &readfds); |
| 110 |
|
| 111 |
if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) { |
| 112 |
if (verbose) { |
| 113 |
perror("select()"); |
| 114 |
} |
| 115 |
free(buf); |
| 116 |
|
| 117 |
return -1; |
| 118 |
} else { |
| 119 |
|
| 120 |
if (FD_ISSET(sck, &readfds)) { |
| 121 |
|
| 122 |
if ( (n = recv(sck, buf, 1022, 0)) < 0) { |
| 123 |
if (verbose) { |
| 124 |
perror("recv()"); |
| 125 |
} |
| 126 |
free(buf); |
| 127 |
|
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
if (verbose) |
| 133 |
printf("[Server] %s\n", buf); |
| 134 |
|
| 135 |
} |
| 136 |
else { |
| 137 |
if (verbose) |
| 138 |
printf("[-] Timeout\n"); |
| 139 |
|
| 140 |
return -1; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
buf[n] = '\0'; |
| 145 |
|
| 146 |
|
| 147 |
if (strfnbytes(buf, "200", 3) == 0) { |
| 148 |
OK = 0; |
| 149 |
} else { |
| 150 |
OK = 1; |
| 151 |
} |
| 152 |
|
| 153 |
memset(buf, 0x0, 1024); |
| 154 |
|
| 155 |
free(buf); |
| 156 |
|
| 157 |
|
| 158 |
if (OK == 0) { |
| 159 |
return 0; //0 returned; operation sucessfull! |
| 160 |
} else { |
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
} |