2 * Revision 1.2 1996/04/14 16:34:36 sde1000
4 * mpbin/mpstring functions moved from dh.c
6 * Revision 1.1 1996/03/14 17:05:03 sde1000
20 #include <sys/types.h>
24 #include "unaligned.h"
26 #define MIN_BUFFER_SIZE 64
27 #define DEFAULT_BUFFER_SIZE 4096
28 #define MAX_BUFFER_SIZE 131072
30 static char *hexdigits="0123456789abcdef";
32 uint32_t message_level=M_WARNING|M_ERROR|M_FATAL;
33 uint32_t syslog_level=M_WARNING|M_ERROR|M_FATAL;
34 static uint32_t current_phase=0;
39 struct phase_hook *next;
42 static struct phase_hook *hooks[NR_PHASES]={NULL,};
44 static void vMessage(uint32_t class, char *message, va_list args)
47 if (class & message_level) {
48 if (class&M_FATAL || class&M_ERROR || class&M_WARNING) {
51 vfprintf(dest,message,args);
53 /* XXX do something about syslog output here */
55 /* Maybe send message to syslog */
56 vsprintf(buff, message, args);
57 /* XXX Send each line as a separate log entry */
58 log(syslog_prio[level], buff);
62 void Message(uint32_t class, char *message, ...)
68 vMessage(class,message,ap);
73 static void vfatal(int status, bool_t perror, char *message, va_list args)
79 enter_phase(PHASE_SHUTDOWN);
81 Message(M_FATAL, "secnet fatal error: ");
82 vMessage(M_FATAL, message, args);
83 Message(M_FATAL, ": %s\n",strerror(err));
86 Message(M_FATAL, "secnet fatal error: ");
87 vMessage(M_FATAL,message,args);
92 void fatal(char *message, ...)
95 va_start(args,message);
96 vfatal(current_phase,False,message,args);
100 void fatal_status(int status, char *message, ...)
103 va_start(args,message);
104 vfatal(status,False,message,args);
108 void fatal_perror(char *message, ...)
111 va_start(args,message);
112 vfatal(current_phase,True,message,args);
116 void fatal_perror_status(int status, char *message, ...)
119 va_start(args,message);
120 vfatal(status,True,message,args);
124 void cfgfatal(struct cloc loc, string_t facility, char *message, ...)
128 va_start(args,message);
130 enter_phase(PHASE_SHUTDOWN);
132 if (loc.file && loc.line) {
133 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
135 } else if (!loc.file && loc.line) {
136 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
138 Message(M_FATAL, "config error (%s): ",facility);
141 vMessage(M_FATAL,message,args);
146 char *safe_strdup(char *s, char *message)
151 fatal_perror(message);
156 void *safe_malloc(size_t size, char *message)
161 fatal_perror(message);
166 /* Convert a buffer into its MP_INT representation */
167 void read_mpbin(MP_INT *a, uint8_t *bin, int binsize)
172 buff=safe_malloc(binsize*2 + 1,"read_mpbin");
174 for (i=0; i<binsize; i++) {
175 buff[i*2]=hexdigits[(bin[i] & 0xf0) >> 4];
176 buff[i*2+1]=hexdigits[(bin[i] & 0xf)];
180 mpz_set_str(a, buff, 16);
184 /* Convert a MP_INT into a hex string */
185 char *write_mpstring(MP_INT *a)
189 buff=safe_malloc(mpz_sizeinbase(a,16)+2,"write_mpstring");
190 mpz_get_str(buff, 16, a);
194 static uint8_t hexval(uint8_t c)
223 /* Convert a MP_INT into a buffer; return length; truncate if necessary */
224 uint32_t write_mpbin(MP_INT *a, uint8_t *buffer, uint32_t buflen)
229 if (buflen==0) return 0;
230 hb=write_mpstring(a);
235 /* The number starts with a half-byte */
236 buffer[i++]=hexval(hb[j++]);
238 for (; hb[j] && i<buflen; i++) {
239 buffer[i]=(hexval(hb[j])<<4)|hexval(hb[j+1]);
246 bool_t subnet_match(struct subnet_list *list, uint32_t address)
249 for (i=0; i<list->entries; i++) {
250 if (list->list[i].prefix == (address&list->list[i].mask)) return True;
255 /* The string buffer must be at least 16 bytes long */
256 string_t ipaddr_to_string(uint32_t addr)
261 s=safe_malloc(16,"ipaddr_to_string");
266 snprintf(s, 16, "%d.%d.%d.%d", a, b, c, d);
270 string_t subnet_to_string(struct subnet *sn)
272 uint32_t mask=sn->mask, addr=sn->prefix;
277 s=safe_malloc(19,"subnet_to_string");
282 for (i=0; mask; i++) {
285 snprintf(s, 19, "%d.%d.%d.%d/%d", a, b, c, d, i);
289 int sys_cmd(const char *path, char *arg, ...)
298 /* Parent -> wait for child */
303 /* Child -> exec command */
306 while ((args[i++]=va_arg(ap,char *)));
311 fatal_perror("sys_cmd(%s,%s,...)");
318 /* Take a list of log closures and merge them */
321 struct loglist *next;
324 static void log_vmulti(void *state, int priority, char *message, va_list args)
326 struct loglist *st=state, *i;
328 for (i=st; i; i=i->next) {
329 i->l->vlog(i->l->st,priority,message,args);
333 static void log_multi(void *st, int priority, char *message, ...)
337 va_start(ap,message);
339 log_vmulti(st,priority,message,ap);
344 struct log_if *init_log(list_t *ll)
349 struct loglist *l=NULL, *n;
352 while ((item=list_elem(ll,i++))) {
353 if (item->type!=t_closure) {
354 cfgfatal(item->loc,"init_log","item is not a closure");
356 cl=item->data.closure;
357 if (cl->type!=CL_LOG) {
358 cfgfatal(item->loc,"init_log","closure is not a logger");
360 n=safe_malloc(sizeof(*n),"init_log");
366 fatal("init_log: none of the items in the list are loggers");
368 r=safe_malloc(sizeof(*r), "init_log");
381 static void logfile_vlog(void *state, int priority, char *message,
384 struct logfile *st=state;
386 vfprintf(st->f,message,args);
390 static void logfile_log(void *state, int priority, char *message, ...)
394 va_start(ap,message);
395 logfile_vlog(state,priority,message,ap);
399 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
404 st=safe_malloc(sizeof(*st),"logfile_apply");
405 st->cl.description="logfile";
408 st->cl.interface=&st->ops;
410 st->ops.log=logfile_log;
411 st->ops.vlog=logfile_vlog;
412 st->f=stderr; /* XXX ignore args */
414 return new_closure(&st->cl);
417 static char *phases[NR_PHASES]={
427 void enter_phase(uint32_t new_phase)
429 struct phase_hook *i;
431 Message(M_DEBUG_PHASE,"entering %s... ", phases[new_phase]);
432 current_phase=new_phase;
434 for (i=hooks[new_phase]; i; i=i->next)
435 i->fn(i->state, new_phase);
436 Message(M_DEBUG_PHASE,"now in %s\n",phases[new_phase]);
439 bool_t add_hook(uint32_t phase, hook_fn *fn, void *state)
441 struct phase_hook *h;
443 h=safe_malloc(sizeof(*h),"add_hook");
446 h->next=hooks[phase];
451 bool_t remove_hook(uint32_t phase, hook_fn *fn, void *state)
453 fatal("remove_hook: not implemented\n");
458 void log(struct log_if *lf, int priority, char *message, ...)
462 va_start(ap,message);
463 lf->vlog(lf->st,priority,message,ap);
469 struct buffer_if ops;
472 void buffer_assert_free(struct buffer_if *buffer, string_t file, uint32_t line)
475 fatal("BUF_ASSERT_FREE, %s line %d, owned by %s\n",
476 file,line,buffer->owner);
480 void buffer_assert_used(struct buffer_if *buffer, string_t file, uint32_t line)
483 fatal("BUF_ASSERT_USED, %s line %d, last owned by %s\n",
484 file,line,buffer->owner);
488 void buffer_init(struct buffer_if *buffer, uint32_t max_start_pad)
490 buffer->start=buffer->base+max_start_pad;
494 void *buf_append(struct buffer_if *buf, uint32_t amount) {
496 p=buf->start + buf->size;
501 void *buf_prepend(struct buffer_if *buf, uint32_t amount) {
503 return buf->start-=amount;
506 void *buf_unappend(struct buffer_if *buf, uint32_t amount) {
507 if (buf->size < amount) return 0;
508 return buf->start+(buf->size-=amount);
511 void *buf_unprepend(struct buffer_if *buf, uint32_t amount) {
519 /* Append a two-byte length and the string to the buffer. Length is in
520 network byte order. */
521 void buf_append_string(struct buffer_if *buf, string_t s)
526 buf_append_uint16(buf,len);
527 memcpy(buf_append(buf,len),s,len);
530 void buffer_new(struct buffer_if *buf, uint32_t len)
540 buf->base=safe_malloc(len,"buffer_new");
543 static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
549 bool_t lockdown=False;
550 uint32_t len=DEFAULT_BUFFER_SIZE;
552 st=safe_malloc(sizeof(*st),"buffer_apply");
553 st->cl.description="buffer";
554 st->cl.type=CL_BUFFER;
556 st->cl.interface=&st->ops;
558 /* First argument, if present, is buffer length */
559 item=list_elem(args,0);
561 if (item->type!=t_number) {
562 cfgfatal(st->ops.loc,"buffer","first parameter must be a "
563 "number (buffer size)\n");
565 len=item->data.number;
566 if (len<MIN_BUFFER_SIZE) {
567 cfgfatal(st->ops.loc,"buffer","ludicrously small buffer size\n");
569 if (len>MAX_BUFFER_SIZE) {
570 cfgfatal(st->ops.loc,"buffer","ludicrously large buffer size\n");
573 /* Second argument, if present, is a dictionary */
574 item=list_elem(args,1);
576 if (item->type!=t_dict) {
577 cfgfatal(st->ops.loc,"buffer","second parameter must be a "
580 dict=item->data.dict;
581 lockdown=dict_read_bool(dict,"lockdown",False,"buffer",st->ops.loc,
585 buffer_new(&st->ops,len);
587 Message(M_WARNING,"buffer: XXX lockdown\n");
590 return new_closure(&st->cl);
593 init_module util_module;
594 void util_module(dict_t *dict)
596 add_closure(dict,"logfile",logfile_apply);
597 add_closure(dict,"sysbuffer",buffer_apply);