1 /*
2  * Library for dealing with dpip tags (dillo plugin protocol tags).
3  */
4 
5 #ifndef __DPIP_H__
6 #define __DPIP_H__
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif /* __cplusplus */
11 
12 #include "../dlib/dlib.h"
13 
14 /*
15  * Communication mode flags
16  */
17 #define   DPIP_TAG        1   /* Dpip tags in the socket */
18 #define   DPIP_LAST_TAG   2   /* Dpip mode-switching tag */
19 #define   DPIP_RAW        4   /* Raw data in the socket  */
20 #define   DPIP_NONBLOCK   8   /* Nonblocking IO          */
21 
22 typedef enum {
23    DPIP_EAGAIN,
24    DPIP_ERROR,
25    DPIP_EOF
26 } DpipDshStatus;
27 
28 /*
29  * Dpip socket handler type.
30  */
31 typedef struct {
32    int fd_in;
33    int fd_out;
34    /* FILE *in;    --Unused. The stream functions block when reading. */
35    FILE *out;
36 
37    Dstr *wrbuf;    /* write buffer */
38    Dstr *rdbuf;    /* read buffer */
39    int flush_sz;   /* max size before flush */
40 
41    int mode;       /* mode flags: DPIP_TAG | DPIP_LAST_TAG | DPIP_RAW */
42    int status;     /* status code: DPIP_EAGAIN | DPIP_ERROR | DPIP_EOF */
43 } Dsh;
44 
45 
46 /*
47  * Printf like function for building dpip commands.
48  * It takes care of dpip escaping of its arguments.
49  * NOTE : It ONLY accepts string parameters, and
50  *        only one %s per parameter.
51  */
52 char *a_Dpip_build_cmd(const char *format, ...);
53 
54 /*
55  * Task: given a tag and an attribute name, return its value.
56  *       (dpip character escaping is removed here)
57  * Return value: the attribute value, or NULL if not present or malformed.
58  */
59 char *a_Dpip_get_attr(const char *tag, const char *attrname);
60 char *a_Dpip_get_attr_l(const char *tag, size_t tagsize, const char *attrname);
61 
62 int a_Dpip_check_auth(const char *auth);
63 
64 /*
65  * Dpip socket API
66  */
67 Dsh *a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz);
68 int a_Dpip_dsh_write(Dsh *dsh, int flush, const char *Data, int DataSize);
69 int a_Dpip_dsh_write_str(Dsh *dsh, int flush, const char *str);
70 int a_Dpip_dsh_tryflush(Dsh *dsh);
71 int a_Dpip_dsh_trywrite(Dsh *dsh, const char *Data, int DataSize);
72 char *a_Dpip_dsh_read_token(Dsh *dsh, int blocking);
73 char *a_Dpip_dsh_read_token2(Dsh *dsh, int blocking, int *DataSize);
74 void a_Dpip_dsh_close(Dsh *dsh);
75 void a_Dpip_dsh_free(Dsh *dsh);
76 
77 #define a_Dpip_dsh_printf(sh, flush, ...)                   \
78    D_STMT_START {                                           \
79       Dstr *dstr = dStr_sized_new(128);                     \
80       dStr_sprintf(dstr, __VA_ARGS__);                      \
81       a_Dpip_dsh_write(sh, flush, dstr->str, dstr->len);    \
82       dStr_free(dstr, 1);                                   \
83    } D_STMT_END
84 
85 #ifdef __cplusplus
86 }
87 #endif /* __cplusplus */
88 
89 #endif /* __DPIP_H__ */
90 
91