/[imapfilter]/imapfilter/connect.c
ViewVC logotype

Annotation of /imapfilter/connect.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations)
Sun Sep 30 20:11:27 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.11: +8 -5 lines
File MIME type: text/plain
Error codes added.

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.12 return ERROR_NETWORK;
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.12 return ERROR_NETWORK;
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.12 return ERROR_NETWORK;
47 lefcha 1.1 }
48 lefcha 1.12
49     strncpy(serv, he->h_name, SERVER_LEN - 1);
50 lefcha 1.1
51 lefcha 1.12 info("Connected to %s.\n", serv);
52     log_info(1, serv);
53 lefcha 1.1
54 lefcha 1.6 return 0;
55 lefcha 1.1 }
56    
57    
58     /*
59 lefcha 1.11 * Disconnect from mail server.
60 lefcha 1.1 */
61     int close_connection(void)
62     {
63     if (close(sock)) {
64     error("imapfilter: closing socket; %s\n", strerror(errno));
65 lefcha 1.12 return ERROR_NETWORK;
66 lefcha 1.6 } else
67     return 0;
68 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26