xref: /dflybsd-src/bin/cpdup/hclink.h (revision 8bf5b238b6869b07d32313f388d8d715ae7c284d)
14d858d58SMatthew Dillon /*
24d858d58SMatthew Dillon  * HCLINK.H
34d858d58SMatthew Dillon  *
4713d03c0SMatthew Dillon  * $DragonFly: src/bin/cpdup/hclink.h,v 1.7 2008/05/24 17:21:36 dillon Exp $
54d858d58SMatthew Dillon  */
64d858d58SMatthew Dillon 
74d858d58SMatthew Dillon #ifndef _HCLINK_H_
84d858d58SMatthew Dillon #define _HCLINK_H_
94d858d58SMatthew Dillon 
10c0538630SMatthew Dillon /* Changing the buffer size breaks protocol compatibility! */
11c0538630SMatthew Dillon #define HC_BUFSIZE	65536
12c0538630SMatthew Dillon 
134d858d58SMatthew Dillon struct HCHostDesc {
144d858d58SMatthew Dillon     struct HCHostDesc *next;
15c0538630SMatthew Dillon     intptr_t desc;
164d858d58SMatthew Dillon     int type;
174d858d58SMatthew Dillon     void *data;
184d858d58SMatthew Dillon };
194d858d58SMatthew Dillon 
20a2dc574cSMatthew Dillon struct HostConf;
21a2dc574cSMatthew Dillon 
22a2dc574cSMatthew Dillon typedef struct HCTransaction {
23c0538630SMatthew Dillon     char	rbuf[HC_BUFSIZE];	/* input buffer */
24c0538630SMatthew Dillon     char	wbuf[HC_BUFSIZE];	/* output buffer */
25a2dc574cSMatthew Dillon     struct HostConf *hc;
26c0538630SMatthew Dillon     uint16_t	id;		/* assigned transaction id */
27c0538630SMatthew Dillon     int		swap;		/* have to swap byte order */
28a2dc574cSMatthew Dillon     int		windex;		/* output buffer index */
29*8bf5b238SMatthew Dillon     enum { HCT_IDLE, HCT_SENT, HCT_REPLIED, HCT_DONE, HCT_FAIL } state;
30a2dc574cSMatthew Dillon } *hctransaction_t;
31a2dc574cSMatthew Dillon 
324d858d58SMatthew Dillon struct HostConf {
334d858d58SMatthew Dillon     char	*host;		/* [user@]host */
344d858d58SMatthew Dillon     int		fdin;		/* pipe */
354d858d58SMatthew Dillon     int		fdout;		/* pipe */
364d858d58SMatthew Dillon     int		error;		/* permanent failure code */
374d858d58SMatthew Dillon     pid_t	pid;
38a2dc574cSMatthew Dillon     int		version;	/* cpdup protocol version */
394d858d58SMatthew Dillon     struct HCHostDesc *hostdescs;
40a2dc574cSMatthew Dillon     struct HCTransaction trans;
414d858d58SMatthew Dillon };
424d858d58SMatthew Dillon 
434d858d58SMatthew Dillon struct HCHead {
444d858d58SMatthew Dillon     int32_t magic;		/* magic number / byte ordering */
454d858d58SMatthew Dillon     int32_t bytes;		/* size of packet */
464d858d58SMatthew Dillon     int16_t cmd;		/* command code */
47c0538630SMatthew Dillon     uint16_t id;		/* transaction id */
484d858d58SMatthew Dillon     int32_t error;		/* error code (response) */
4975bd842aSMatthew Dillon } __aligned(8);	/* fix clang warning, not required for correct operation */
504d858d58SMatthew Dillon 
514d858d58SMatthew Dillon #define HCMAGIC		0x48435052	/* compatible byte ordering */
524d858d58SMatthew Dillon #define HCC_ALIGN(bytes)	(((bytes) + 7) & ~7)
534d858d58SMatthew Dillon 
544d858d58SMatthew Dillon struct HCLeaf {
554d858d58SMatthew Dillon     int16_t leafid;
564d858d58SMatthew Dillon     int16_t reserved;		/* reserved must be 0 */
574d858d58SMatthew Dillon     int32_t bytes;
5875bd842aSMatthew Dillon } __aligned(8);	/* fix clang warning, not required for correct operation */
594d858d58SMatthew Dillon 
60c0538630SMatthew Dillon #define HCF_CONTINUE	0x4000		/* expect another reply */
614d858d58SMatthew Dillon #define HCF_REPLY	0x8000		/* reply */
624d858d58SMatthew Dillon 
634d858d58SMatthew Dillon #define LCF_TYPEMASK	0x0F00
644d858d58SMatthew Dillon #define LCF_INT32	0x0100		/* 4 byte integer */
654d858d58SMatthew Dillon #define LCF_INT64	0x0200		/* 8 byte integer */
664d858d58SMatthew Dillon #define LCF_STRING	0x0300		/* string, must be 0-terminated */
674d858d58SMatthew Dillon #define LCF_BINARY	0x0F00		/* binary data */
684d858d58SMatthew Dillon 
694d858d58SMatthew Dillon struct HCDesc {
704d858d58SMatthew Dillon     int16_t cmd;
71a2dc574cSMatthew Dillon     int (*func)(hctransaction_t, struct HCHead *);
724d858d58SMatthew Dillon };
734d858d58SMatthew Dillon 
744d858d58SMatthew Dillon /*
754d858d58SMatthew Dillon  * Item extraction macros
764d858d58SMatthew Dillon  */
774d858d58SMatthew Dillon #define HCC_STRING(item)	((const char *)((item) + 1))
784d858d58SMatthew Dillon #define HCC_INT32(item)		(*(int32_t *)((item) + 1))
794d858d58SMatthew Dillon #define HCC_INT64(item)		(*(int64_t *)((item) + 1))
804d858d58SMatthew Dillon #define HCC_BINARYDATA(item)	((void *)((item) + 1))
814d858d58SMatthew Dillon 
82c0538630SMatthew Dillon #define FOR_EACH_ITEM(item, trans, head)	\
83c0538630SMatthew Dillon 		for (item = hcc_firstitem(trans, head); item; \
84c0538630SMatthew Dillon 		     item = hcc_nextitem(trans, head, item))
85c0538630SMatthew Dillon 
864d858d58SMatthew Dillon /*
874d858d58SMatthew Dillon  * Prototypes
884d858d58SMatthew Dillon  */
89c0538630SMatthew Dillon int hcc_connect(struct HostConf *hc, int readonly);
904d858d58SMatthew Dillon int hcc_slave(int fdin, int fdout, struct HCDesc *descs, int count);
914d858d58SMatthew Dillon 
92c0538630SMatthew Dillon struct HCHead *hcc_read_command(struct HostConf *hc, hctransaction_t trans);
93a2dc574cSMatthew Dillon hctransaction_t hcc_start_command(struct HostConf *hc, int16_t cmd);
94a2dc574cSMatthew Dillon struct HCHead *hcc_finish_command(hctransaction_t trans);
95a2dc574cSMatthew Dillon void hcc_leaf_string(hctransaction_t trans, int16_t leafid, const char *str);
96a2dc574cSMatthew Dillon void hcc_leaf_data(hctransaction_t trans, int16_t leafid, const void *ptr, int bytes);
97a2dc574cSMatthew Dillon void hcc_leaf_int32(hctransaction_t trans, int16_t leafid, int32_t value);
98a2dc574cSMatthew Dillon void hcc_leaf_int64(hctransaction_t trans, int16_t leafid, int64_t value);
99c0538630SMatthew Dillon int hcc_check_space(hctransaction_t trans, struct HCHead *head, int n, int size);
1004d858d58SMatthew Dillon 
101c0538630SMatthew Dillon intptr_t hcc_alloc_descriptor(struct HostConf *hc, void *ptr, int type);
102c0538630SMatthew Dillon void *hcc_get_descriptor(struct HostConf *hc, intptr_t desc, int type);
103c0538630SMatthew Dillon void hcc_set_descriptor(struct HostConf *hc, intptr_t desc, void *ptr, int type);
1044d858d58SMatthew Dillon 
105c0538630SMatthew Dillon struct HCLeaf *hcc_nextitem(hctransaction_t trans, struct HCHead *head, struct HCLeaf *item);
106c0538630SMatthew Dillon #define hcc_firstitem(trans, head)	hcc_nextitem(trans, head, NULL)
107c0538630SMatthew Dillon struct HCLeaf *hcc_nextchaineditem(struct HostConf *hc, struct HCHead *head);
108c0538630SMatthew Dillon struct HCLeaf *hcc_currentchaineditem(struct HostConf *hc, struct HCHead *head);
1094d858d58SMatthew Dillon 
1104d858d58SMatthew Dillon void hcc_debug_dump(struct HCHead *head);
1114d858d58SMatthew Dillon 
1124d858d58SMatthew Dillon #endif
1134d858d58SMatthew Dillon 
114