For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / lib / util.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "util.h"
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 const char *program_name;
43
44 void
45 out_of_memory(void) 
46 {
47     ofp_fatal(0, "virtual memory exhausted");
48 }
49
50 void *
51 xcalloc(size_t count, size_t size) 
52 {
53     void *p = count && size ? calloc(count, size) : malloc(1);
54     if (p == NULL) {
55         out_of_memory();
56     }
57     return p;
58 }
59
60 void *
61 xmalloc(size_t size) 
62 {
63     void *p = malloc(size ? size : 1);
64     if (p == NULL) {
65         out_of_memory();
66     }
67     return p;
68 }
69
70 void *
71 xrealloc(void *p, size_t size) 
72 {
73     p = realloc(p, size ? size : 1);
74     if (p == NULL) {
75         out_of_memory();
76     }
77     return p;
78 }
79
80 void *
81 xmemdup(const void *p_, size_t size)
82 {
83     void *p = xmalloc(size);
84     memcpy(p, p_, size);
85     return p;
86 }
87
88 char *
89 xmemdup0(const char *p_, size_t length)
90 {
91     char *p = xmalloc(length + 1);
92     memcpy(p, p_, length);
93     p[length] = '\0';
94     return p;
95 }
96
97 char *
98 xstrdup(const char *s) 
99 {
100     return xmemdup0(s, strlen(s));
101 }
102
103 char *
104 xvasprintf(const char *format, va_list args)
105 {
106     va_list args2;
107     size_t needed;
108     char *s;
109
110     va_copy(args2, args);
111     needed = vsnprintf(NULL, 0, format, args);
112
113     s = xmalloc(needed + 1);
114
115     vsnprintf(s, needed + 1, format, args2);
116     va_end(args2);
117
118     return s;
119 }
120
121 void *
122 x2nrealloc(void *p, size_t *n, size_t s)
123 {
124     *n = *n == 0 ? 1 : 2 * *n;
125     return xrealloc(p, *n * s);
126 }
127
128 char *
129 xasprintf(const char *format, ...)
130 {
131     va_list args;
132     char *s;
133
134     va_start(args, format);
135     s = xvasprintf(format, args);
136     va_end(args);
137
138     return s;
139 }
140
141 void
142 strlcpy(char *dst, const char *src, size_t size)
143 {
144     if (size > 0) {
145         size_t n = strlen(src);
146         size_t n_copy = MIN(n, size - 1);
147         memcpy(dst, src, n_copy);
148         dst[n_copy] = '\0';
149     }
150 }
151
152 void
153 ofp_fatal(int err_no, const char *format, ...)
154 {
155     va_list args;
156
157     fprintf(stderr, "%s: ", program_name);
158     va_start(args, format);
159     vfprintf(stderr, format, args);
160     va_end(args);
161     if (err_no != 0)
162         fprintf(stderr, " (%s)", strerror(err_no));
163     putc('\n', stderr);
164
165     exit(EXIT_FAILURE);
166 }
167
168 void
169 ofp_error(int err_no, const char *format, ...)
170 {
171     int save_errno = errno;
172     va_list args;
173
174     fprintf(stderr, "%s: ", program_name);
175     va_start(args, format);
176     vfprintf(stderr, format, args);
177     va_end(args);
178     if (err_no != 0)
179         fprintf(stderr, " (%s)", strerror(err_no));
180     putc('\n', stderr);
181
182     errno = save_errno;
183 }
184
185 /* Sets program_name based on 'argv0'.  Should be called at the beginning of
186  * main(), as "set_program_name(argv[0]);".  */
187 void set_program_name(const char *argv0)
188 {
189     const char *slash = strrchr(argv0, '/');
190     program_name = slash ? slash + 1 : argv0;
191 }
192
193 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
194  * line.  Numeric offsets are also included, starting at 'ofs' for the first
195  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
196  * are also rendered alongside. */
197 void
198 ofp_hex_dump(FILE *stream, const void *buf_, size_t size,
199              uintptr_t ofs, bool ascii)
200 {
201   const uint8_t *buf = buf_;
202   const size_t per_line = 16; /* Maximum bytes per line. */
203
204   while (size > 0)
205     {
206       size_t start, end, n;
207       size_t i;
208
209       /* Number of bytes on this line. */
210       start = ofs % per_line;
211       end = per_line;
212       if (end - start > size)
213         end = start + size;
214       n = end - start;
215
216       /* Print line. */
217       fprintf(stream, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
218       for (i = 0; i < start; i++)
219         fprintf(stream, "   ");
220       for (; i < end; i++)
221         fprintf(stream, "%02hhx%c",
222                 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
223       if (ascii)
224         {
225           for (; i < per_line; i++)
226             fprintf(stream, "   ");
227           fprintf(stream, "|");
228           for (i = 0; i < start; i++)
229             fprintf(stream, " ");
230           for (; i < end; i++) {
231               int c = buf[i - start];
232               putc(c >= 32 && c < 127 ? c : '.', stream);
233           }
234           for (; i < per_line; i++)
235             fprintf(stream, " ");
236           fprintf(stream, "|");
237         }
238       fprintf(stream, "\n");
239
240       ofs += n;
241       buf += n;
242       size -= n;
243     }
244 }