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
23 #define MIN_BUFFER_SIZE 64
24 #define DEFAULT_BUFFER_SIZE 4096
25 #define MAX_BUFFER_SIZE 131072
27 static char *hexdigits
="0123456789abcdef";
29 uint32_t message_level
=M_WARNING
|M_ERROR
|M_FATAL
;
30 uint32_t syslog_level
=M_WARNING
|M_ERROR
|M_FATAL
;
31 static uint32_t current_phase
=0;
36 struct phase_hook
*next
;
39 static struct phase_hook
*hooks
[NR_PHASES
]={NULL
,};
41 static void vMessage(uint32_t class, char *message
, va_list args
)
44 if (class & message_level
) {
45 if (class&M_FATAL
|| class&M_ERROR
|| class&M_WARNING
) {
48 vfprintf(dest
,message
,args
);
50 /* XXX do something about syslog output here */
52 /* Maybe send message to syslog */
53 vsprintf(buff
, message
, args
);
54 /* XXX Send each line as a separate log entry */
55 log(syslog_prio
[level
], buff
);
59 void Message(uint32_t class, char *message
, ...)
65 vMessage(class,message
,ap
);
70 static void vfatal(int status
, bool_t perror
, char *message
, va_list args
)
76 enter_phase(PHASE_SHUTDOWN
);
78 Message(M_FATAL
, "secnet fatal error: ");
79 vMessage(M_FATAL
, message
, args
);
80 Message(M_FATAL
, ": %s\n",strerror(err
));
83 Message(M_FATAL
, "secnet fatal error: ");
84 vMessage(M_FATAL
,message
,args
);
89 void fatal(char *message
, ...)
92 va_start(args
,message
);
93 vfatal(current_phase
,False
,message
,args
);
97 void fatal_status(int status
, char *message
, ...)
100 va_start(args
,message
);
101 vfatal(status
,False
,message
,args
);
105 void fatal_perror(char *message
, ...)
108 va_start(args
,message
);
109 vfatal(current_phase
,True
,message
,args
);
113 void fatal_perror_status(int status
, char *message
, ...)
116 va_start(args
,message
);
117 vfatal(status
,True
,message
,args
);
121 void cfgfatal(struct cloc loc
, string_t facility
, char *message
, ...)
125 va_start(args
,message
);
127 enter_phase(PHASE_SHUTDOWN
);
129 if (loc
.file
&& loc
.line
) {
130 Message(M_FATAL
, "config error (%s, %s:%d): ",facility
,loc
.file
,
132 } else if (!loc
.file
&& loc
.line
) {
133 Message(M_FATAL
, "config error (%s, line %d): ",facility
,loc
.line
);
135 Message(M_FATAL
, "config error (%s): ",facility
);
138 vMessage(M_FATAL
,message
,args
);
143 char *safe_strdup(char *s
, char *message
)
148 fatal_perror(message
);
153 void *safe_malloc(size_t size
, char *message
)
158 fatal_perror(message
);
163 /* Convert a buffer into its MP_INT representation */
164 void read_mpbin(MP_INT
*a
, uint8_t *bin
, int binsize
)
169 buff
=safe_malloc(binsize
*2 + 1,"read_mpbin");
171 for (i
=0; i
<binsize
; i
++) {
172 buff
[i
*2]=hexdigits
[(bin
[i
] & 0xf0) >> 4];
173 buff
[i
*2+1]=hexdigits
[(bin
[i
] & 0xf)];
177 mpz_set_str(a
, buff
, 16);
181 /* Convert a MP_INT into a hex string */
182 char *write_mpstring(MP_INT
*a
)
186 buff
=safe_malloc(mpz_sizeinbase(a
,16)+2,"write_mpstring");
187 mpz_get_str(buff
, 16, a
);
191 static uint8_t hexval(uint8_t c
)
220 /* Convert a MP_INT into a buffer; return length; truncate if necessary */
221 uint32_t write_mpbin(MP_INT
*a
, uint8_t *buffer
, uint32_t buflen
)
226 if (buflen
==0) return 0;
227 hb
=write_mpstring(a
);
232 /* The number starts with a half-byte */
233 buffer
[i
++]=hexval(hb
[j
++]);
235 for (; hb
[j
] && i
<buflen
; i
++) {
236 buffer
[i
]=(hexval(hb
[j
])<<4)|hexval(hb
[j
+1]);
243 bool_t
subnet_match(struct subnet_list
*list
, uint32_t address
)
246 for (i
=0; i
<list
->entries
; i
++) {
247 if (list
->list
[i
].prefix
== (address
&list
->list
[i
].mask
)) return True
;
252 /* The string buffer must be at least 16 bytes long */
253 string_t
ipaddr_to_string(uint32_t addr
)
258 s
=safe_malloc(16,"ipaddr_to_string");
263 snprintf(s
, 16, "%d.%d.%d.%d", a
, b
, c
, d
);
267 string_t
subnet_to_string(struct subnet
*sn
)
269 uint32_t mask
=sn
->mask
, addr
=sn
->prefix
;
274 s
=safe_malloc(19,"subnet_to_string");
279 for (i
=0; mask
; i
++) {
282 snprintf(s
, 19, "%d.%d.%d.%d/%d", a
, b
, c
, d
, i
);
286 /* Take a list of log closures and merge them */
289 struct loglist
*next
;
292 static void log_vmulti(void *state
, int priority
, char *message
, va_list args
)
294 struct loglist
*st
=state
, *i
;
296 for (i
=st
; i
; i
=i
->next
) {
297 i
->l
->vlog(i
->l
->st
,priority
,message
,args
);
301 static void log_multi(void *st
, int priority
, char *message
, ...)
305 va_start(ap
,message
);
307 log_vmulti(st
,priority
,message
,ap
);
312 struct log_if
*init_log(list_t
*ll
)
317 struct loglist
*l
=NULL
, *n
;
320 while ((item
=list_elem(ll
,i
++))) {
321 if (item
->type
!=t_closure
) {
322 cfgfatal(item
->loc
,"init_log","item is not a closure");
324 cl
=item
->data
.closure
;
325 if (cl
->type
!=CL_LOG
) {
326 cfgfatal(item
->loc
,"init_log","closure is not a logger");
328 n
=safe_malloc(sizeof(*n
),"init_log");
334 fatal("init_log: none of the items in the list are loggers");
336 r
=safe_malloc(sizeof(*r
), "init_log");
349 static void logfile_vlog(void *state
, int priority
, char *message
,
352 struct logfile
*st
=state
;
354 vfprintf(st
->f
,message
,args
);
358 static void logfile_log(void *state
, int priority
, char *message
, ...)
362 va_start(ap
,message
);
363 logfile_vlog(state
,priority
,message
,ap
);
367 static list_t
*logfile_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
372 st
=safe_malloc(sizeof(*st
),"logfile_apply");
373 st
->cl
.description
="logfile";
376 st
->cl
.interface
=&st
->ops
;
378 st
->ops
.log
=logfile_log
;
379 st
->ops
.vlog
=logfile_vlog
;
380 st
->f
=stderr
; /* XXX ignore args */
382 return new_closure(&st
->cl
);
385 static char *phases
[NR_PHASES
]={
395 void enter_phase(uint32_t new_phase
)
397 struct phase_hook
*i
;
399 Message(M_DEBUG_PHASE
,"entering %s... ", phases
[new_phase
]);
400 current_phase
=new_phase
;
402 for (i
=hooks
[new_phase
]; i
; i
=i
->next
)
403 i
->fn(i
->state
, new_phase
);
404 Message(M_DEBUG_PHASE
,"now in %s\n",phases
[new_phase
]);
407 bool_t
add_hook(uint32_t phase
, hook_fn
*fn
, void *state
)
409 struct phase_hook
*h
;
411 h
=safe_malloc(sizeof(*h
),"add_hook");
414 h
->next
=hooks
[phase
];
419 bool_t
remove_hook(uint32_t phase
, hook_fn
*fn
, void *state
)
421 fatal("remove_hook: not implemented\n");
426 void log(struct log_if
*lf
, int priority
, char *message
, ...)
430 va_start(ap
,message
);
431 lf
->vlog(lf
->st
,priority
,message
,ap
);
437 struct buffer_if ops
;
440 void buffer_assert_free(struct buffer_if
*buffer
, string_t file
, uint32_t line
)
443 fatal("BUF_ASSERT_FREE, %s line %d, owned by %s\n",
444 file
,line
,buffer
->owner
);
448 void buffer_assert_used(struct buffer_if
*buffer
, string_t file
, uint32_t line
)
451 fatal("BUF_ASSERT_USED, %s line %d, last owned by %s\n",
452 file
,line
,buffer
->owner
);
456 void buffer_init(struct buffer_if
*buffer
, uint32_t max_start_pad
)
458 buffer
->start
=buffer
->base
+max_start_pad
;
462 void *buf_append(struct buffer_if
*buf
, uint32_t amount
) {
464 p
=buf
->start
+ buf
->size
;
469 void *buf_prepend(struct buffer_if
*buf
, uint32_t amount
) {
471 return buf
->start
-=amount
;
474 void *buf_unappend(struct buffer_if
*buf
, uint32_t amount
) {
475 if (buf
->size
< amount
) return 0;
476 return buf
->start
+(buf
->size
-=amount
);
479 void *buf_unprepend(struct buffer_if
*buf
, uint32_t amount
) {
487 /* Append a two-byte length and the string to the buffer. Length is in
488 network byte order. */
489 void buf_append_string(struct buffer_if
*buf
, string_t s
)
494 *(uint16_t *)buf_append(buf
,2)=htons(len
);
495 memcpy(buf_append(buf
,len
),s
,len
);
498 void buffer_new(struct buffer_if
*buf
, uint32_t len
)
508 buf
->base
=safe_malloc(len
,"buffer_new");
511 static list_t
*buffer_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
517 bool_t lockdown
=False
;
519 st
=safe_malloc(sizeof(*st
),"buffer_apply");
520 st
->cl
.description
="buffer";
521 st
->cl
.type
=CL_BUFFER
;
523 st
->cl
.interface
=&st
->ops
;
529 st
->ops
.len
=DEFAULT_BUFFER_SIZE
;
532 /* First argument, if present, is buffer length */
533 item
=list_elem(args
,0);
535 if (item
->type
!=t_number
) {
536 cfgfatal(st
->ops
.loc
,"buffer","first parameter must be a "
537 "number (buffer size)\n");
539 st
->ops
.len
=item
->data
.number
;
540 if (st
->ops
.len
<MIN_BUFFER_SIZE
) {
541 cfgfatal(st
->ops
.loc
,"buffer","ludicrously small buffer size\n");
543 if (st
->ops
.len
>MAX_BUFFER_SIZE
) {
544 cfgfatal(st
->ops
.loc
,"buffer","ludicrously large buffer size\n");
547 /* Second argument, if present, is a dictionary */
548 item
=list_elem(args
,1);
550 if (item
->type
!=t_dict
) {
551 cfgfatal(st
->ops
.loc
,"buffer","second parameter must be a "
554 dict
=item
->data
.dict
;
555 lockdown
=dict_read_bool(dict
,"lockdown",False
,"buffer",st
->ops
.loc
,
559 st
->ops
.base
=safe_malloc(st
->ops
.len
,"buffer");
561 Message(M_WARNING
,"buffer: XXX lockdown\n");
564 return new_closure(&st
->cl
);
567 init_module util_module
;
568 void util_module(dict_t
*dict
)
570 add_closure(dict
,"logfile",logfile_apply
);
571 add_closure(dict
,"sysbuffer",buffer_apply
);