Question: Here is the mymail.c file that is to be edited. Please implement code wherever it says //TODO. Thank you for your help! #include #include #include

Here is the mymail.c file that is to be edited. Please implement code wherever it says //TODO. Thank you for your help!
#include #include #include #include #include #include #include #include #include
#define DEFAULT_HOST "smtp.xxx.xxx" #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;
}
//socket_send will just take a socket id and message and
//keep trying to send until the entire message is sent. The message is a string that ends with NULL.
//return the number of bytes sent.
int socket_send(int sid, char* message) {
int sent= 0;
// TODO implement me!
return sent;
}
//socket_read takes an initialized payload and will resize the buffer as necessary to hold a line of response
//from the socket specified. note that a line must end with ' ' and ' .' return the length of the line received from the socket.
//you can terminate the program if read(), recv() or realloc() fails.
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);
}
}
// call socket_read to get a line of response. Then, call sscanf to find the code returned from the server.
//you do not have to handle multiple lines of code from the server. return the code.
int get_server_response(int sid, Payload * pp) {
int code = 0;
//TODO
return code;
}
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:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
