1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
42 const char *program_name;
47 ofp_fatal(0, "virtual memory exhausted");
51 xcalloc(size_t count, size_t size)
53 void *p = count && size ? calloc(count, size) : malloc(1);
63 void *p = malloc(size ? size : 1);
71 xrealloc(void *p, size_t size)
73 p = realloc(p, size ? size : 1);
81 xmemdup(const void *p_, size_t size)
83 void *p = xmalloc(size);
89 xmemdup0(const char *p_, size_t length)
91 char *p = xmalloc(length + 1);
92 memcpy(p, p_, length);
98 xstrdup(const char *s)
100 return xmemdup0(s, strlen(s));
104 xvasprintf(const char *format, va_list args)
110 va_copy(args2, args);
111 needed = vsnprintf(NULL, 0, format, args);
113 s = xmalloc(needed + 1);
115 vsnprintf(s, needed + 1, format, args2);
122 xasprintf(const char *format, ...)
127 va_start(args, format);
128 s = xvasprintf(format, args);
135 strlcpy(char *dst, const char *src, size_t size)
138 size_t n = strlen(src);
139 size_t n_copy = MIN(n, size - 1);
140 memcpy(dst, src, n_copy);
146 ofp_fatal(int err_no, const char *format, ...)
150 fprintf(stderr, "%s: ", program_name);
151 va_start(args, format);
152 vfprintf(stderr, format, args);
155 fprintf(stderr, " (%s)", strerror(err_no));
162 ofp_error(int err_no, const char *format, ...)
164 int save_errno = errno;
167 fprintf(stderr, "%s: ", program_name);
168 va_start(args, format);
169 vfprintf(stderr, format, args);
172 fprintf(stderr, " (%s)", strerror(err_no));
178 /* Sets program_name based on 'argv0'. Should be called at the beginning of
179 * main(), as "set_program_name(argv[0]);". */
180 void set_program_name(const char *argv0)
182 const char *slash = strrchr(argv0, '/');
183 program_name = slash ? slash + 1 : argv0;
186 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
187 * line. Numeric offsets are also included, starting at 'ofs' for the first
188 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
189 * are also rendered alongside. */
191 ofp_hex_dump(FILE *stream, const void *buf_, size_t size,
192 uintptr_t ofs, bool ascii)
194 const uint8_t *buf = buf_;
195 const size_t per_line = 16; /* Maximum bytes per line. */
199 size_t start, end, n;
202 /* Number of bytes on this line. */
203 start = ofs % per_line;
205 if (end - start > size)
210 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
211 for (i = 0; i < start; i++)
212 fprintf(stream, " ");
214 fprintf(stream, "%02hhx%c",
215 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
218 for (; i < per_line; i++)
219 fprintf(stream, " ");
220 fprintf(stream, "|");
221 for (i = 0; i < start; i++)
222 fprintf(stream, " ");
223 for (; i < end; i++) {
224 int c = buf[i - start];
225 putc(c >= 32 && c < 127 ? c : '.', stream);
227 for (; i < per_line; i++)
228 fprintf(stream, " ");
229 fprintf(stream, "|");
231 fprintf(stream, "\n");