2 * General utility functions for udp tunnel
5 * Copyright (C) 2000 Ian Jackson
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with userv-utils; if not, write to the Free Software
19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "forwarder.h"
30 const char *const *argv;
31 char programid[SYS_NMLN+sizeof(PROGRAM)+3];
33 void arg_assert_fail(const char *msg) {
34 fprintf(stderr, PROGRAM ": argument error (!`%s')\n",msg);
38 void sysfail(const char *msg) {
39 fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno));
43 void fail(const char *msg) {
44 fprintf(stderr, "%s: fatal error: %s\n", programid, msg);
48 void sysdiag(const char *msg) {
49 fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno));
52 void diag(const char *msg) {
53 fprintf(stderr, "%s: %s\n", programid, msg);
58 if (time(&r) == (time_t)-1) sysfail("get time of day");
62 void *xmalloc(size_t sz) {
65 if (!r) sysfail("allocate memory");
69 void write_must(int fd, const void *p_in, int sz, const char *what) {
70 const unsigned char *p= p_in;
76 if (errno == EINTR) continue;
85 void read_must(int fd, void *p_in, int sz, const char *what) {
86 unsigned char *p= p_in;
92 if (errno == EINTR) continue;
102 const char *getarg_string(void) {
110 unsigned long getarg_ulong(void) {
114 ul= strtoul(getarg_string(),&ep,0);
119 void *buf_append(struct buffer *buf, size_t amount) {
122 p= buf->start + buf->size;
127 void *buf_prepend(struct buffer *buf, size_t amount) {
129 return buf->start -= amount;
132 void *buf_unappend(struct buffer *buf, size_t amount) {
133 if (buf->size < amount) return 0;
134 return buf->start + (buf->size -= amount);
137 void *buf_unprepend(struct buffer *buf, size_t amount) {
141 buf->start += amount;