xref: /dragonfly/bin/cpdup/hclink.h (revision 8bf5b238b6869b07d32313f388d8d715ae7c284d)
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 /* Changing the buffer size breaks protocol compatibility! */
11 #define HC_BUFSIZE  65536
12 
13 struct HCHostDesc {
14     struct HCHostDesc *next;
15     intptr_t desc;
16     int type;
17     void *data;
18 };
19 
20 struct HostConf;
21 
22 typedef struct HCTransaction {
23     char  rbuf[HC_BUFSIZE];   /* input buffer */
24     char  wbuf[HC_BUFSIZE];   /* output buffer */
25     struct HostConf *hc;
26     uint16_t        id;                 /* assigned transaction id */
27     int             swap;               /* have to swap byte order */
28     int             windex;             /* output buffer index */
29     enum { HCT_IDLE, HCT_SENT, HCT_REPLIED, HCT_DONE, HCT_FAIL } state;
30 } *hctransaction_t;
31 
32 struct HostConf {
33     char  *host;              /* [user@]host */
34     int             fdin;               /* pipe */
35     int             fdout;              /* pipe */
36     int             error;              /* permanent failure code */
37     pid_t pid;
38     int             version;  /* cpdup protocol version */
39     struct HCHostDesc *hostdescs;
40     struct HCTransaction trans;
41 };
42 
43 struct HCHead {
44     int32_t magic;            /* magic number / byte ordering */
45     int32_t bytes;            /* size of packet */
46     int16_t cmd;              /* command code */
47     uint16_t id;              /* transaction id */
48     int32_t error;            /* error code (response) */
49 } __aligned(8);     /* fix clang warning, not required for correct operation */
50 
51 #define HCMAGIC               0x48435052          /* compatible byte ordering */
52 #define HCC_ALIGN(bytes)      (((bytes) + 7) & ~7)
53 
54 struct HCLeaf {
55     int16_t leafid;
56     int16_t reserved;                   /* reserved must be 0 */
57     int32_t bytes;
58 } __aligned(8);     /* fix clang warning, not required for correct operation */
59 
60 #define HCF_CONTINUE          0x4000              /* expect another reply */
61 #define HCF_REPLY   0x8000              /* reply */
62 
63 #define LCF_TYPEMASK          0x0F00
64 #define LCF_INT32   0x0100              /* 4 byte integer */
65 #define LCF_INT64   0x0200              /* 8 byte integer */
66 #define LCF_STRING  0x0300              /* string, must be 0-terminated */
67 #define LCF_BINARY  0x0F00              /* binary data */
68 
69 struct HCDesc {
70     int16_t cmd;
71     int (*func)(hctransaction_t, struct HCHead *);
72 };
73 
74 /*
75  * Item extraction macros
76  */
77 #define HCC_STRING(item)      ((const char *)((item) + 1))
78 #define HCC_INT32(item)                 (*(int32_t *)((item) + 1))
79 #define HCC_INT64(item)                 (*(int64_t *)((item) + 1))
80 #define HCC_BINARYDATA(item)  ((void *)((item) + 1))
81 
82 #define FOR_EACH_ITEM(item, trans, head)          \
83                     for (item = hcc_firstitem(trans, head); item; \
84                          item = hcc_nextitem(trans, head, item))
85 
86 /*
87  * Prototypes
88  */
89 int hcc_connect(struct HostConf *hc, int readonly);
90 int hcc_slave(int fdin, int fdout, struct HCDesc *descs, int count);
91 
92 struct HCHead *hcc_read_command(struct HostConf *hc, hctransaction_t trans);
93 hctransaction_t hcc_start_command(struct HostConf *hc, int16_t cmd);
94 struct HCHead *hcc_finish_command(hctransaction_t trans);
95 void hcc_leaf_string(hctransaction_t trans, int16_t leafid, const char *str);
96 void hcc_leaf_data(hctransaction_t trans, int16_t leafid, const void *ptr, int bytes);
97 void hcc_leaf_int32(hctransaction_t trans, int16_t leafid, int32_t value);
98 void hcc_leaf_int64(hctransaction_t trans, int16_t leafid, int64_t value);
99 int hcc_check_space(hctransaction_t trans, struct HCHead *head, int n, int size);
100 
101 intptr_t hcc_alloc_descriptor(struct HostConf *hc, void *ptr, int type);
102 void *hcc_get_descriptor(struct HostConf *hc, intptr_t desc, int type);
103 void hcc_set_descriptor(struct HostConf *hc, intptr_t desc, void *ptr, int type);
104 
105 struct HCLeaf *hcc_nextitem(hctransaction_t trans, struct HCHead *head, struct HCLeaf *item);
106 #define hcc_firstitem(trans, head)      hcc_nextitem(trans, head, NULL)
107 struct HCLeaf *hcc_nextchaineditem(struct HostConf *hc, struct HCHead *head);
108 struct HCLeaf *hcc_currentchaineditem(struct HostConf *hc, struct HCHead *head);
109 
110 void hcc_debug_dump(struct HCHead *head);
111 
112 #endif
113 
114