forked from M-griffin/EtherTerm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketFTP.cpp
142 lines (125 loc) · 3.12 KB
/
socketFTP.cpp
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
// EtherTerm SVN: $Id$
// Source: $HeadURL$
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
#include "socketState.hpp"
#include "socketHandler.hpp"
#include "inputHandler.hpp"
#include "sequenceParser.hpp"
#ifdef TARGET_OS_MAC
#include <SDL2/SDL_net.h>
#elif _WIN32
#include <SDL_net.h>
#else
#include <SDL2/SDL_net.h>
#endif
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <time.h>
/*
* Start of FTP_Socket Derived Class (FTP)
*/
/* send a string buffer over a TCP socket with error checking */
/* returns 0 on any errors, length sent on success */
int FTP_Socket::sendSocket(unsigned char *buf, Uint32 len)
{
Uint32 result;
result = SDLNet_TCP_Send(controlSocket, buf, len);
if(result < strlen((char *)buf))
{
if(SDLNet_GetError() && strlen(SDLNet_GetError()))
{
printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
return(0);
}
}
return(result);
}
/* handle Telnet better */
int FTP_Socket::recvSocket(char *message)
{
Uint32 result;
/* get the string buffer over the socket */
result = SDLNet_TCP_Recv(controlSocket, message, 8192);
// TCP Connection is broken. (because of error or closure)
if(result <= 0)
{
onExit();
return -1;
}
/* return the new buffer */
return result;
}
int FTP_Socket::pollSocket()
{
int numready = SDLNet_CheckSockets(set, 0);
if(numready == -1)
{
std::cout << "SDLNet_CheckSockets: " << SDLNet_GetError() << std::endl;
TheSocketHandler::Instance()->setActive(false);
return numready;
}
if(numready && SDLNet_SocketReady(controlSocket) > 0)
{
numready = 1;
}
else
{
numready = 0;
}
return numready;
}
bool FTP_Socket::onEnter()
{
std::cout << "FTP_Socket::onEnter()" << std::endl;
IPaddress ip;
set = SDLNet_AllocSocketSet(1);
if(!set)
{
printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
onExit();
return false;
}
/* Resolve the argument into an IP address type */
std::cout << "Connecting to " << host.c_str() << " on port " << port << std::endl;
if(SDLNet_ResolveHost(&ip,host.c_str(),port) == -1)
{
printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError());
onExit();
return false;
}
//open the socket
controlSocket = SDLNet_TCP_Open(&ip);
if(!controlSocket)
{
printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
onExit();
return false;
}
if(SDLNet_TCP_AddSocket(set,controlSocket) == -1)
{
printf("SDLNet_TCP_AddSocket: %s\n",SDLNet_GetError());
onExit();
return false;
}
/* reference Only
if(SDLNet_TCP_DelSocket(set,controlSocket) == -1)
{
printf("SDLNet_DelSocket: %s\n",SDLNet_GetError());
onExit();
return false;
} */
return true;
}
bool FTP_Socket::onExit()
{
std::cout << "FTP_Socket::onExit()" << std::endl;
if(TheInputHandler::Instance()->isGlobalShutdown())
{
SDLNet_TCP_Close(controlSocket);
}
SDLNet_FreeSocketSet(set);
return true;
}