1 |
lefcha |
1.11 |
#include <string.h> |
2 |
lefcha |
1.1 |
#include <errno.h> |
3 |
|
|
#include <unistd.h> |
4 |
|
|
#include <sys/types.h> |
5 |
|
|
#include <sys/socket.h> |
6 |
lefcha |
1.11 |
#include <netinet/in.h> |
7 |
lefcha |
1.1 |
#include <netdb.h> |
8 |
lefcha |
1.11 |
|
9 |
lefcha |
1.1 |
|
10 |
|
|
#include "config.h" |
11 |
|
|
#include "imapfilter.h" |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
int sock; |
15 |
|
|
|
16 |
|
|
|
17 |
|
|
/* |
18 |
lefcha |
1.11 |
* Connect to mail server. |
19 |
lefcha |
1.1 |
*/ |
20 |
lefcha |
1.11 |
int init_connection(char *serv, unsigned int port) |
21 |
lefcha |
1.1 |
{ |
22 |
lefcha |
1.11 |
struct sockaddr_in sa; |
23 |
|
|
struct hostent *he; |
24 |
lefcha |
1.1 |
|
25 |
lefcha |
1.11 |
memset((char *) &sa, 0, sizeof(struct sockaddr_in)); |
26 |
lefcha |
1.1 |
|
27 |
|
|
sock = socket(PF_INET, SOCK_STREAM, 0); |
28 |
lefcha |
1.14 |
|
29 |
lefcha |
1.1 |
if (sock < 0) { |
30 |
|
|
error("imapfilter: create socket; %s\n", strerror(errno)); |
31 |
lefcha |
1.12 |
return ERROR_NETWORK; |
32 |
lefcha |
1.1 |
} |
33 |
lefcha |
1.11 |
if (!(he = gethostbyname(serv))) { |
34 |
lefcha |
1.1 |
error("imapfilter: get network host entry; %s\n", strerror(errno)); |
35 |
|
|
close_connection(); |
36 |
lefcha |
1.12 |
return ERROR_NETWORK; |
37 |
lefcha |
1.1 |
} |
38 |
lefcha |
1.11 |
sa.sin_family = AF_INET; |
39 |
|
|
sa.sin_port = htons(port); |
40 |
|
|
sa.sin_addr = *(struct in_addr *) he->h_addr; |
41 |
lefcha |
1.1 |
|
42 |
lefcha |
1.14 |
if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) { |
43 |
lefcha |
1.1 |
error("imapfilter: initiating connection; %s\n", strerror(errno)); |
44 |
|
|
close_connection(); |
45 |
lefcha |
1.12 |
return ERROR_NETWORK; |
46 |
lefcha |
1.1 |
} |
47 |
lefcha |
1.12 |
strncpy(serv, he->h_name, SERVER_LEN - 1); |
48 |
lefcha |
1.1 |
|
49 |
lefcha |
1.12 |
info("Connected to %s.\n", serv); |
50 |
lefcha |
1.13 |
log_info(LOG_SERVER, serv); |
51 |
lefcha |
1.1 |
|
52 |
lefcha |
1.6 |
return 0; |
53 |
lefcha |
1.1 |
} |
54 |
|
|
|
55 |
|
|
|
56 |
|
|
/* |
57 |
lefcha |
1.11 |
* Disconnect from mail server. |
58 |
lefcha |
1.1 |
*/ |
59 |
|
|
int close_connection(void) |
60 |
|
|
{ |
61 |
|
|
if (close(sock)) { |
62 |
|
|
error("imapfilter: closing socket; %s\n", strerror(errno)); |
63 |
lefcha |
1.12 |
return ERROR_NETWORK; |
64 |
lefcha |
1.6 |
} else |
65 |
|
|
return 0; |
66 |
lefcha |
1.1 |
} |