3 * - output and logging support
4 * - program lifetime support
5 * - IP address and subnet munging routines
6 * - MPI convenience functions
9 * This file is part of secnet.
10 * See README for full list of copyright holders.
12 * secnet is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * secnet is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * version 3 along with secnet; if not, see
24 * https://www.gnu.org/licenses/gpl.html.
37 #include "unaligned.h"
41 #define MIN_BUFFER_SIZE 64
42 #define DEFAULT_BUFFER_SIZE 4096
43 #define MAX_BUFFER_SIZE 131072
45 static const char *hexdigits="0123456789abcdef";
47 uint32_t current_phase=0;
52 LIST_ENTRY(phase_hook) entry;
55 static LIST_HEAD(, phase_hook) hooks[NR_PHASES];
57 char *safe_strdup(const char *s, const char *message)
62 fatal_perror("%s",message);
67 void *safe_malloc(size_t size, const char *message)
74 fatal_perror("%s",message);
78 void *safe_realloc_ary(void *p, size_t size, size_t count,
79 const char *message) {
80 if (count >= INT_MAX/size) {
81 fatal("array allocation overflow: %s", message);
83 assert(size && count);
84 p = realloc(p, size*count);
86 fatal_perror("%s", message);
90 void *safe_malloc_ary(size_t size, size_t count, const char *message) {
93 return safe_realloc_ary(0,size,count,message);
96 void hex_encode(const uint8_t *bin, int binsize, char *buff)
100 for (i=0; i<binsize; i++) {
101 buff[i*2]=hexdigits[(bin[i] & 0xf0) >> 4];
102 buff[i*2+1]=hexdigits[(bin[i] & 0xf)];
107 string_t hex_encode_alloc(const uint8_t *bin, int binsize)
111 buff=safe_malloc(hex_encode_size(binsize),"hex_encode");
112 hex_encode(bin,binsize,buff);
116 static uint8_t hexval(uint8_t c)
145 bool_t hex_decode(uint8_t *buffer, int32_t buflen, int32_t *outlen,
146 cstring_t hb, bool_t allow_odd_nibble)
148 int i = 0, j = 0, l = strlen(hb), hi, lo;
151 if (!l || !buflen) { ok = !l; goto done; }
153 /* The number starts with a half-byte */
154 if (!allow_odd_nibble) goto done;
155 lo = hexval(hb[j++]); if (lo < 0) goto done;
158 for (; hb[j] && i < buflen; i++) {
159 hi = hexval(hb[j++]);
160 lo = hexval(hb[j++]);
161 if (hi < 0 || lo < 0) goto done;
162 buffer[i] = (hi << 4) | lo;
170 void read_mpbin(MP_INT *a, uint8_t *bin, int binsize)
172 char *buff = hex_encode_alloc(bin, binsize);
173 mpz_set_str(a, buff, 16);
177 char *write_mpstring(MP_INT *a)
181 buff=safe_malloc(mpz_sizeinbase(a,16)+2,"write_mpstring");
182 mpz_get_str(buff, 16, a);
186 int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen)
188 char *hb = write_mpstring(a);
190 hex_decode(buffer, buflen, &len, hb, True);
195 #define DEFINE_SETFDFLAG(fn,FL,FLAG) \
197 int r=fcntl(fd, F_GET##FL); \
198 if (r<0) fatal_perror("fcntl(,F_GET" #FL ") failed"); \
199 r=fcntl(fd, F_SET##FL, r|FLAG); \
200 if (r<0) fatal_perror("fcntl(,F_SET" #FL ",|" #FLAG ") failed"); \
203 DEFINE_SETFDFLAG(setcloexec,FD,FD_CLOEXEC);
204 DEFINE_SETFDFLAG(setnonblock,FL,O_NONBLOCK);
206 void pipe_cloexec(int fd[2]) {
208 if (r) fatal_perror("pipe");
213 static const char *phases[NR_PHASES]={
219 "PHASE_GETRESOURCES",
226 void enter_phase(uint32_t new_phase)
228 struct phase_hook *i;
230 if (!LIST_EMPTY(&hooks[new_phase]))
231 Message(M_DEBUG_PHASE,"Running hooks for %s...\n", phases[new_phase]);
232 current_phase=new_phase;
234 LIST_FOREACH(i, &hooks[new_phase], entry)
235 i->fn(i->state, new_phase);
236 Message(M_DEBUG_PHASE,"Now in %s\n",phases[new_phase]);
239 void phase_hooks_init(void)
242 for (i=0; i<NR_PHASES; i++)
243 LIST_INIT(&hooks[i]);
246 void clear_phase_hooks(uint32_t phase)
248 struct phase_hook *h, *htmp;
249 LIST_FOREACH_SAFE(h, &hooks[phase], entry, htmp)
251 LIST_INIT(&hooks[phase]);
254 bool_t add_hook(uint32_t phase, hook_fn *fn, void *state)
256 struct phase_hook *h;
261 LIST_INSERT_HEAD(&hooks[phase],h,entry);
265 bool_t remove_hook(uint32_t phase, hook_fn *fn, void *state)
267 fatal("remove_hook: not implemented");
272 void vslilog(struct log_if *lf, int priority, const char *message, va_list ap)
274 lf->vlogfn(lf->st,priority,message,ap);
277 void slilog(struct log_if *lf, int priority, const char *message, ...)
281 va_start(ap,message);
282 vslilog(lf,priority,message,ap);
288 struct buffer_if ops;
291 void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
295 fprintf(stderr,"secnet: BUF_ASSERT_FREE, %s line %d, owned by %s",
296 file,line,buffer->owner);
297 assert(!"buffer_assert_free failure");
301 void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
305 fprintf(stderr,"secnet: BUF_ASSERT_USED, %s line %d, last owned by %s",
306 file,line,buffer->owner);
307 assert(!"buffer_assert_used failure");
311 void buffer_init(struct buffer_if *buffer, int32_t max_start_pad)
313 assert(max_start_pad<=buffer->alloclen);
314 buffer->start=buffer->base+max_start_pad;
318 void buffer_destroy(struct buffer_if *buf)
320 BUF_ASSERT_FREE(buf);
322 buf->start=buf->base=0;
323 buf->size=buf->alloclen=0;
326 void *buf_append(struct buffer_if *buf, int32_t amount) {
328 assert(amount <= buf_remaining_space(buf));
329 p=buf->start + buf->size;
334 void *buf_prepend(struct buffer_if *buf, int32_t amount) {
335 assert(amount <= buf->start - buf->base);
337 return buf->start-=amount;
340 void *buf_unappend(struct buffer_if *buf, int32_t amount) {
341 if (buf->size < amount) return 0;
342 return buf->start+(buf->size-=amount);
345 void *buf_unprepend(struct buffer_if *buf, int32_t amount) {
347 if (buf->size < amount) return 0;
354 void buf_append_string(struct buffer_if *buf, cstring_t s)
359 /* fixme: if string is longer than 65535, result is a corrupted packet */
360 buf_append_uint16(buf,len);
361 BUF_ADD_BYTES(append,buf,s,len);
364 void truncmsg_add_string(struct buffer_if *buf, cstring_t s)
366 int32_t l = MIN((int32_t)strlen(s), buf_remaining_space(buf));
367 BUF_ADD_BYTES(append, buf, s, l);
369 void truncmsg_add_packet_string(struct buffer_if *buf, int32_t l,
375 if (c >= ' ' && c <= 126 && c != '\\' && c != '"' && c != '\'') {
376 if (!buf_remaining_space(buf)) break;
377 buf->start[buf->size++] = c;
384 case '\n': quoted[1] = 'n'; break;
385 case '\r': quoted[1] = 'r'; break;
386 case '\t': quoted[1] = 't'; break;
387 case '\\': case '"': case '\'': quoted[1] = c; break;
388 default: sprintf(quoted, "\\x%02x", (unsigned)c);
390 truncmsg_add_string(buf, quoted);
393 const char *truncmsg_terminate(const struct buffer_if *buf)
395 if (buf_remaining_space(buf)) {
396 buf->start[buf->size] = 0;
398 assert(buf->size >= 4);
399 strcpy(buf->start + buf->size - 4, "...");
404 void priomsg_new(struct priomsg *pm, int32_t maxlen)
406 buffer_new(&pm->m, maxlen);
409 void priomsg_reset(struct priomsg *pm)
411 buffer_init(&pm->m, 0);
414 bool_t priomsg_update_p(struct priomsg *pm, int prio)
416 if (prio <= pm->prio) return False;
417 buffer_init(&pm->m, 0);
421 const char *priomsg_getmessage(const struct priomsg *pm, const char *defmsg)
423 if (pm->prio >= INT_MIN)
424 return truncmsg_terminate(&pm->m);
430 void buffer_new(struct buffer_if *buf, int32_t len)
439 buf->base=safe_malloc(len,"buffer_new");
442 void buffer_readonly_view(struct buffer_if *buf, const void *data, int32_t len)
445 buf->owner="READONLY";
448 buf->size=buf->alloclen=len;
449 buf->base=buf->start=(uint8_t*)data;
452 void buffer_readonly_clone(struct buffer_if *out, const struct buffer_if *in)
454 buffer_readonly_view(out,in->start,in->size);
457 void buffer_copy(struct buffer_if *dst, const struct buffer_if *src)
459 if (dst->alloclen < src->alloclen) {
460 dst->base=realloc(dst->base,src->alloclen);
461 if (!dst->base) fatal_perror("buffer_copy");
462 dst->alloclen = src->alloclen;
464 dst->start = dst->base + (src->start - src->base);
465 dst->size = src->size;
466 memcpy(dst->start, src->start, dst->size);
469 static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
475 bool_t lockdown=False;
476 uint32_t len=DEFAULT_BUFFER_SIZE;
479 st->cl.description="buffer";
480 st->cl.type=CL_BUFFER;
482 st->cl.interface=&st->ops;
484 /* First argument, if present, is buffer length */
485 item=list_elem(args,0);
487 if (item->type!=t_number) {
488 cfgfatal(st->ops.loc,"buffer","first parameter must be a "
489 "number (buffer size)\n");
491 len=item->data.number;
492 if (len<MIN_BUFFER_SIZE) {
493 cfgfatal(st->ops.loc,"buffer","ludicrously small buffer size\n");
495 if (len>MAX_BUFFER_SIZE) {
496 cfgfatal(st->ops.loc,"buffer","ludicrously large buffer size\n");
499 /* Second argument, if present, is a dictionary */
500 item=list_elem(args,1);
502 if (item->type!=t_dict) {
503 cfgfatal(st->ops.loc,"buffer","second parameter must be a "
506 dict=item->data.dict;
507 lockdown=dict_read_bool(dict,"lockdown",False,"buffer",st->ops.loc,
511 buffer_new(&st->ops,len);
513 /* XXX mlock the buffer if possible */
516 return new_closure(&st->cl);
519 void send_nak(const struct comm_addr *dest, uint32_t our_index,
520 uint32_t their_index, uint32_t msgtype,
521 struct buffer_if *buf, const char *logwhy)
523 buffer_init(buf,calculate_max_start_pad());
524 buf_append_uint32(buf,their_index);
525 buf_append_uint32(buf,our_index);
526 buf_append_uint32(buf,LABEL_NAK);
528 Message(M_INFO,"%s: %08"PRIx32"<-%08"PRIx32": %08"PRIx32":"
529 " %s; sending NAK\n",
530 comm_addr_to_string(dest),
531 our_index, their_index, msgtype, logwhy);
532 dest->comm->sendmsg(dest->comm->st, buf, dest, 0);
535 int consttime_memeq(const void *s1in, const void *s2in, size_t n)
537 const uint8_t *s1=s1in, *s2=s2in;
538 register volatile uint8_t accumulator=0;
541 accumulator |= (*s1++ ^ *s2++);
543 accumulator |= accumulator >> 4; /* constant-time */
544 accumulator |= accumulator >> 2; /* boolean canonicalisation */
545 accumulator |= accumulator >> 1;
551 void hash_hash(const struct hash_if *hashi, const void *msg, int32_t len,
553 uint8_t hst[hashi->slen];
555 hashi->update(hst,msg,len);
556 hashi->final(hst,digest);
559 void util_module(dict_t *dict)
561 add_closure(dict,"sysbuffer",buffer_apply);
564 void update_max_start_pad(int32_t *our_module_global, int32_t our_instance)
566 if (*our_module_global < our_instance)
567 *our_module_global=our_instance;
570 int32_t transform_max_start_pad, comm_max_start_pad;
572 int32_t calculate_max_start_pad(void)
576 transform_max_start_pad +
580 void vslilog_part(struct log_if *lf, int priority, const char *message, va_list ap)
587 assert(bp < LOG_MESSAGE_BUFLEN);
588 vsnprintf(buff+bp,LOG_MESSAGE_BUFLEN-bp,message,ap);
589 buff[LOG_MESSAGE_BUFLEN-1] = '\n';
590 buff[LOG_MESSAGE_BUFLEN] = '\0';
591 /* Each line is sent separately */
592 while ((nlp=strchr(buff,'\n'))) {
594 slilog(lf,priority,"%s",buff);
595 memmove(buff,nlp+1,strlen(nlp+1)+1);
599 extern void slilog_part(struct log_if *lf, int priority, const char *message, ...)
602 va_start(ap,message);
603 vslilog_part(lf,priority,message,ap);
607 void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
612 ia->sin.sin_family=AF_INET;
613 ia->sin.sin_addr.s_addr=htonl(string_item_to_ipaddr(item,desc));
614 ia->sin.sin_port=htons(port);
616 #else /* CONFIG_IPV6 => we have adns_text2addr */
618 if (item->type!=t_string)
619 cfgfatal(item->loc,desc,"expecting a string IP (v4 or v6) address\n");
620 socklen_t salen=sizeof(*ia);
621 int r=adns_text2addr(item->data.string, port,
622 adns_qf_addrlit_ipv4_quadonly,
625 if (r) cfgfatal(item->loc,desc,"invalid IP (v4 or v6) address: %s\n",
628 #endif /* CONFIG_IPV6 */
631 #define IADDR_NBUFS 8
633 const char *iaddr_to_string(const union iaddr *ia)
637 SBUF_DEFINE(IADDR_NBUFS, 100);
639 assert(ia->sa.sa_family == AF_INET);
641 snprintf(SBUF, sizeof(SBUF), "[%s]:%d",
642 inet_ntoa(ia->sin.sin_addr),
643 ntohs(ia->sin.sin_port));
645 #else /* CONFIG_IPV6 => we have adns_addr2text */
647 SBUF_DEFINE(IADDR_NBUFS, 1+ADNS_ADDR2TEXT_BUFLEN+20);
651 char *addrbuf = SBUF;
653 int addrbuflen = ADNS_ADDR2TEXT_BUFLEN;
655 int r = adns_addr2text(&ia->sa, 0, addrbuf, &addrbuflen, &port);
657 const char fmt[]= "bad addr, error: %.*s";
658 sprintf(addrbuf, fmt,
659 (int)(ADNS_ADDR2TEXT_BUFLEN - sizeof(fmt)) /* underestimate */,
663 char *portbuf = addrbuf;
664 int addrl = strlen(addrbuf);
667 snprintf(portbuf, sizeof(SBUF)-addrl, "]:%d", port);
669 #endif /* CONFIG_IPV6 */
674 bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib,
677 if (ia->sa.sa_family != ib->sa.sa_family)
679 switch (ia->sa.sa_family) {
681 return ia->sin.sin_addr.s_addr == ib->sin.sin_addr.s_addr
683 ia->sin.sin_port == ib->sin.sin_port);
686 return !memcmp(&ia->sin6.sin6_addr, &ib->sin6.sin6_addr, 16)
687 && ia->sin6.sin6_scope_id == ib->sin6.sin6_scope_id
689 ia->sin6.sin6_port == ib->sin6.sin6_port)
690 /* we ignore the flowinfo field */;
691 #endif /* CONFIG_IPV6 */
697 int iaddr_socklen(const union iaddr *ia)
699 switch (ia->sa.sa_family) {
700 case AF_INET: return sizeof(ia->sin);
702 case AF_INET6: return sizeof(ia->sin6);
703 #endif /* CONFIG_IPV6 */
708 const char *pollbadbit(int revents)
711 if ((revents & b)) return #b
714 /* POLLNVAL is handled by the event loop - see afterpoll_fn comment */
719 enum async_linebuf_result
720 async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
721 const char **emsg_out)
723 int revents=pfd->revents;
725 #define BAD(m) do{ *emsg_out=(m); return async_linebuf_broken; }while(0)
727 const char *badbit=pollbadbit(revents);
728 if (badbit) BAD(badbit);
730 if (!(revents & POLLIN))
731 return async_linebuf_nothing;
734 * Data structure: A line which has been returned to the user is
735 * stored in buf at base before start. But we retain the usual
736 * buffer meaning of size. So:
738 * | returned : | input read, | unused |
739 * | to user : \0 | awaiting | buffer |
740 * | : | processing | space |
742 * ^base ^start ^start+size ^base+alloclen
745 BUF_ASSERT_USED(buf);
747 /* firstly, eat any previous */
748 if (buf->start != buf->base) {
749 memmove(buf->base,buf->start,buf->size);
750 buf->start=buf->base;
753 uint8_t *searched=buf->base;
756 * During the workings here we do not use start. We set start
757 * when we return some actual data. So we have this:
759 * | searched | read, might | unused |
760 * | for \n | contain \n | buffer |
761 * | none found | but not \0 | space |
763 * ^base ^searched ^base+size ^base+alloclen
768 uint8_t *dataend=buf->base+buf->size;
769 char *newline=memchr(searched,'\n',dataend-searched);
772 buf->start=newline+1;
773 buf->size=dataend-buf->start;
774 return async_linebuf_ok;
777 ssize_t space=(buf->base+buf->alloclen)-dataend;
778 if (!space) BAD("input line too long");
779 ssize_t r=read(pfd->fd,searched,space);
782 *emsg_out=buf->size?"no newline at eof":0;
783 buf->start=searched+1;
785 return async_linebuf_eof;
790 if (iswouldblock(errno))
791 return async_linebuf_nothing;
792 BAD(strerror(errno));
795 if (memchr(searched,0,r)) BAD("nul in input data");