1 /* $Id: sync.h,v 1.23 2015/06/08 19:05:50 manu Exp $ */
2 /* vim: set sw=8 ts=8 sts=8 noet cino=(0: */
3 
4 /*
5  * Copyright (c) 2004-2007 Emmanuel Dreyfus
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *        This product includes software developed by Emmanuel Dreyfus
19  *
20  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _SYNC_H_
34 #define _SYNC_H_
35 
36 #include "pending.h"
37 #include "milter-greylist.h"
38 
39 #ifndef SYNC_MAXQLEN
40 #define SYNC_MAXQLEN	1024
41 #endif
42 
43 #define CMDLEN 10
44 #define LINELEN 512
45 
46 #define MXGLSYNC_NAME "mxglsync"
47 #define MXGLSYNC_PORT "5252"
48 
49 #define MXGLSYNC_BACKLOG 5 /* Maximum connections */
50 
51 /* socket communication default time out */
52 #define COM_TIMEOUT 3
53 
54 #ifdef HAVE_MISSING_SOCKLEN_T
55 typedef unsigned int socklen_t;
56 #endif
57 
58 #define PEER_WRLOCK WRLOCK(peer_lock);
59 #define PEER_RDLOCK RDLOCK(peer_lock);
60 #define PEER_UNLOCK UNLOCK(peer_lock);
61 
62 LIST_HEAD(peerlist, peer);
63 TAILQ_HEAD(synclist, sync);
64 
65 struct peer {
66 	char *p_name;
67 	FILE *p_stream;
68 	int p_socket;
69 	time_t p_socket_timeout;
70 	/* p_mtx protects p_deferred and p_qlen.
71 	 * peer list must be read or rw locked before. */
72 	pthread_mutex_t p_mtx;
73 	struct synclist p_deferred;
74 	LIST_ENTRY(peer) p_list;
75 	unsigned int p_qlen;
76 	int p_flags;
77 	int p_vers;
78 };
79 
80 #define P_LOCAL	1
81 
82 typedef enum { PS_CREATE, PS_DELETE, PS_DELETE2, PS_FLUSH } peer_sync_t;
83 
84 struct sync {
85 	struct pending *s_pending;
86 	peer_sync_t s_type;
87 	time_t s_autowhite;
88 	TAILQ_ENTRY(sync) s_list;
89 };
90 
91 void peer_init(void);
92 void peer_clear(void);
93 void peer_add(char *, time_t);
94 int peer_connect(struct peer *);
95 void peer_create(struct pending *);
96 void peer_delete(struct pending *, time_t);
97 void peer_flush(struct pending *);
98 
99 int sync_send(struct peer *, peer_sync_t,  struct pending *, time_t);
100 void sync_sender_start(void);
101 void sync_queue(struct peer *, peer_sync_t, struct pending *, time_t);
102 void sync_free(struct sync *);
103 
104 void sync_sender(void *);
105 void sync_master_restart(void);
106 void sync_master_stop(void);
107 void *sync_master(void *);
108 void sync_server(void *);
109 void sync_help(FILE *);
110 int sync_waitdata(int, time_t);
111 
112 
113 #endif /* _SYNC_H_ */
114