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