-
Notifications
You must be signed in to change notification settings - Fork 4
/
Client.c
448 lines (389 loc) · 12.7 KB
/
Client.c
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
Author : Meet Akbari (AU1841073)
Mayankkumar Tank (AU1841057)
Course : Computer Networks | 5th semester
Organization : School of Engineering and Applied Science, Ahmedabad University, India.
Project : SMTP(Simple Mail Transfer Protocol) Implementation in C using Socket Programming
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define domain "server.smtp.com"
char mail_from[100];
char mail_to[100];
void send_image(int socket)
{
FILE *fpimage;
int size;
int readsize;
int status;
int packet_index = 1;
char send_buffer[10239];
char read_buffer[256];
char f_name[100];
printf("\nEnter Image Path/Image file name : ");
scanf("%s",f_name);
fpimage = fopen(f_name, "r");
printf("\nCLIENT: \nGetting Image Size...\n");
if(fpimage == NULL){
printf("Error occured while opening Image File!\n");
}
fseek(fpimage, 0, SEEK_END);
size = ftell(fpimage);
fseek(fpimage, 0, SEEK_SET);
printf("Total Image size: %i\n",size);
printf("Sending Image Size\n");
write(socket, (void *)&size, sizeof(int));
printf("Sending Image as a byte array...");
do{
status=read(socket, &read_buffer , 255);
//printf("Bytes read: %i\n",status);
}while(status < 0);
//printf("Data successfully received in socket!\n");
//printf("Socket Data: %s\n", read_buffer);
while(!feof(fpimage))
{
readsize = fread(send_buffer, 1, sizeof(send_buffer)-1, fpimage);
do{
status = write(socket, send_buffer, readsize);
}while(status<0);
//printf("Packet number: %i\n",packet_index);
//printf("Packet size sent: %i\n",readsize);
printf(" \n");
printf(" \n");
packet_index++;
bzero(send_buffer, sizeof(send_buffer));
}
}
char * Mail_Header(char *from,char *to,char *sub,char *content)
{
time_t t;
time(&t);
char *header = NULL;
char date[26];
char DATE_h[8 + strlen(date)];
char sender[8 + strlen(from)]; //FROM: sender's_email\r\n
char recep[6 + strlen(to)]; //TO: recepient's_email\r\n
char subject[11 + strlen(sub)];
char content_a[1 + strlen(content) + 2 + 1 + 1];
strftime(date, (33), "%a %d %b %Y %H:%M:%S",localtime(&t));
sprintf(DATE_h,"DATE: %s\r\n",date);
sprintf(sender,"FROM: %s\r\n",from);
sprintf(subject,"Subject: %s\r\n",sub);
sprintf(recep,"TO: %s\r\n",to);
//extra \n is used to end the header part
sprintf(content_a,"%s\r\n",content);
int header_length = strlen(DATE_h) + strlen(sender) + strlen(subject) + strlen(recep) + strlen(content_a);
header = (char *) malloc(header_length * sizeof(char));
memcpy(&header[0], &DATE_h, strlen(DATE_h));
memcpy(&header[0 + strlen(DATE_h)], &sender , strlen(sender));
memcpy(&header[0 + strlen(DATE_h) + strlen(sender)], &subject , strlen(subject));
memcpy(&header[0 + strlen(DATE_h) + strlen(sender) + strlen(subject)], &recep , strlen(recep));
memcpy(&header[0 + strlen(DATE_h) + strlen(sender) + strlen(subject) + strlen(recep)], &content_a , strlen(content_a));
return header;
}
int main(int argc, char *argv[])
{
int socket_id,n;
int portno;
struct sockaddr_in serv_addr;
struct hostent *server;
char cname[256];
char buff[10240];
if(argc < 3)
{
perror("\nPlease enter the hostname and port number.\n");
exit(0);
}
portno = atoi(argv[2]);
socket_id = socket(AF_INET,SOCK_STREAM, 0);
if(socket_id < 0)
{
perror("\nError occured while opening the socket!\n");
exit(0);
}
server = gethostbyname(argv[1]);
if(server == NULL)
{
perror("\nError: No such host found!\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
//connect to the server
int connect_id;
connect_id = connect(socket_id,(struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(connect_id < 0)
{
perror("Error occured while connecting to server...\n");
}
//Implementing commands which are used b/w Client and SMTP Server for communication
do
{
printf("Enter the command : ");
intake: gets(cname);
//cname[strlen(cname) + 1] = '\0';
char code[4]; //to store the 3 digit response code received from server
if(strcasecmp(cname,"HELO") == 0)
{
bzero(buff,10240);
strcpy(buff,"HELO ");
strcat(buff, domain);
strcat(buff,"\r\n");
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
printf("\nCLIENT : %s",buff); //HELO domain
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n",buff); //250 Hello domain
//checking error
code[0] = buff[0];
code[1] = buff[1];
code[2] = buff[2];
code[3] = '\0';
if(strcmp(code,"250") == 0)
{
printf("\nGo to next command...\n\n");
}
else{
printf("\nError occured!\n\n");
}
fflush(stdin);
}
else if(strcasecmp(cname,"MAIL FROM") == 0)
{
bzero(buff,10240);
printf("\nEnter Sender Email id : ");
scanf("%s",mail_from);
strcpy(buff,"MAIL FROM:<");
strcat(buff,mail_from);
strcat(buff,">");
strcat(buff,"\r\n");
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
printf("\nCLIENT : %s",buff); //MAIL FROM:<your email id>
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n",buff); //250 OK
//checking error
code[0] = buff[0];
code[1] = buff[1];
code[2] = buff[2];
code[3] = '\0';
if(strcmp(code,"250") == 0)
{
printf("\nGo to next command...\n\n");
}
else{
printf("\nError occured!\n\n");
}
fflush(stdin);
}
else if(strcasecmp(cname,"RCPT TO") == 0)
{
bzero(buff,10240);
printf("\nEnter Recipient Email id : ");
scanf("%s",mail_to);
strcpy(buff,"RCPT TO:<");
strcat(buff,mail_to);
strcat(buff,">");
strcat(buff,"\r\n");
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
printf("\nCLIENT : %s",buff); //RCPT TO:<your email id>
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n",buff); //250 OK
//checking error
code[0] = buff[0];
code[1] = buff[1];
code[2] = buff[2];
code[3] = '\0';
if(strcmp(code,"250") == 0)
{
printf("\nGo to next command...\n\n");
}
else{
printf("\nError occured!\n\n");
}
fflush(stdin);
}
else if(strcasecmp(cname,"DATA") == 0)
{
bzero(buff,10240);
strcpy(buff,"DATA");
strcat(buff,"\r\n");
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
printf("\nCLIENT : %s",buff); //DATA
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n",buff); //354 Send message content; end with <CRLF>.<CRLF>
//checking error
code[0] = buff[0];
code[1] = buff[1];
code[2] = buff[2];
code[3] = '\0';
if(strcmp(code,"354") == 0)
{
printf("\nReady to send header data!\n\n");
}
else{
printf("\nError occured!\n\n");
}
//creating a mail header
char sub[150];
char content[450];
printf("\nEnter Subject : ");
scanf("%[^\n]",sub);
printf("\nEnter content : (Press Tab and Enter Key to end)\n");
scanf("%[^\t]",content);
bzero(buff,10240);
//Mail_header function declared above
strcpy(buff, Mail_Header(mail_from,mail_to,sub,content)); //assigning header to buffer
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
printf("\nCLIENT : ====| Mail header & content |====\n%s\n",buff); //header content
bzero(buff,10240);
strcpy(buff,".\r\n");
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n",buff); //250 OK
//checking error
code[0] = buff[0];
code[1] = buff[1];
code[2] = buff[2];
code[3] = '\0';
if(strcmp(code,"250") == 0)
{
printf("\nGo to next command...\n\n");
}
else{
printf("\nError occured!\n\n");
}
fflush(stdin);
}
else if(strcasecmp(cname,"ATTACHMENT") == 0){
bzero(buff,10240);
strcpy(buff,"Attachment");
strcat(buff,"\r\n");
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
printf("\nCLIENT : %s",buff); //DATA
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n",buff); //420 send Attachment
code[0] = buff[0];
code[1] = buff[1];
code[2] = buff[2];
code[3] = '\0';
if(strcmp(code,"420") == 0)
{
printf("\nReady to send image!\n\n");
}
else{
printf("\nError occured!\n\n");
}
send_image(socket_id);
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n\n",buff);
}
else if(strcasecmp(cname,"QUIT") == 0)
{
bzero(buff,10240);
strcpy(buff,"QUIT");
strcat(buff,"\r\n");
n = write(socket_id,buff,strlen(buff));
if(n < 0)
{
printf("\nError occured while writing to socket!\n");
}
printf("\nCLIENT : %s",buff); //QUIT
bzero(buff,10240);
n = read(socket_id,buff,10239);
if(n < 0)
{
printf("\nError occured while reading from socket!\n");
}
printf("SERVER : %s\n",buff); //221 Bye
//checking error
code[0] = buff[0];
code[1] = buff[1];
code[2] = buff[2];
code[3] = '\0';
if(strcmp(code,"221") == 0)
{
printf("\nConnection closed successfully with SMTP Server!\n\n");
}
else{
printf("\nError occured!\n\n");
}
fflush(stdin);
}
else
{
strcpy(cname,"");
goto intake;
}
}while(strcmp(cname,"QUIT") != 0);
}