-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path5.c
62 lines (50 loc) · 1.49 KB
/
5.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
// decompiler
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h> // for close
#define PORT 25
int main(int argc, char *argv[])
{
int sockfd, port;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[1024];
if (argc < 2) {
fprintf(stderr,"usage %s hostname\n", argv[0]);
exit(0);
}
port = PORT;
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
// create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
perror("ERROR opening socket");
// set up the server address
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
// connect to the server
if (connect(sockfd, (const struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
perror("ERROR connecting");
// send the SMTP request
memset(buffer, 0, 1024);
strcpy(buffer, "HELO smtp.example.com\r\n");
if (send(sockfd, buffer, strlen(buffer), 0) < 0)
perror("ERROR writing to socket");
// get the response
memset(buffer, 0, 1024);
if (recv(sockfd, buffer, 1023, 0) < 0)
perror("ERROR reading from socket");
printf("Received response: %s\n", buffer);
close(sockfd);
return 0;
}