Skip to content
Snippets Groups Projects
Commit 09b527b7 authored by Bogdan's avatar Bogdan
Browse files

delte_useless_folder

parent 710e14f6
No related branches found
No related tags found
1 merge request!9Add lab 11
# Protocoale de comunicatii
# Laborator 11 - E-mail
# Makefile
CFLAGS = -Wall -g
IP_SERVER = 127.0.0.1
NUME_FISIER = file.txt
default: send_mail
send_mail: send_mail.c
gcc $(CFLAGS) send_mail.c -o send_mail
run: send_mail
./send_mail ${IP_SERVER} ${NUME_FISIER}
.PHONY: clean
clean:
rm -f send_mail
This is an attachment.
/*
* Protocoale de comunicatii
* Laborator 11 - E-mail
* send_mail.c
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#define SMTP_PORT 25
#define MAXLEN 500
/**
* Citeste maxim maxlen octeti de pe socket-ul sockfd in
* buffer-ul vptr. Intoarce numarul de octeti cititi.
*/
ssize_t read_line(int sockd, void *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *buffer;
buffer = vptr;
for (n = 1; n < maxlen; n++) {
if ((rc = read(sockd, &c, 1)) == 1) {
*buffer++ = c;
if (c == '\n') {
break;
}
} else if (rc == 0) {
if (n == 1) {
return 0;
} else {
break;
}
} else {
if (errno == EINTR) {
continue;
}
return -1;
}
}
*buffer = 0;
return n;
}
/**
* Trimite o comanda SMTP si asteapta raspuns de la server. Comanda
* trebuie sa fie in buffer-ul sendbuf. Sirul expected contine
* inceputul raspunsului pe care trebuie sa-l trimita serverul
* in caz de succes (de exemplu, codul 250). Daca raspunsul
* semnaleaza o eroare, se iese din program.
*/
void send_command(int sockfd, const char sendbuf[], char *expected)
{
char recvbuf[MAXLEN];
int nbytes;
char CRLF[2] = {13, 10};
printf("Trimit: %s\n", sendbuf);
write(sockfd, sendbuf, strlen(sendbuf));
write(sockfd, CRLF, sizeof(CRLF));
nbytes = read_line(sockfd, recvbuf, MAXLEN - 1);
recvbuf[nbytes] = 0;
printf("Am primit: %s", recvbuf);
if (strstr(recvbuf, expected) != recvbuf) {
printf("Am primit mesaj de eroare de la server!\n");
exit(-1);
}
}
int main(int argc, char **argv) {
int sockfd;
int port = SMTP_PORT;
struct sockaddr_in servaddr;
char server_ip[INET_ADDRSTRLEN];
char sendbuf[MAXLEN];
char recvbuf[MAXLEN];
if (argc != 3) {
printf("Utilizare: ./send_msg adresa_server nume_fisier\n");
exit(-1);
}
strncpy(server_ip, argv[1], INET_ADDRSTRLEN);
// TODO 1: creati socket-ul TCP client
// TODO 2: completati structura sockaddr_in cu
// portul si adresa IP ale serverului SMTP
// TODO 3: conectati-va la server
// se primeste mesajul de conectare de la server
read_line(sockfd, recvbuf, MAXLEN -1);
printf("Am primit: %s\n", recvbuf);
// se trimite comanda de HELO
sprintf(sendbuf, "HELO localhost");
send_command(sockfd, sendbuf, "250");
// TODO 4: trimiteti comanda de MAIL FROM
// TODO 5: trimiteti comanda de RCPT TO
// TODO 6: trimiteti comanda de DATA
// TODO 7: trimiteti e-mail-ul (antete + corp + atasament)
// TODO 8: trimiteti comanda de QUIT
// TODO 9: inchideti socket-ul TCP client
return 0;
}
# Protocoale de comunicatii
# Laborator 11 - DNS
# Makefile
CFLAGS = -Wall -g
default: dns
dns: dns.c
.PHONY: clean
clean:
rm -f dns
// Protocoale de comunicatii
// Laborator 9 - DNS
// dns.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
int usage(char* name)
{
printf("Usage:\n\t%s -n <NAME>\n\t%s -a <IP>\n", name, name);
return 1;
}
// Receives a name and prints IP addresses
void get_ip(char* name)
{
int ret;
struct addrinfo hints, *result, *p;
// TODO: set hints
// TODO: get addresses
// TODO: iterate through addresses and print them
// TODO: free allocated data
}
// Receives an address and prints the associated name and service
void get_name(char* ip)
{
int ret;
struct sockaddr_in addr;
char host[1024];
char service[20];
// TODO: fill in address data
// TODO: get name and service
// TODO: print name and service
}
int main(int argc, char **argv)
{
if (argc < 3) {
return usage(argv[0]);
}
if (strncmp(argv[1], "-n", 2) == 0) {
get_ip(argv[2]);
} else if (strncmp(argv[1], "-a", 2) == 0) {
get_name(argv[2]);
} else {
return usage(argv[0]);
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment