Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We are expected to do the following in C via a linux terminal. Here is the mymail.c file that is to be edited. Please implement

We are expected to do the following in C via a linux terminal.

image text in transcribed

Here is the mymail.c file that is to be edited. Please implement code wherever it says TODO:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define DEFAULT_HOST "smtp.uconn.edu"

#define DEFAULT_PORT 25

/* Email struct is a handy way to organize your email fields, the

* fields are not dynamically sized so things will get truncated */

typedef struct Email{

char from[100];

char to[100];

char message[1000];

} Email;

/* Payload struct just holds a buffer (you'll have to malloc it

* before use and free it afterwards) and the buffers size. For

* use with socket_read */

typedef struct Payload{

char* buf;

int sz;

} Payload;

/* Socket_send will just take a socket id and a message and keep

* trying to send until the whole message has sent. */

int socket_send(int sid, char* message);

/* Socket read takes an initialized payload struct and will resize

* the buffer as necessary to hold however much information is read

* from the socket specified. */

int socket_read(int sid, Payload p);

/* Prints human-readable error codes when socket acts up. */

void checkError();

int main(int argc, char*argv[]){

/* Decide on server name and port. */

int port;

char* servername;

servername = (argc > 1) ? argv[1] : DEFAULT_HOST;

port = (argc > 1) ? atoi(argv[2]) : DEFAULT_PORT;

struct hostent *server = gethostbyname(servername);

if (server==NULL){

perror("Error: no such host ");

exit(1);

}

/* Establish socket connection with server. */

struct sockaddr_in serv_addr;

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);

int sockfd = socket(PF_INET, SOCK_STREAM, 0);

if (sockfd

serv_addr.sin_port = htons(port);

int status = connect( sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

checkError(status, __LINE__);

printf("Connected to %s:%d successfully ", servername, port);

/* Get host machine's fully qualified domain name (FQDN),

* stored in fqdn variable */

struct addrinfo hints;

struct addrinfo* info;

char hostname[1024];

hostname[1023] = '\0';

gethostname(hostname, 1023);

memset(&hints, 0, sizeof(hints));

hints.ai_family = AF_INET;

hints.ai_socktype = SOCK_STREAM;

hints.ai_flags = AI_CANONNAME;

getaddrinfo(hostname ,"http", &hints, &info);

char* fqdn = info[0].ai_canonname;

printf("%s ", fqdn);

/* Now that the connection is made in sockfd (variable holding the sockets

* file descriptor), gather the information to send to the server, talk to

* the server to read it, and implement mail protocol here! */

// TODO implement me!

// Clean up!

freeaddrinfo(info);

return 0;

}

int socket_send(int sid, char* message) {

// TODO implement me!

}

int socket_read(int sid, Payload p) {

// TODO implement me!

}

void checkError(int status, int line) {

if (status

printf("socket error(%d)-%d: [%s] ", getpid(), line, strerror(errno));

exit(-1);

}

}

Today you will implement the classic mail, command of UNIX that was (once upon a time) used by most of us to emails. 'mail' is a program that negotiates a connection with a server and sends an email using SMTP (Simple Mail Transfer Protocol). SMTP is a simple text protocol. The following messages are sent between a client and an SMTP server on port 25 in the following order: 1. Client sends the "EHLO" command with an argument equal to the hostname of the machine initiating the connection on port 25 2. Server sends an ack or a reject 3. Client sends the MAIL command with the return address for the message, which has the following form mail to: , and is ended by . 4. Server sends an ack or a reject 5. Client sends the RCPT command with the recipient address for the message, which has the following form rcpt to: and is ended by . 6. Server sends an ack or a reject 7. Client sends the "DATA" command to signal the beginning of the message text. 8. Server sends an ack or a reject (If it can receive the message or not) 9. The message contain one or more lines of text. To terminate the message, the last line must be a period with a single linefeed. The message is terminated with: . 10. Server sends an ack or a reject (If it accepts or rejects the methods) To make this extra clear, a transcript of such a conversation while using "telnet" as the client is shown in Figure1 Note how the conversation starts with a EHLO" message whose argument (in quotes) is the name of the host from which the email is being sent. The server responds with a message of several lines reporting its capabilities. Thoe next message of the client is mail from:, with the email address of the sender. The server responds with one line, saying (in essence) 'Ok'. The next client message is the 'rept to:' message with the email address of the recipient. This prompts (again) a one line answer from the server. The client wraps up by sending DATA' on a line all by itself, followed by multiple lines of text containing the body of the message. A final line with a period (and nothing else) followed by a line-feed ends the email, and the server responds indicating that the message was queued. The client then terminates the exchange with a quit' message. The UNIX mail command (that you are implementing here) automates this whole process. You run it by invoking mail with the address of the recipient on the command line. Your mail program uses your user identifier and your domain to form the address of the sender. mail should prompt the user for the body of the email and deliver it to the server. Clearly, this involves the creation of a socket to contact the server and sending the right messages over the socket. In addition to the protocol, you should implement the socket send and receive functions. Note: Keep in mind that when reading from and writing to sockets, the basic write0 and read0 functions may not write the entirety of the payload. Your functions should ensure that all of the payload is written and read from the socket Today you will implement the classic mail, command of UNIX that was (once upon a time) used by most of us to emails. 'mail' is a program that negotiates a connection with a server and sends an email using SMTP (Simple Mail Transfer Protocol). SMTP is a simple text protocol. The following messages are sent between a client and an SMTP server on port 25 in the following order: 1. Client sends the "EHLO" command with an argument equal to the hostname of the machine initiating the connection on port 25 2. Server sends an ack or a reject 3. Client sends the MAIL command with the return address for the message, which has the following form mail to: , and is ended by . 4. Server sends an ack or a reject 5. Client sends the RCPT command with the recipient address for the message, which has the following form rcpt to: and is ended by . 6. Server sends an ack or a reject 7. Client sends the "DATA" command to signal the beginning of the message text. 8. Server sends an ack or a reject (If it can receive the message or not) 9. The message contain one or more lines of text. To terminate the message, the last line must be a period with a single linefeed. The message is terminated with: . 10. Server sends an ack or a reject (If it accepts or rejects the methods) To make this extra clear, a transcript of such a conversation while using "telnet" as the client is shown in Figure1 Note how the conversation starts with a EHLO" message whose argument (in quotes) is the name of the host from which the email is being sent. The server responds with a message of several lines reporting its capabilities. Thoe next message of the client is mail from:, with the email address of the sender. The server responds with one line, saying (in essence) 'Ok'. The next client message is the 'rept to:' message with the email address of the recipient. This prompts (again) a one line answer from the server. The client wraps up by sending DATA' on a line all by itself, followed by multiple lines of text containing the body of the message. A final line with a period (and nothing else) followed by a line-feed ends the email, and the server responds indicating that the message was queued. The client then terminates the exchange with a quit' message. The UNIX mail command (that you are implementing here) automates this whole process. You run it by invoking mail with the address of the recipient on the command line. Your mail program uses your user identifier and your domain to form the address of the sender. mail should prompt the user for the body of the email and deliver it to the server. Clearly, this involves the creation of a socket to contact the server and sending the right messages over the socket. In addition to the protocol, you should implement the socket send and receive functions. Note: Keep in mind that when reading from and writing to sockets, the basic write0 and read0 functions may not write the entirety of the payload. Your functions should ensure that all of the payload is written and read from the socket

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 17th International Conference Dasfaa 2012 Busan South Korea April 2012 Proceedings Part 1 Lncs 7238

Authors: Sang-goo Lee ,Zhiyong Peng ,Xiaofang Zhou ,Yang-Sae Moon ,Rainer Unland ,Jaesoo Yoo

2012 Edition

364229037X, 978-3642290374

More Books

Students also viewed these Databases questions

Question

2. What do the others in the network want to achieve?

Answered: 1 week ago

Question

what type of attack allows an attacker to use brute force approach

Answered: 1 week ago

Question

The following problem concern basic cache lookups

Answered: 1 week ago