-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxMsClient.cpp
411 lines (325 loc) · 11.6 KB
/
wxMsClient.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
From: http://www.exegesis.uklinux.net/gandalf/winsock/winsock2.htm
*/
#if 0
/*
* popclnt1.c
*
*
* A very basic pop client that retrieves mail from an account into
* a multi pop retrieval client in later parts of the tutorial with all
* the extra error handling that's needed. Use this as a framework to
* develop your own pop client. This program doesn't delete any mails
* from the pop mail server, so you can try it out without being afraid
* of losing any emails!
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <winsock.h>
#define WIN32_LEAN_AND_MEAN
void handle_error(void);
void pop_error(void);
void write_file(int file, char *p);
int main (int argc, char *argv[])
{
const long BUF_LEN=60000;
/* 60000 char buffer. In real life we would have to allow for a much
* larger buffer than this to allow retrieval of long messages which
* might have attachments. We will be doing this in one of the later
* parts of the tutorial by using synchronised data flow with a smaller
* buffer that downloads mails in smaller chunks.
*/
const char POP_ERR_MSG[]="-ERR"; /* POP Server error response */
const int LOG_FILE=1; /* write to log file */
const int MAIL_FILE=2; /* write to mail file */
const int SCREEN=4; /* display on screen */
WORD wVersionRequested; /* socket dll version info */
WSADATA wsaData; /* socket lib initialisation */
SOCKET sock; /* socket details */
struct sockaddr_in address; /* socket address stuff */
struct hostent * host; /* host stuff */
int err; /* error trapping */
float socklib_ver; /* socket dll version */
char * File_Buf; /* file buffer */
char * Rec_Buf; /* recieve buffer */
/* Fill in to suit your own account, increase array sizes if needed */
char hostname[20] = "pop.wherever.net"; /* hostname for pop server */
char username[20] = "username"; /* username */
char password[20] = "password"; /* password */
int i,j; /* general counters */
char tempbuf[20] = ""; /* temp string buffer for parsing */
int num_flag=0; /* string parse flag */
time_t now; /* from time.h */
/* allocate dynamic storage from the heap */
File_Buf = malloc(BUF_LEN * sizeof(char) + 1);
Rec_Buf = malloc(BUF_LEN * sizeof(char) + 1);
wVersionRequested = MAKEWORD( 1, 1 );
if ( WSAStartup( wVersionRequested, &wsaData ) != 0 )
handle_error();
/* Check socket DLL supports 1.1 or higher */
socklib_ver = HIBYTE( wsaData.wVersion ) / 10.0;
socklib_ver += LOBYTE( wsaData.wVersion );
if ( socklib_ver {
printf ("Error: socket library must support 1.1 or greater\n");
WSACleanup();
return 0;
}
/* get date and time to write to log file */
time(&now);
sprintf(File_Buf,"Pop Client Session started %.24s.\n\n", ctime(&now));
write_file(LOG_FILE | SCREEN, File_Buf);
/* open a socket based on TCP/IP */
if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
handle_error();
address.sin_family=AF_INET; /* internet */
address.sin_port = htons(110); /* pop port */
sprintf(File_Buf,"Connecting to %s\n", hostname);
write_file(LOG_FILE | SCREEN, File_Buf);
/* DNS on hostname */
if ( (host=gethostbyname(hostname)) == NULL )
handle_error();
/* IP address for hostname */
address.sin_addr.s_addr=*((unsigned long *) host->h_addr);
/* try to connect to pop server */
if ( (connect(sock,(struct sockaddr *) &address, sizeof(address))) != 0)
handle_error();
sprintf(File_Buf, "Connected to %s\n", hostname);
write_file(LOG_FILE | SCREEN, File_Buf);
err=recv(sock,Rec_Buf,BUF_LEN,0); /* get response from server */
sprintf(File_Buf,"received: %s", Rec_Buf); /* should start +OK */
write_file(LOG_FILE, File_Buf);
if ( strstr(Rec_Buf,POP_ERR_MSG) != NULL ) /* check for errors */
pop_error();
strcpy(Rec_Buf,"USER ");
strcat(Rec_Buf, username);
strcat(Rec_Buf, "\r\n");
err=send(sock,Rec_Buf,strlen(Rec_Buf),0); /* send username */
sprintf(File_Buf,"sent: %s",Rec_Buf);
write_file(LOG_FILE, File_Buf);
err=recv(sock,Rec_Buf,BUF_LEN,0); /* should recieve +OK */
sprintf(File_Buf,"received: %s", Rec_Buf);
write_file(LOG_FILE, File_Buf);
if ( strstr(Rec_Buf,POP_ERR_MSG) != NULL ) /* check for errors */
pop_error();
strcpy(Rec_Buf,"PASS ");
strcat(Rec_Buf, password);
strcat(Rec_Buf, "\r\n");
err=send(sock,Rec_Buf,strlen(Rec_Buf),0); /* send password */
sprintf(File_Buf,"sent: %s",Rec_Buf);
write_file(LOG_FILE, File_Buf);
err=recv(sock,Rec_Buf,BUF_LEN,0); /* should recieve +OK */
sprintf(File_Buf,"received: %s",Rec_Buf);
write_file(LOG_FILE, File_Buf);
if ( strstr(Rec_Buf,POP_ERR_MSG) != NULL ) /* check for errors */
pop_error();
strcpy(Rec_Buf,"STAT\r\n"); /* send stat command */
err=send(sock,Rec_Buf,strlen(Rec_Buf),0);
sprintf(File_Buf,"sent: %s", Rec_Buf);
write_file(LOG_FILE, File_Buf);
/*
* should recieve +OK x y where x = number of messages
* and y is total octets. Note the buffers we have allocated
* would not be sufficient for a real pop client. They
* should be dynamically allocated here after checking the
* size of the waiting emails.
*/
err=recv(sock,Rec_Buf,BUF_LEN,0);
sprintf(File_Buf,"received: %s", Rec_Buf);
write_file(LOG_FILE, File_Buf);
if ( strstr(Rec_Buf,POP_ERR_MSG) != NULL ) /* check for errors */
pop_error();
/*
* Now we need to work out how many emails we have to retrieve
* and set up a loop to get them all.
*/
i = j = 0;
while (i {
if (Rec_Buf[i] > '0' && Rec_Buf[i] {
while (Rec_Buf[i] != ' ') /* space is end of number */
{
tempbuf[j] = Rec_Buf[i];
j++;
i++;
}
num_flag=1;
tempbuf[j] = '\0';
}
i++;
}
i = atoi(tempbuf); /* number of emails */
printf("There are %d message(s) waiting\r\n", i);
/*
* At this point we should really be checking the length of each email
* on the POP server and then either allocate dynamic memory, or better
* still, try synchronised access which we will be doing in a later
* part of the tutorial. Until then, let's keep it simple. If you do
* have an email larger than the buffer, it won't do anything else
* other than crash the program!
*/
for( j=1; j<=i; j++)
{
sprintf(Rec_Buf, "RETR %d\r\n", j); /* add number */
printf("Retrieving message number %d\n", j);
err=send(sock,Rec_Buf,strlen(Rec_Buf),0);
sprintf(File_Buf,"sent: %s ",Rec_Buf);
write_file(LOG_FILE, File_Buf);
err=recv(sock,Rec_Buf,BUF_LEN,0); /* +OK message will follow */
sprintf(File_Buf,"received: %s ",Rec_Buf);
write_file(LOG_FILE, File_Buf);
if ( strstr(Rec_Buf,POP_ERR_MSG) != NULL ) /* check for errors */
pop_error();
err=recv(sock,Rec_Buf,BUF_LEN,0); /* retrieve the mail */
sprintf(File_Buf,"%s",Rec_Buf);
write_file(MAIL_FILE, File_Buf);
}
printf ("All done\n\n");
/* send quit command then clean up */
strcpy(Rec_Buf,"QUIT\r\n");
err=send(sock,Rec_Buf,strlen(Rec_Buf),0);
sprintf(File_Buf,"sent: %s", Rec_Buf);
write_file(LOG_FILE, File_Buf);
/* deallocate buffer memory */
free(File_Buf);
free(Rec_Buf);
WSACleanup();
return 0;
}
void handle_error(void)
{
/*
* Errors are handled by calling the WSAGetLastError routine which
* will return the last error as one of the following. As we develop
* this tutorial, we will go into much more detail on what they mean
* and what caused them.
*/
switch ( WSAGetLastError() )
{
case WSANOTINITIALISED :
printf("Unable to initialise socket.\n");
break;
case WSAEAFNOSUPPORT :
printf("The specified address family is not supported.\n");
break;
case WSAEADDRNOTAVAIL :
printf("Specified address is not available from the local machine.\n");
break;
case WSAECONNREFUSED :
printf("The attempt to connect was forcefully rejected.\n");
break;
case WSAEDESTADDRREQ :
printf("address destination address is required.\n");
break;
case WSAEFAULT :
printf("The namelen argument is incorrect.\n");
break;
case WSAEINVAL :
printf("The socket is not already bound to an address.\n");
break;
case WSAEISCONN :
printf("The socket is already connected.\n");
break;
case WSAEADDRINUSE :
printf("The specified address is already in use.\n");
break;
case WSAEMFILE :
printf("No more file descriptors are available.\n");
break;
case WSAENOBUFS :
printf("No buffer space available. The socket cannot be created.\n");
break;
case WSAEPROTONOSUPPORT :
printf("The specified protocol is not supported.\n");
break;
case WSAEPROTOTYPE :
printf("The specified protocol is the wrong type for this socket.\n");
break;
case WSAENETUNREACH :
printf("The network can't be reached from this host at this time.\n");
break;
case WSAENOTSOCK :
printf("The descriptor is not a socket.\n");
break;
case WSAETIMEDOUT :
printf("Attempt timed out without establishing a connection.\n");
break;
case WSAESOCKTNOSUPPORT :
printf("Socket type is not supported in this address family.\n");
break;
case WSAENETDOWN :
printf("Network subsystem failure.\n");
break;
case WSAHOST_NOT_FOUND :
printf("Authoritative Answer Host not found.\n");
break;
case WSATRY_AGAIN :
printf("Non-Authoritative Host not found or SERVERFAIL.\n");
break;
case WSANO_RECOVERY :
printf("Non recoverable errors, FORMERR, REFUSED, NOTIMP.\n");
break;
case WSANO_DATA :
printf("Valid name, no data record of requested type.\n");
break;
case WSAEINPROGRESS :
printf("address blocking Windows Sockets operation is in progress.\n");
break;
case WSAEINTR :
printf("The (blocking) call was canceled via WSACancelBlockingCall().\n");
break;
default :
printf("Unknown error.\n");
break;
}
WSACleanup();
// exit(0);
}
void pop_error(void)
{
printf("POP Error, Check the Log File for Details\r\n\r\n");
WSACleanup();
// exit(0);
}
void write_file(int file_type, char *p)
{
const int LOG_FILE=1; /* write to log file */
const int MAIL_FILE=2; /* write to mail file */
const int SCREEN=4; /* display on screen */
if( (file_type & LOG_FILE) != 0 ) /* add to logs */
{
FILE *fp=fopen("poplog.txt","a+");
fprintf(fp,"%s\n",p);
fclose(fp);
}
if( (file_type & MAIL_FILE) != 0 ) /* add to mailbox */
{
FILE *fp=fopen("mailbox.txt","a+");
fprintf(fp,"%s\n",p);
fclose(fp);
}
if ( (file_type & SCREEN) != 0 ) /* screen */
{
printf("%s\n",p);
}
}
Log File Example after connecting to a POP Server:-
Pop Client Session started Tue Mar 28 22:27:06 2000.
Connecting to pop.freeuk.net
Connected to pop.freeuk.net
received: +OK CPOP v2.3 Ready for Action
sent: USER xxxxxxxx
received: +OK
xxxxxxxx
sent: PASS xxxxxxxx
received: +OK
xxxxxxxx
sent: STAT
received: +OK 1 7165
sent: RETR 1
received: +OK
1
sent: QUIT
#endif
/*
*/