xref: /dragonfly/bin/cpdup/hclink.h (revision f746689a)
1 /*
2  * HCLINK.H
3  *
4  * $DragonFly: src/bin/cpdup/hclink.h,v 1.7 2008/05/24 17:21:36 dillon Exp $
5  */
6 
7 #ifndef _HCLINK_H_
8 #define _HCLINK_H_
9 
10 struct HCHostDesc {
11     struct HCHostDesc *next;
12     int desc;
13     int type;
14     void *data;
15 };
16 
17 struct HostConf;
18 
19 typedef struct HCTransaction {
20     struct HCTransaction *next;
21     struct HostConf *hc;
22     u_int16_t	id;		/* assigned transaction id */
23     int		windex;		/* output buffer index */
24     enum { HCT_IDLE, HCT_SENT, HCT_REPLIED, HCT_DONE } state;
25 #if USE_PTHREADS
26     pthread_t	tid;
27     pthread_cond_t cond;
28     int		waiting;
29 #endif
30     char	rbuf[65536];	/* input buffer */
31     char	wbuf[65536];	/* output buffer */
32 } *hctransaction_t;
33 
34 #if USE_PTHREADS
35 #define HCTHASH_SIZE	16
36 #define HCTHASH_MASK	(HCTHASH_SIZE - 1)
37 #endif
38 
39 struct HostConf {
40     char	*host;		/* [user@]host */
41     int		fdin;		/* pipe */
42     int		fdout;		/* pipe */
43     int		error;		/* permanent failure code */
44     pid_t	pid;
45     int		version;	/* cpdup protocol version */
46     struct HCHostDesc *hostdescs;
47 #if USE_PTHREADS
48     pthread_mutex_t hct_mutex[HCTHASH_SIZE];
49     hctransaction_t hct_hash[HCTHASH_SIZE];
50     pthread_t	reader_thread;
51 #else
52     struct HCTransaction trans;
53 #endif
54 };
55 
56 struct HCHead {
57     int32_t magic;		/* magic number / byte ordering */
58     int32_t bytes;		/* size of packet */
59     int16_t cmd;		/* command code */
60     u_int16_t id;			/* transaction id */
61     int32_t error;		/* error code (response) */
62 };
63 
64 #define HCMAGIC		0x48435052	/* compatible byte ordering */
65 #define HCMAGIC_REV	0x52504348	/* reverse byte ordering */
66 #define HCC_ALIGN(bytes)	(((bytes) + 7) & ~7)
67 
68 struct HCLeaf {
69     int16_t leafid;
70     int16_t reserved;		/* reserved must be 0 */
71     int32_t bytes;
72 };
73 
74 #define HCF_REPLY	0x8000		/* reply */
75 
76 #define LCF_TYPEMASK	0x0F00
77 #define LCF_INT32	0x0100		/* 4 byte integer */
78 #define LCF_INT64	0x0200		/* 8 byte integer */
79 #define LCF_STRING	0x0300		/* string, must be 0-terminated */
80 #define LCF_BINARY	0x0F00		/* binary data */
81 
82 #define LCF_NESTED	0x8000
83 
84 struct HCDesc {
85     int16_t cmd;
86     int (*func)(hctransaction_t, struct HCHead *);
87 };
88 
89 /*
90  * Item extraction macros
91  */
92 #define HCC_STRING(item)	((const char *)((item) + 1))
93 #define HCC_INT32(item)		(*(int32_t *)((item) + 1))
94 #define HCC_INT64(item)		(*(int64_t *)((item) + 1))
95 #define HCC_BINARYDATA(item)	((void *)((item) + 1))
96 
97 /*
98  * Prototypes
99  */
100 int hcc_connect(struct HostConf *hc);
101 int hcc_slave(int fdin, int fdout, struct HCDesc *descs, int count);
102 
103 hctransaction_t hcc_start_command(struct HostConf *hc, int16_t cmd);
104 struct HCHead *hcc_finish_command(hctransaction_t trans);
105 void hcc_leaf_string(hctransaction_t trans, int16_t leafid, const char *str);
106 void hcc_leaf_data(hctransaction_t trans, int16_t leafid, const void *ptr, int bytes);
107 void hcc_leaf_int32(hctransaction_t trans, int16_t leafid, int32_t value);
108 void hcc_leaf_int64(hctransaction_t trans, int16_t leafid, int64_t value);
109 
110 int hcc_alloc_descriptor(struct HostConf *hc, void *ptr, int type);
111 void *hcc_get_descriptor(struct HostConf *hc, int desc, int type);
112 void hcc_set_descriptor(struct HostConf *hc, int desc, void *ptr, int type);
113 
114 struct HCLeaf *hcc_firstitem(struct HCHead *head);
115 struct HCLeaf *hcc_nextitem(struct HCHead *head, struct HCLeaf *item);
116 
117 void hcc_debug_dump(struct HCHead *head);
118 void hcc_free_trans(struct HostConf *hc);
119 
120 #endif
121 
122