1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
CONFIG.H
#define zFTP_HOSTNAME "*********.***" // HOSTNAME
#define zFTP_PORT 21 // PORT
#define zFTP_USERNAME "******" // Username
#define zFTP_PASSWORD "******" // Password
#define zFTP_TYPE 1 // 1 for Ascii or 2 for Binary
FTP.CPP
#include <stdio.h>
//#include <stdlib.h>
#include <assert.h>
#include <winsock2.h>
#include "ftp.h"
#include "config.h"
#include "log.h"
#pragma comment(lib, "ws2_32.lib")
char ipData[255];
int pData;
int pasv;
int zMakePort(char *buffer)
{
char ip[64] = {0};
char ip1[32] = {0};
char ip2[32] = {0};
char ip3[32] = {0};
char ip4[32] = {0};
char port1[32] = {0};
char port2[32] = {0};
int p1;
int p2;
int i,j;
for (i = 0; buffer[i] != '('; i++);
i++;
for (j = 0; buffer[i] != ','; i++){
ip1[j++] = buffer[i];
}
i++;
for (j = 0; buffer[i] != ','; i++){
ip2[j++] = buffer[i];
}
i++;
for (j = 0; buffer[i] != ','; i++){
ip3[j++] = buffer[i];
}
i++;
for (j = 0; buffer[i] != ','; i++){
ip4[j++] = buffer[i];
}
i++;
sprintf(ip,"%s.%s.%s.%s", ip1, ip2, ip3, ip4);
strcpy(ipData,ip);
//printf("Adresse IP: %s\n", ipData);
for (j = 0; buffer[i] != ','; i++){
port1[j++] = buffer[i];
}
i++;
sscanf(port1,"%d",&p1);
for (j = 0; buffer[i] != ')'; i++){
port2[j++] = buffer[i];
}
i++;
sscanf(port2,"%d",&p2);
pData = p1 * 256 + p2;
//printf("Data port: %d\n", pData);
return 0;
}
int zRECV(SOCKET sock)
{
char buf[1024];
int n = recv (sock, buf, sizeof buf - 1, 0);
if (n > 0){
buf[n] = 0;
printf ("<< %s", buf);
fflush (stdout);
if(pasv == 1){
zMakePort(buf);
pasv = 0;
}
}
else{
printf ("recv %s\n", n == 0 ? "disconnected" : "error");
}
return 0;
}
int zSEND(SOCKET sock, char const *command)
{
char buf[1024];
int n = sprintf (buf, "%s\r\n", command);
assert (n < (int) (sizeof buf));
n = send (sock, buf, strlen (buf), 0);
if (n > 0){
buf[n] = 0;
printf (">> %s", buf);
fflush (stdout);
}
else{
printf ("send %s\n", n == 0 ? "disconnected" : "error");
}
return 0;
}
int zDIAL(SOCKET sock)
{
Sleep(100);
zRECV(sock);
Sleep(100);
zSEND(sock, "USER " zFTP_USERNAME); // Send login
Sleep(100);
zRECV(sock);
Sleep(100);
zSEND(sock, "PASS " zFTP_PASSWORD); // Send password
Sleep(100);
zRECV(sock);
Sleep(100);
zSEND(sock, "SYST");
Sleep(100);
zRECV(sock);
Sleep(100);
zSEND(sock, "PWD");
Sleep(100);
zRECV(sock);
Sleep(100);
if(zFTP_TYPE == 1){
zSEND(sock, "TYPE A"); // Ascii Mode
Sleep(100);
zRECV(sock);
}
if(zFTP_TYPE == 2){
zSEND(sock, "TYPE I"); // Binary mode
Sleep(100);
zRECV(sock);
}
Sleep(100);
pasv = 1;
zSEND(sock, "PASV"); // Passive
Sleep(100);
zRECV(sock);
Sleep(100);
zSEND(sock, "CWD www");
Sleep(100);
zRECV(sock);
Sleep(100);
return 0;
}
int zFTP()
{
int iResult;
WSADATA WSAData;
iResult = WSAStartup(MAKEWORD(2,0), &WSAData);
if (iResult != 0) {
screenLog('E',"System","WSAStartup failed!\n");
return 1;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
SOCKET datasock = socket(AF_INET, SOCK_STREAM, 0);
struct hostent *hostinfo = NULL;
const char *hostname = zFTP_HOSTNAME;
hostinfo = gethostbyname(hostname);
SOCKADDR_IN ftpServAddr = { 0 };
//ftpServAddr.sin_addr.s_addr = inet_addr("192.168.1.200");
ftpServAddr.sin_addr = *(IN_ADDR *) hostinfo->h_addr;
ftpServAddr.sin_family = AF_INET;
ftpServAddr.sin_port = htons(zFTP_PORT);
screenLog('S',"System","Connect to ftp server...");
iResult = connect(sock, (SOCKADDR*)&ftpServAddr, sizeof(ftpServAddr));
if(iResult == SOCKET_ERROR){
screenLog('E',"System","Unable to connect to ftp server!");
}
zDIAL(sock);
SOCKADDR_IN dataServAddr;
dataServAddr.sin_addr.s_addr = inet_addr(ipData);
dataServAddr.sin_family = AF_INET;
dataServAddr.sin_port = htons(pData);
iResult = connect(datasock, (SOCKADDR *)&dataServAddr, sizeof(dataServAddr));
if(iResult == SOCKET_ERROR){
screenLog('E',"System","Unable to connect to ftp data server!");
}
zSEND(sock, "STOR filename.txt");
Sleep(100);
zRECV(datasock);
screenLog('S',"System","Closing ftp connexion...");
closesocket(sock);
closesocket(datasock);
WSACleanup();
return 0;
} |