25#define RBUF_SZ 16*1024
28#define DPIP_TAG_END " '>"
29#define DPIP_MODE_SWITCH_TAG "cmd='start_send_page' "
30#define MSG_ERR(...) fprintf(stderr, "[dpip]: " __VA_ARGS__)
90 if (strchr(format,
Quote))
95 va_start(argp, format);
96 for (p = q = (
char*)format; *q; ) {
109 s = va_arg (argp,
char *);
136 uint_t i, n = 0, found = 0;
137 const char *p, *q, *start;
138 char *r, *s, *val = NULL;
141 if (!tag || !tagsize || !attrname || !*attrname)
144 for (i = 1; i < tagsize && !found; ++i) {
147 if (tag[i] == attrname[0] && (tag[i-1] ==
' ' || tag[i-1] ==
'<')) {
150 }
else if (tag[i] ==
Quote && tag[i-1] ==
'=')
154 if (tag[i] == attrname[n])
156 else if (tag[i] ==
'=' && !attrname[n])
176 while ((q = strchr(p,
Quote)) && q[1] ==
Quote)
178 if (q && q[1] ==
' ') {
180 for (r = s = val; (*r = *s); ++r, ++s)
181 if (s[0] ==
Quote && s[0] == s[1])
203 char SharedSecret[32];
205 char *fname, *rcline = NULL, *tail, *cmd, *msg;
206 int i, port, ret = -1;
216 if ((In = fopen(fname,
"r")) == NULL) {
218 }
else if ((rcline =
dGetline(In)) == NULL) {
219 MSG_ERR(
"[a_Dpip_check_auth] empty file: %s\n", fname);
221 port = strtol(rcline, &tail, 10);
222 if (tail && port != 0) {
223 for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
224 SharedSecret[i] = tail[i+1];
226 if (strcmp(msg, SharedSecret) == 0)
260 if (fcntl(dsh->
fd_in, F_GETFL) & O_NONBLOCK)
272 int req_mode, old_flags = 0, st, ret = -3, sent = 0;
277 old_flags = fcntl(dsh->
fd_out, F_GETFL);
278 fcntl(dsh->
fd_out, F_SETFL,
279 (nb) ? O_NONBLOCK | old_flags : old_flags & ~O_NONBLOCK);
283 st = write(dsh->
fd_out, Data + sent, DataSize - sent);
285 if (errno == EINTR) {
287 }
else if (errno == EAGAIN) {
298 if (nb || sent == DataSize) {
307 fcntl(dsh->
fd_out, F_SETFL, old_flags);
353 return (dsh->
wrbuf->
len == 0) ? 0 : st;
386 int req_mode, old_flags = 0, st, nb = !blocking;
393 old_flags = fcntl(dsh->
fd_in, F_GETFL);
394 fcntl(dsh->
fd_in, F_SETFL,
395 (nb) ? O_NONBLOCK | old_flags : old_flags & ~O_NONBLOCK);
401 if (errno == EINTR) {
403 }
else if (errno == EAGAIN) {
411 }
else if (st == 0) {
424 fcntl(dsh->
fd_out, F_SETFL, old_flags);
440 char *p, *ret = NULL;
457 }
while (!p && dsh->
status == EAGAIN);
472 *DataSize = p - dsh->
rdbuf->
str + 3;
char * dGetline(FILE *stream)
Get a line from a FILE stream.
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
void dStr_append(Dstr *ds, const char *s)
Append a C string to a Dstr.
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
void dStr_erase(Dstr *ds, int pos_0, int len)
Erase a substring.
void dStr_free(Dstr *ds, int all)
Free a dillo string.
int dClose(int fd)
Close a FD handling EINTR.
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
void dStr_append_c(Dstr *ds, int c)
Append one character.
char * dStrndup(const char *s, size_t sz)
void dStr_truncate(Dstr *ds, int len)
Truncate a Dstr to be 'len' bytes long.
char * dGethomedir(void)
Return the home directory in a static string (don't free)
#define dNew(type, count)
int a_Dpip_dsh_trywrite(Dsh *dsh, const char *Data, int DataSize)
char * a_Dpip_dsh_read_token2(Dsh *dsh, int blocking, int *DataSize)
Return a newlly allocated string with the next dpip token in the socket.
void a_Dpip_dsh_free(Dsh *dsh)
Free the SockHandler structure.
char * a_Dpip_build_cmd(const char *format,...)
Printf like function for building dpip commands.
int a_Dpip_dsh_write_str(Dsh *dsh, int flush, const char *str)
Convenience function.
static int Dpip_dsh_write(Dsh *dsh, int nb, const char *Data, int DataSize)
char * a_Dpip_get_attr(const char *tag, const char *attrname)
Task: given a tag and an attribute name, return its value.
char * a_Dpip_dsh_read_token(Dsh *dsh, int blocking)
Return a newlly allocated string with the next dpip token in the socket.
void a_Dpip_dsh_close(Dsh *dsh)
Close this socket for reading and writing.
static void Dpip_dsh_read(Dsh *dsh, int blocking)
Read raw data from the socket into our buffer in either BLOCKING or NONBLOCKING mode.
char * a_Dpip_get_attr_l(const char *tag, size_t tagsize, const char *attrname)
Task: given a tag, its size and an attribute name, return the attribute value (stuffing of ' is remov...
int a_Dpip_check_auth(const char *auth_tag)
Check whether the given 'auth' string equals what dpid saved.
int a_Dpip_dsh_write(Dsh *dsh, int flush, const char *Data, int DataSize)
Streamed write to socket.
#define DPIP_MODE_SWITCH_TAG
int a_Dpip_dsh_tryflush(Dsh *dsh)
Dsh * a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz)
Create and initialize a dpip socket handler.
#define DPIP_RAW
Raw data in the socket
#define DPIP_TAG
Dpip tags in the socket.
#define DPIP_LAST_TAG
Dpip mode-switching tag.
#define DPIP_NONBLOCK
Nonblocking IO
Dpip socket handler type.
int mode
mode flags: DPIP_TAG | DPIP_LAST_TAG | DPIP_RAW
int status
status code: DPIP_EAGAIN | DPIP_ERROR | DPIP_EOF
int flush_sz
max size before flush