1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* General utility functions for udp tunnel
*/
/*
* Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
* Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
* Copyright 1999,2003
* Chancellor Masters and Scholars of the University of Cambridge
* Copyright 2010 Tony Finch <fanf@dotat.at>
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with userv-utils; if not, see http://www.gnu.org/licenses/.
*/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include "forwarder.h"
const char *const *argv;
char programid[SYS_NMLN+sizeof(PROGRAM)+3];
void arg_assert_fail(const char *msg) {
fprintf(stderr, PROGRAM ": argument error (!`%s')\n",msg);
exit(12);
}
void sysfail(const char *msg) {
fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno));
exit(8);
}
void fail(const char *msg) {
fprintf(stderr, "%s: fatal error: %s\n", programid, msg);
exit(4);
}
void sysdiag(const char *msg) {
fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno));
}
void diag(const char *msg) {
fprintf(stderr, "%s: %s\n", programid, msg);
}
time_t now(void) {
time_t r;
if (time(&r) == (time_t)-1) sysfail("get time of day");
return r;
}
void *xmalloc(size_t sz) {
void *r;
r= malloc(sz);
if (!r) sysfail("allocate memory");
return r;
}
void write_must(int fd, const void *p_in, int sz, const char *what) {
const unsigned char *p= p_in;
int r;
while (sz) {
r= write(fd,p,sz);
if (r<0) {
if (errno == EINTR) continue;
else sysfail(what);
}
assert(r && r <= sz);
p += r;
sz -= r;
}
}
void read_must(int fd, void *p_in, int sz, const char *what) {
unsigned char *p= p_in;
int r;
while (sz) {
r= read(fd,p,sz);
if (r<0) {
if (errno == EINTR) continue;
else sysfail(what);
}
if (r==0) fail(what);
assert(r <= sz);
p += r;
sz -= r;
}
}
const char *getarg_string(void) {
const char *arg;
arg= *++argv;
arg_assert(arg);
return arg;
}
unsigned long getarg_ulong(void) {
char *ep;
unsigned long ul;
ul= strtoul(getarg_string(),&ep,0);
arg_assert(!*ep);
return ul;
}
void *buf_append(struct buffer *buf, size_t amount) {
void *p;
p= buf->start + buf->size;
buf->size += amount;
return p;
}
void *buf_prepend(struct buffer *buf, size_t amount) {
buf->size += amount;
return buf->start -= amount;
}
void *buf_unappend(struct buffer *buf, size_t amount) {
if (buf->size < amount) return 0;
return buf->start + (buf->size -= amount);
}
void *buf_unprepend(struct buffer *buf, size_t amount) {
void *p;
p= buf->start;
buf->start += amount;
buf->size -= amount;
return p;
}
|