2 * General utility functions for udp tunnel
5 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
8 * Chancellor Masters and Scholars of the University of Cambridge
9 * Copyright 2010 Tony Finch <fanf@dotat.at>
11 * This is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
31 #include "forwarder.h"
33 const char *const *argv
;
34 char programid
[SYS_NMLN
+sizeof(PROGRAM
)+3];
36 void arg_assert_fail(const char *msg
) {
37 fprintf(stderr
, PROGRAM
": argument error (!`%s')\n",msg
);
41 void sysfail(const char *msg
) {
42 fprintf(stderr
, "%s: fatal system error: %s: %s\n", programid
, msg
, strerror(errno
));
46 void fail(const char *msg
) {
47 fprintf(stderr
, "%s: fatal error: %s\n", programid
, msg
);
51 void sysdiag(const char *msg
) {
52 fprintf(stderr
, "%s: system/network error: %s: %s\n", programid
, msg
, strerror(errno
));
55 void diag(const char *msg
) {
56 fprintf(stderr
, "%s: %s\n", programid
, msg
);
61 if (time(&r
) == (time_t)-1) sysfail("get time of day");
65 void *xmalloc(size_t sz
) {
68 if (!r
) sysfail("allocate memory");
72 void write_must(int fd
, const void *p_in
, int sz
, const char *what
) {
73 const unsigned char *p
= p_in
;
79 if (errno
== EINTR
) continue;
88 void read_must(int fd
, void *p_in
, int sz
, const char *what
) {
89 unsigned char *p
= p_in
;
95 if (errno
== EINTR
) continue;
105 const char *getarg_string(void) {
113 unsigned long getarg_ulong(void) {
117 ul
= strtoul(getarg_string(),&ep
,0);
122 void *buf_append(struct buffer
*buf
, size_t amount
) {
125 p
= buf
->start
+ buf
->size
;
130 void *buf_prepend(struct buffer
*buf
, size_t amount
) {
132 return buf
->start
-= amount
;
135 void *buf_unappend(struct buffer
*buf
, size_t amount
) {
136 if (buf
->size
< amount
) return 0;
137 return buf
->start
+ (buf
->size
-= amount
);
140 void *buf_unprepend(struct buffer
*buf
, size_t amount
) {
144 buf
->start
+= amount
;