-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
320 lines (265 loc) · 10.8 KB
/
client.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
SMTP.c: TCP/IP client side, use socket functions.
Compiler: BC++ 3.1,
Turbo C++ 1.01(3.01) (free from http://cc.codegear.com/free/cpp)
Compile mode: large
Project: Server.c
..\..\lib\7186el.lib
..\..\lib\tcp_dm32.lib
[Nov 03, 2008] by Liam
[Aug,03,2011] by Nicholas
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "..\..\lib\7186e.h"
#include "..\..\lib\Tcpip32.h"
#define BUFSIZE 1024 /* read/write buffer size */
#define SMTP 25
#define SOCKETS 32 // define the maximum number of sockets
#define QLEN (SOCKETS-1) // maximum connection queue length
int Quit=0, Quit1=0;
fd_set rfds; // set of socket file descriptors for reading
struct sockaddr_in sin;
char *ipaddr="210.241.239.169"; // change for your SMTP server address
struct ip host_ip={{192, 168, 255, 2}, {255, 255, 0, 0}};
int LineMode=1;
char InDataBuf[1460], DataBuf[200];
int DataIdx=0, length, quitmode=0, SmtpMode=0;
char HELO[]="HELLO ICPDAS\r\n";
char CC[50], BCC[50];
char content[1024];
char MAIL[64];
char RCPT[64];
void key_string(char *sinput, char *skeyin)
{
char data;
int i=0;
Print("<Current:%s>", sinput);
while((data=Getch())!='\r')
{
Putch(data);
if(data=='\b')
{
i--;
if(i<0)
i=0;
}
else
skeyin[i++]=data;
}
if(i>0)
skeyin[i]=0;
else
strcpy(skeyin, sinput);
}
void main(int argc, char *argv[])
{
unsigned long t1;
struct sockaddr_in ssin; /* client Internet endpoing address */
int sin_len; /* length of sockaddr_in */
int client;
int ss; /* slave socket got from accept */
int type, protocol, err=0;
int rc; /* return code of selectsocket */
int i;
struct timeval seltime={6,0};
unsigned ServicePort=SMTP;
char kin;
InitLib();
sprintf(MAIL,"%s", "[email protected]");
sprintf(RCPT,"%s", "[email protected]");
sprintf(content,"%s", "hello");
// In general it will show 10ms
while(!Quit)
{
Print("\r\n");
Print("\r\n");
Print("1.Change the SMTP server.(default:168.95.4.211)\r\n");
Print("2.Input the 'Mail From' address.([email protected])\r\n");
Print("3.Input the Mail 'RCPT To' address.([email protected])\r\n");
Print("4.Input Mail CC address([email protected]\r\n");
Print("5.Input Mail BCC address ([email protected])\r\n");
Print("6.Input the characters as Mail's content:\r\n");
Print("7.Connect to the SMTP server and send a mail.\r\n");
Print("Q to exit program.\r\n");
Print("Please Input your select? ");
kin=Getch();
Print("%c\n\r", kin);
i=0;
switch(kin)
{
case 'Q':
case 'q':
Quit=1;
break;
case '1':
key_string(ipaddr,InDataBuf);
strcpy(ipaddr,InDataBuf);
Print("\n%s",ipaddr);
break;
case '2':
key_string(MAIL,InDataBuf);
strcpy(MAIL,InDataBuf);
Print("\n%s",MAIL);
break;
case '3':
key_string(RCPT,InDataBuf);
strcpy(RCPT,InDataBuf);
Print("\n%s",RCPT);
break;
case '4':
key_string(CC,InDataBuf);
strcpy(CC,InDataBuf);
Print("\n%s",CC);
break;
case '5':
key_string(BCC,InDataBuf);
strcpy(BCC,InDataBuf);
Print("\n%s",BCC);
break;
case '6':
key_string(content,InDataBuf);
strcpy(content,InDataBuf);
Print("\n%s",content);
break;
case '7':
// mandatory Network Environment
err=NetStart(); // initiate host environment
if(err<0)
{
Print( "initiate host environment failed! error %d\n\r", err );
Nterm();
return;
}
err=client=socket(PF_INET, SOCK_STREAM, 0); // TCP/IP
if(client<0)
{
Print( "cannot create socket! error %d\n\r", err);
Nterm();
return;
}
// addressing for master socket
memset(&sin, 0, sizeof(sin)); // bzero is a unix system call
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=inet_addr(ipaddr);
sin.sin_port=htons( ServicePort );
FD_ZERO(&rfds);
t1=GetTimeTicks();
#ifdef _OLD_METHOD_
// on success return 0
if(connect(client, (struct sockaddr *)&sin, sizeof(sin)))
{
extern int errno;
Print("connect() failed: %d,time=%lu ms\n", errno,*TimeTicks-t1);
// when the server does not exist, it will show about 240000ms , that is 4 minutes.
// the errno=-12.(ETIMEDOUT)
return;
}
#else
SOCKET_NOBLOCK(client);
// on success return 0
if(connect(client, (struct sockaddr *)&sin, sizeof(sin)))
{
extern int errno;
unsigned cnt=0;
if(errno==EINPROGRESS)
{
#define ConnectTimeout 4000
while(GetTimeTicks()-t1<ConnectTimeout)
{
YIELD();
Print("\r[%u]",++cnt);
if(SOCKET_ISOPEN(client))
goto ConnectSuccess;
}
Print("connect() failed. time=%lu ms,state=%d\n", GetTimeTicks()-t1,
connblo[client].state);
return;
}
else
{
Print("connect() failed: %d,time=%lu ms\n", errno,*TimeTicks-t1);
return;
}
}
#endif
ConnectSuccess:
Print("connect() success, time=%lu ms\n", GetTimeTicks()-t1);
// In general it will show 10ms
while(!Quit1)
{
FD_SET(client, &rfds);
if(selectsocket( SOCKETS, &rfds, NULL, NULL, &seltime)>0)
{
length=readsocket(client, InDataBuf ,sizeof(InDataBuf));
if(length>0)
{
for(i=0; i<length; i++)
Putch(InDataBuf[i]);
switch(SmtpMode)
{
case 0:
Print("Send(%d bytes):%s", sizeof(HELO)-1, HELO);
err=writesocket( client, HELO, sizeof(HELO)-1);
SmtpMode++;
break;
case 1:
sprintf(DataBuf,"MAIL FROM:<%s>\r\n",MAIL);
err=writesocket( client,DataBuf,strlen(DataBuf));
SmtpMode++;
break;
case 2:
if((strlen(CC)>0)&&(strlen(BCC)>0))
sprintf(DataBuf,"RCPT TO:<%s>;<%s>;<%s>\r\n", RCPT, CC, BCC);
else if(strlen(CC)>0)
sprintf(DataBuf,"RCPT TO:<%s>;<%s>\r\n", RCPT, CC);
else if(strlen(BCC)>0)
sprintf(DataBuf,"RCPT TO:<%s>;<%s>\r\n", RCPT, BCC);
else
sprintf(DataBuf, "RCPT TO:<%s>\r\n", RCPT);
err=writesocket(client, DataBuf, strlen(DataBuf));
SmtpMode++;
break;
case 3:
Print("Send:DATA\r\n");
err = writesocket( client, "DATA\r\n", 6);
SmtpMode++;
break;
case 4:
sprintf(DataBuf, "From:<%s>\r\nTo:<%s>\r\nCc:<%s>\r\nSubject:This is a SMTP demo\r\n%s\r\n.\r\n", MAIL, RCPT, CC,content);
Print(DataBuf);
err=writesocket(client, DataBuf, strlen(DataBuf));
SmtpMode++;
break;
case 5:
Print("Send:QUIT\r\n");
err=writesocket(client, "QUIT\r\n", 6);
SmtpMode++;
//Quit1=1;
break;
}
}
else if(length==0)
{
// socket is disconnected
Print("Disconnected by remote side.]");
Quit1=1;
}
else
{
// error
Print("[Error:Socket read error!]");
Quit1=1;
}
}
}
closesocket( client );
Nterm();
SmtpMode=0;
Quit1=0;
break;
}
}
}