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 |
|
|
if (sock < 0) { |
29 |
|
|
error("imapfilter: create socket; %s\n", strerror(errno)); |
30 |
lefcha |
1.6 |
return 1; |
31 |
lefcha |
1.1 |
} |
32 |
|
|
|
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.6 |
return 1; |
37 |
lefcha |
1.1 |
} |
38 |
|
|
|
39 |
lefcha |
1.11 |
sa.sin_family = AF_INET; |
40 |
|
|
sa.sin_port = htons(port); |
41 |
|
|
sa.sin_addr = *(struct in_addr *) he->h_addr; |
42 |
lefcha |
1.1 |
|
43 |
lefcha |
1.11 |
if (connect(sock, (struct sockaddr *) &sa, sizeof(struct sockaddr))) { |
44 |
lefcha |
1.1 |
error("imapfilter: initiating connection; %s\n", strerror(errno)); |
45 |
|
|
close_connection(); |
46 |
lefcha |
1.6 |
return 1; |
47 |
lefcha |
1.1 |
} |
48 |
|
|
|
49 |
lefcha |
1.11 |
info("Connected to %s.\n", he->h_name); |
50 |
lefcha |
1.1 |
|
51 |
lefcha |
1.6 |
return 0; |
52 |
lefcha |
1.1 |
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/* |
56 |
lefcha |
1.11 |
* Disconnect from mail server. |
57 |
lefcha |
1.1 |
*/ |
58 |
|
|
int close_connection(void) |
59 |
|
|
{ |
60 |
|
|
if (close(sock)) { |
61 |
|
|
error("imapfilter: closing socket; %s\n", strerror(errno)); |
62 |
lefcha |
1.6 |
return 1; |
63 |
|
|
} else |
64 |
|
|
return 0; |
65 |
lefcha |
1.1 |
} |