f9e59051 |
1 | /* |
f0e54a99 |
2 | * General utility functions for udp tunnel |
3 | */ |
4 | /* |
711a0748 |
5 | * Copyright (C) 2000,2003 Ian Jackson |
f0e54a99 |
6 | * |
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. |
11 | * |
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. |
16 | * |
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. |
f9e59051 |
20 | */ |
21 | |
22 | #include <string.h> |
23 | #include <errno.h> |
24 | #include <unistd.h> |
25 | #include <stdlib.h> |
26 | #include <assert.h> |
27 | |
28 | #include "forwarder.h" |
29 | |
30 | const char *const *argv; |
31 | char programid[SYS_NMLN+sizeof(PROGRAM)+3]; |
32 | |
33 | void arg_assert_fail(const char *msg) { |
34 | fprintf(stderr, PROGRAM ": argument error (!`%s')\n",msg); |
35 | exit(12); |
36 | } |
37 | |
38 | void sysfail(const char *msg) { |
39 | fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno)); |
40 | exit(8); |
41 | } |
42 | |
43 | void fail(const char *msg) { |
44 | fprintf(stderr, "%s: fatal error: %s\n", programid, msg); |
45 | exit(4); |
46 | } |
47 | |
48 | void sysdiag(const char *msg) { |
49 | fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno)); |
50 | } |
51 | |
52 | void diag(const char *msg) { |
53 | fprintf(stderr, "%s: %s\n", programid, msg); |
54 | } |
55 | |
56 | time_t now(void) { |
57 | time_t r; |
58 | if (time(&r) == (time_t)-1) sysfail("get time of day"); |
59 | return r; |
60 | } |
61 | |
62 | void *xmalloc(size_t sz) { |
63 | void *r; |
64 | r= malloc(sz); |
65 | if (!r) sysfail("allocate memory"); |
66 | return r; |
67 | } |
68 | |
69 | void write_must(int fd, const void *p_in, int sz, const char *what) { |
70 | const unsigned char *p= p_in; |
71 | int r; |
72 | |
73 | while (sz) { |
74 | r= write(fd,p,sz); |
75 | if (r<0) { |
76 | if (errno == EINTR) continue; |
77 | else sysfail(what); |
78 | } |
79 | assert(r && r <= sz); |
80 | p += r; |
81 | sz -= r; |
82 | } |
83 | } |
84 | |
85 | void read_must(int fd, void *p_in, int sz, const char *what) { |
86 | unsigned char *p= p_in; |
87 | int r; |
88 | |
89 | while (sz) { |
90 | r= read(fd,p,sz); |
91 | if (r<0) { |
92 | if (errno == EINTR) continue; |
93 | else sysfail(what); |
94 | } |
95 | if (r==0) fail(what); |
96 | assert(r <= sz); |
97 | p += r; |
98 | sz -= r; |
99 | } |
100 | } |
101 | |
f9e59051 |
102 | const char *getarg_string(void) { |
103 | const char *arg; |
104 | |
105 | arg= *++argv; |
106 | arg_assert(arg); |
107 | return arg; |
108 | } |
109 | |
110 | unsigned long getarg_ulong(void) { |
111 | char *ep; |
112 | unsigned long ul; |
113 | |
114 | ul= strtoul(getarg_string(),&ep,0); |
115 | arg_assert(!*ep); |
116 | return ul; |
117 | } |
118 | |
119 | void *buf_append(struct buffer *buf, size_t amount) { |
120 | void *p; |
121 | |
122 | p= buf->start + buf->size; |
123 | buf->size += amount; |
124 | return p; |
125 | } |
126 | |
127 | void *buf_prepend(struct buffer *buf, size_t amount) { |
128 | buf->size += amount; |
129 | return buf->start -= amount; |
130 | } |
131 | |
132 | void *buf_unappend(struct buffer *buf, size_t amount) { |
133 | if (buf->size < amount) return 0; |
134 | return buf->start + (buf->size -= amount); |
135 | } |
136 | |
137 | void *buf_unprepend(struct buffer *buf, size_t amount) { |
138 | void *p; |
139 | |
140 | p= buf->start; |
141 | buf->start += amount; |
142 | buf->size -= amount; |
143 | return p; |
144 | } |