static int msg_allocfill(msg_t *m) { m->mode = M_FAKE; msg_fill(m); m->buf = zalloc(m->filled); m->len = m->filled; m->mode = M_FILL; return msg_fill(m); }
int tea_thread_msg_push(int tid, INET_ADDR *addr, void *msg, int msg_size) { TEA_MSG *tmsg; int r = 0; pthreadex_lock_get_shared(&ttable_lock); if(ttable[tid]) { if(ttable[tid]->mqueue) { tmsg = msg_get(); if(addr) msg_set_addr(tmsg, addr); msg_fill(tmsg, msg, msg_size); mqueue_push(ttable[tid]->mqueue, tmsg); pthreadex_flag_up(&(ttable[tid]->mwaiting)); } else DBG("[tea] network message ignored by %d", tid); } else r = -1; pthreadex_lock_release(); return r; }
/* Return the next character in the source, i.e. the first character that will be returned by the next call to msg_gets(). This is currently only used to check for RFC 2822 header continuation lines. It is not safe to use in conjunction with msg_getb(). */ int msg_nextc (msg_source_t source) { assert (source != NULL); if (source->rn <= 0 && !msg_fill (source)) return -1; return *source->rp; }
/* Block oriented reader. The output buffer is not used for efficiency. */ const char * msg_getb (msg_source_t source, int *len) { assert (source != NULL); if (source->rn <= 0 && !msg_fill (source)) return NULL; /* Just return whatever is in the input buffer */ *len = source->rn; source->rn = 0; return source->rp; }
static bool set(char *key, char *value, int timeout_ms) { Message msg, answer; msg_fill(&msg, MEAN_SET, key, value); if (query(&msg, &answer, timeout_ms)) { if (answer.meaning == MEAN_OK) { return true; } else { assert(answer.meaning == MEAN_FAIL); } } return false; }
static char *get(char *key, int timeout_ms) { Message msg, answer; msg_fill(&msg, MEAN_GET, key, ""); if (query(&msg, &answer, timeout_ms)) { if (answer.meaning == MEAN_OK) { return strndup(answer.value.data, sizeof(answer.value)); } else { assert(answer.meaning == MEAN_FAIL); } } return NULL; }
/* Line oriented reader. An output buffer is allocated as required. The return value is a pointer to the line and remains valid until the next call to msg_gets (). The line is guaranteed to be terminated with a \r\n. Len is set to the number of octets in the buffer. The line is *not* terminated with a \0. If concatenate is non-zero, the next line of input is concatenated with the existing line and the return value points to the original line in the buffer. In this case *len is the number of octets in the buffer when called and is updated to the new count on return. */ const char * msg_gets (msg_source_t source, int *len, int concatenate) { int lastc, c, buflen; char *p, *nbuf; assert (source != NULL && len != NULL); if (source->rn <= 0 && !msg_fill (source)) return NULL; if (source->buf == NULL) { source->nalloc = 1023; /* RFC 2821 max line length + slack */ source->buf = malloc (source->nalloc + 2); if (source->buf == NULL) return NULL; } p = source->buf; buflen = source->nalloc; if (concatenate) { p += *len; buflen -= *len; } lastc = 0; while (source->rn > 0 || msg_fill (source)) { c = *source->rp++; source->rn--; if (buflen <= 0) { buflen = 512; source->nalloc += buflen; nbuf = realloc (source->buf, source->nalloc + 2); if (nbuf == NULL) { free (source->buf); return NULL; } p = nbuf + (p - source->buf); source->buf = nbuf; } *p++ = c; buflen--; if (c == '\n' && lastc == '\r') { *len = p - source->buf; return source->buf; } lastc = c; } /* Only get here if the input was not properly terminated with a \r\n. The handling of the DATA command in protocol.c relies on the \n so we add it here. This is why there is 2 characters of slack in malloc and realloc above. */ if (lastc != '\r') *p++ = '\r'; *p++ = '\n'; *len = p - source->buf; return source->buf; }
static int msg_send(msg_t *m, int fd) { m->mode = M_FD; m->fd = fd; return msg_fill(m); }