xref: /dflybsd-src/bin/cpdup/hclink.h (revision a2dc574ce267db75e11037efdd569f64845072ec)
14d858d58SMatthew Dillon /*
24d858d58SMatthew Dillon  * HCLINK.H
34d858d58SMatthew Dillon  *
4*a2dc574cSMatthew Dillon  * $DragonFly: src/bin/cpdup/hclink.h,v 1.2 2008/04/10 22:09:08 dillon Exp $
54d858d58SMatthew Dillon  */
64d858d58SMatthew Dillon 
74d858d58SMatthew Dillon #ifndef _HCLINK_H_
84d858d58SMatthew Dillon #define _HCLINK_H_
94d858d58SMatthew Dillon 
104d858d58SMatthew Dillon struct HCHostDesc {
114d858d58SMatthew Dillon     struct HCHostDesc *next;
124d858d58SMatthew Dillon     int desc;
134d858d58SMatthew Dillon     int type;
144d858d58SMatthew Dillon     void *data;
154d858d58SMatthew Dillon };
164d858d58SMatthew Dillon 
17*a2dc574cSMatthew Dillon struct HostConf;
18*a2dc574cSMatthew Dillon 
19*a2dc574cSMatthew Dillon typedef struct HCTransaction {
20*a2dc574cSMatthew Dillon     struct HCTransaction *next;
21*a2dc574cSMatthew Dillon     struct HostConf *hc;
22*a2dc574cSMatthew Dillon     u_int16_t	id;		/* assigned transaction id */
23*a2dc574cSMatthew Dillon     int		windex;		/* output buffer index */
24*a2dc574cSMatthew Dillon     enum { HCT_IDLE, HCT_SENT, HCT_REPLIED } state;
25*a2dc574cSMatthew Dillon #if USE_PTHREADS
26*a2dc574cSMatthew Dillon     pthread_t	tid;
27*a2dc574cSMatthew Dillon #endif
28*a2dc574cSMatthew Dillon     char	rbuf[65536];	/* input buffer */
29*a2dc574cSMatthew Dillon     char	wbuf[65536];	/* output buffer */
30*a2dc574cSMatthew Dillon } *hctransaction_t;
31*a2dc574cSMatthew Dillon 
32*a2dc574cSMatthew Dillon #if USE_PTHREADS
33*a2dc574cSMatthew Dillon #define HCTHASH_SIZE	16
34*a2dc574cSMatthew Dillon #define HCTHASH_MASK	(HCTHASH_SIZE - 1)
35*a2dc574cSMatthew Dillon #endif
36*a2dc574cSMatthew Dillon 
374d858d58SMatthew Dillon struct HostConf {
384d858d58SMatthew Dillon     char	*host;		/* [user@]host */
394d858d58SMatthew Dillon     int		fdin;		/* pipe */
404d858d58SMatthew Dillon     int		fdout;		/* pipe */
414d858d58SMatthew Dillon     int		error;		/* permanent failure code */
424d858d58SMatthew Dillon     pid_t	pid;
43*a2dc574cSMatthew Dillon     int		version;	/* cpdup protocol version */
444d858d58SMatthew Dillon     struct HCHostDesc *hostdescs;
45*a2dc574cSMatthew Dillon #if USE_PTHREADS
46*a2dc574cSMatthew Dillon     pthread_mutex_t read_mutex;
47*a2dc574cSMatthew Dillon     hctransaction_t hct_hash[HCTHASH_SIZE];
48*a2dc574cSMatthew Dillon #else
49*a2dc574cSMatthew Dillon     struct HCTransaction trans;
50*a2dc574cSMatthew Dillon #endif
514d858d58SMatthew Dillon };
524d858d58SMatthew Dillon 
534d858d58SMatthew Dillon struct HCHead {
544d858d58SMatthew Dillon     int32_t magic;		/* magic number / byte ordering */
554d858d58SMatthew Dillon     int32_t bytes;		/* size of packet */
564d858d58SMatthew Dillon     int16_t cmd;		/* command code */
57*a2dc574cSMatthew Dillon     u_int16_t id;			/* transaction id */
584d858d58SMatthew Dillon     int32_t error;		/* error code (response) */
594d858d58SMatthew Dillon };
604d858d58SMatthew Dillon 
614d858d58SMatthew Dillon #define HCMAGIC		0x48435052	/* compatible byte ordering */
624d858d58SMatthew Dillon #define HCMAGIC_REV	0x52504348	/* reverse byte ordering */
634d858d58SMatthew Dillon #define HCC_ALIGN(bytes)	(((bytes) + 7) & ~7)
644d858d58SMatthew Dillon 
654d858d58SMatthew Dillon struct HCLeaf {
664d858d58SMatthew Dillon     int16_t leafid;
674d858d58SMatthew Dillon     int16_t reserved;		/* reserved must be 0 */
684d858d58SMatthew Dillon     int32_t bytes;
694d858d58SMatthew Dillon };
704d858d58SMatthew Dillon 
714d858d58SMatthew Dillon #define HCF_REPLY	0x8000		/* reply */
724d858d58SMatthew Dillon 
734d858d58SMatthew Dillon #define LCF_TYPEMASK	0x0F00
744d858d58SMatthew Dillon #define LCF_INT32	0x0100		/* 4 byte integer */
754d858d58SMatthew Dillon #define LCF_INT64	0x0200		/* 8 byte integer */
764d858d58SMatthew Dillon #define LCF_STRING	0x0300		/* string, must be 0-terminated */
774d858d58SMatthew Dillon #define LCF_BINARY	0x0F00		/* binary data */
784d858d58SMatthew Dillon 
794d858d58SMatthew Dillon #define LCF_NESTED	0x8000
804d858d58SMatthew Dillon 
814d858d58SMatthew Dillon struct HCDesc {
824d858d58SMatthew Dillon     int16_t cmd;
83*a2dc574cSMatthew Dillon     int (*func)(hctransaction_t, struct HCHead *);
844d858d58SMatthew Dillon };
854d858d58SMatthew Dillon 
864d858d58SMatthew Dillon /*
874d858d58SMatthew Dillon  * Item extraction macros
884d858d58SMatthew Dillon  */
894d858d58SMatthew Dillon #define HCC_STRING(item)	((const char *)((item) + 1))
904d858d58SMatthew Dillon #define HCC_INT32(item)		(*(int32_t *)((item) + 1))
914d858d58SMatthew Dillon #define HCC_INT64(item)		(*(int64_t *)((item) + 1))
924d858d58SMatthew Dillon #define HCC_BINARYDATA(item)	((void *)((item) + 1))
934d858d58SMatthew Dillon 
944d858d58SMatthew Dillon /*
954d858d58SMatthew Dillon  * Prototypes
964d858d58SMatthew Dillon  */
974d858d58SMatthew Dillon int hcc_connect(struct HostConf *hc);
984d858d58SMatthew Dillon int hcc_slave(int fdin, int fdout, struct HCDesc *descs, int count);
994d858d58SMatthew Dillon 
100*a2dc574cSMatthew Dillon hctransaction_t hcc_start_command(struct HostConf *hc, int16_t cmd);
101*a2dc574cSMatthew Dillon struct HCHead *hcc_finish_command(hctransaction_t trans);
102*a2dc574cSMatthew Dillon void hcc_leaf_string(hctransaction_t trans, int16_t leafid, const char *str);
103*a2dc574cSMatthew Dillon void hcc_leaf_data(hctransaction_t trans, int16_t leafid, const void *ptr, int bytes);
104*a2dc574cSMatthew Dillon void hcc_leaf_int32(hctransaction_t trans, int16_t leafid, int32_t value);
105*a2dc574cSMatthew Dillon void hcc_leaf_int64(hctransaction_t trans, int16_t leafid, int64_t value);
1064d858d58SMatthew Dillon 
1074d858d58SMatthew Dillon int hcc_alloc_descriptor(struct HostConf *hc, void *ptr, int type);
1084d858d58SMatthew Dillon void *hcc_get_descriptor(struct HostConf *hc, int desc, int type);
1094d858d58SMatthew Dillon void hcc_set_descriptor(struct HostConf *hc, int desc, void *ptr, int type);
1104d858d58SMatthew Dillon 
1114d858d58SMatthew Dillon struct HCLeaf *hcc_firstitem(struct HCHead *head);
1124d858d58SMatthew Dillon struct HCLeaf *hcc_nextitem(struct HCHead *head, struct HCLeaf *item);
1134d858d58SMatthew Dillon 
1144d858d58SMatthew Dillon void hcc_debug_dump(struct HCHead *head);
1154d858d58SMatthew Dillon 
1164d858d58SMatthew Dillon #endif
1174d858d58SMatthew Dillon 
118