]> andersk Git - openssh.git/blob - misc.c
fc3fcca44b8c523a7eed02cd538872fba1f832db
[openssh.git] / misc.c
1 /* $OpenBSD: misc.c,v 1.61 2006/07/26 02:35:17 stevesk Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  * Copyright (c) 2005,2006 Damien Miller.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/param.h>
33
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40
41 #include <errno.h>
42 #include <fcntl.h>
43 #ifdef HAVE_PATHS_H
44 # include <paths.h>
45 #include <pwd.h>
46 #endif
47 #ifdef SSH_TUN_OPENBSD
48 #include <net/if.h>
49 #endif
50
51 #include "misc.h"
52 #include "log.h"
53 #include "xmalloc.h"
54 #include "ssh.h"
55
56 /* remove newline at end of string */
57 char *
58 chop(char *s)
59 {
60         char *t = s;
61         while (*t) {
62                 if (*t == '\n' || *t == '\r') {
63                         *t = '\0';
64                         return s;
65                 }
66                 t++;
67         }
68         return s;
69
70 }
71
72 /* set/unset filedescriptor to non-blocking */
73 int
74 set_nonblock(int fd)
75 {
76         int val;
77
78         val = fcntl(fd, F_GETFL, 0);
79         if (val < 0) {
80                 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
81                 return (-1);
82         }
83         if (val & O_NONBLOCK) {
84                 debug3("fd %d is O_NONBLOCK", fd);
85                 return (0);
86         }
87         debug2("fd %d setting O_NONBLOCK", fd);
88         val |= O_NONBLOCK;
89         if (fcntl(fd, F_SETFL, val) == -1) {
90                 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
91                     strerror(errno));
92                 return (-1);
93         }
94         return (0);
95 }
96
97 int
98 unset_nonblock(int fd)
99 {
100         int val;
101
102         val = fcntl(fd, F_GETFL, 0);
103         if (val < 0) {
104                 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
105                 return (-1);
106         }
107         if (!(val & O_NONBLOCK)) {
108                 debug3("fd %d is not O_NONBLOCK", fd);
109                 return (0);
110         }
111         debug("fd %d clearing O_NONBLOCK", fd);
112         val &= ~O_NONBLOCK;
113         if (fcntl(fd, F_SETFL, val) == -1) {
114                 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
115                     fd, strerror(errno));
116                 return (-1);
117         }
118         return (0);
119 }
120
121 /* disable nagle on socket */
122 void
123 set_nodelay(int fd)
124 {
125         int opt;
126         socklen_t optlen;
127
128         optlen = sizeof opt;
129         if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
130                 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
131                 return;
132         }
133         if (opt == 1) {
134                 debug2("fd %d is TCP_NODELAY", fd);
135                 return;
136         }
137         opt = 1;
138         debug2("fd %d setting TCP_NODELAY", fd);
139         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
140                 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
141 }
142
143 /* Characters considered whitespace in strsep calls. */
144 #define WHITESPACE " \t\r\n"
145 #define QUOTE   "\""
146
147 /* return next token in configuration line */
148 char *
149 strdelim(char **s)
150 {
151         char *old;
152         int wspace = 0;
153
154         if (*s == NULL)
155                 return NULL;
156
157         old = *s;
158
159         *s = strpbrk(*s, WHITESPACE QUOTE "=");
160         if (*s == NULL)
161                 return (old);
162
163         if (*s[0] == '\"') {
164                 memmove(*s, *s + 1, strlen(*s)); /* move nul too */
165                 /* Find matching quote */
166                 if ((*s = strpbrk(*s, QUOTE)) == NULL) {
167                         return (NULL);          /* no matching quote */
168                 } else {
169                         *s[0] = '\0';
170                         return (old);
171                 }
172         }
173
174         /* Allow only one '=' to be skipped */
175         if (*s[0] == '=')
176                 wspace = 1;
177         *s[0] = '\0';
178
179         /* Skip any extra whitespace after first token */
180         *s += strspn(*s + 1, WHITESPACE) + 1;
181         if (*s[0] == '=' && !wspace)
182                 *s += strspn(*s + 1, WHITESPACE) + 1;
183
184         return (old);
185 }
186
187 struct passwd *
188 pwcopy(struct passwd *pw)
189 {
190         struct passwd *copy = xcalloc(1, sizeof(*copy));
191
192         copy->pw_name = xstrdup(pw->pw_name);
193         copy->pw_passwd = xstrdup(pw->pw_passwd);
194         copy->pw_gecos = xstrdup(pw->pw_gecos);
195         copy->pw_uid = pw->pw_uid;
196         copy->pw_gid = pw->pw_gid;
197 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
198         copy->pw_expire = pw->pw_expire;
199 #endif
200 #ifdef HAVE_PW_CHANGE_IN_PASSWD
201         copy->pw_change = pw->pw_change;
202 #endif
203 #ifdef HAVE_PW_CLASS_IN_PASSWD
204         copy->pw_class = xstrdup(pw->pw_class);
205 #endif
206         copy->pw_dir = xstrdup(pw->pw_dir);
207         copy->pw_shell = xstrdup(pw->pw_shell);
208         return copy;
209 }
210
211 /*
212  * Convert ASCII string to TCP/IP port number.
213  * Port must be >0 and <=65535.
214  * Return 0 if invalid.
215  */
216 int
217 a2port(const char *s)
218 {
219         long port;
220         char *endp;
221
222         errno = 0;
223         port = strtol(s, &endp, 0);
224         if (s == endp || *endp != '\0' ||
225             (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
226             port <= 0 || port > 65535)
227                 return 0;
228
229         return port;
230 }
231
232 int
233 a2tun(const char *s, int *remote)
234 {
235         const char *errstr = NULL;
236         char *sp, *ep;
237         int tun;
238
239         if (remote != NULL) {
240                 *remote = SSH_TUNID_ANY;
241                 sp = xstrdup(s);
242                 if ((ep = strchr(sp, ':')) == NULL) {
243                         xfree(sp);
244                         return (a2tun(s, NULL));
245                 }
246                 ep[0] = '\0'; ep++;
247                 *remote = a2tun(ep, NULL);
248                 tun = a2tun(sp, NULL);
249                 xfree(sp);
250                 return (*remote == SSH_TUNID_ERR ? *remote : tun);
251         }
252
253         if (strcasecmp(s, "any") == 0)
254                 return (SSH_TUNID_ANY);
255
256         tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
257         if (errstr != NULL)
258                 return (SSH_TUNID_ERR);
259
260         return (tun);
261 }
262
263 #define SECONDS         1
264 #define MINUTES         (SECONDS * 60)
265 #define HOURS           (MINUTES * 60)
266 #define DAYS            (HOURS * 24)
267 #define WEEKS           (DAYS * 7)
268
269 /*
270  * Convert a time string into seconds; format is
271  * a sequence of:
272  *      time[qualifier]
273  *
274  * Valid time qualifiers are:
275  *      <none>  seconds
276  *      s|S     seconds
277  *      m|M     minutes
278  *      h|H     hours
279  *      d|D     days
280  *      w|W     weeks
281  *
282  * Examples:
283  *      90m     90 minutes
284  *      1h30m   90 minutes
285  *      2d      2 days
286  *      1w      1 week
287  *
288  * Return -1 if time string is invalid.
289  */
290 long
291 convtime(const char *s)
292 {
293         long total, secs;
294         const char *p;
295         char *endp;
296
297         errno = 0;
298         total = 0;
299         p = s;
300
301         if (p == NULL || *p == '\0')
302                 return -1;
303
304         while (*p) {
305                 secs = strtol(p, &endp, 10);
306                 if (p == endp ||
307                     (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
308                     secs < 0)
309                         return -1;
310
311                 switch (*endp++) {
312                 case '\0':
313                         endp--;
314                         break;
315                 case 's':
316                 case 'S':
317                         break;
318                 case 'm':
319                 case 'M':
320                         secs *= MINUTES;
321                         break;
322                 case 'h':
323                 case 'H':
324                         secs *= HOURS;
325                         break;
326                 case 'd':
327                 case 'D':
328                         secs *= DAYS;
329                         break;
330                 case 'w':
331                 case 'W':
332                         secs *= WEEKS;
333                         break;
334                 default:
335                         return -1;
336                 }
337                 total += secs;
338                 if (total < 0)
339                         return -1;
340                 p = endp;
341         }
342
343         return total;
344 }
345
346 /*
347  * Returns a standardized host+port identifier string.
348  * Caller must free returned string.
349  */
350 char *
351 put_host_port(const char *host, u_short port)
352 {
353         char *hoststr;
354
355         if (port == 0 || port == SSH_DEFAULT_PORT)
356                 return(xstrdup(host));
357         if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0)
358                 fatal("put_host_port: asprintf: %s", strerror(errno));
359         debug3("put_host_port: %s", hoststr);
360         return hoststr;
361 }
362
363 /*
364  * Search for next delimiter between hostnames/addresses and ports.
365  * Argument may be modified (for termination).
366  * Returns *cp if parsing succeeds.
367  * *cp is set to the start of the next delimiter, if one was found.
368  * If this is the last field, *cp is set to NULL.
369  */
370 char *
371 hpdelim(char **cp)
372 {
373         char *s, *old;
374
375         if (cp == NULL || *cp == NULL)
376                 return NULL;
377
378         old = s = *cp;
379         if (*s == '[') {
380                 if ((s = strchr(s, ']')) == NULL)
381                         return NULL;
382                 else
383                         s++;
384         } else if ((s = strpbrk(s, ":/")) == NULL)
385                 s = *cp + strlen(*cp); /* skip to end (see first case below) */
386
387         switch (*s) {
388         case '\0':
389                 *cp = NULL;     /* no more fields*/
390                 break;
391
392         case ':':
393         case '/':
394                 *s = '\0';      /* terminate */
395                 *cp = s + 1;
396                 break;
397
398         default:
399                 return NULL;
400         }
401
402         return old;
403 }
404
405 char *
406 cleanhostname(char *host)
407 {
408         if (*host == '[' && host[strlen(host) - 1] == ']') {
409                 host[strlen(host) - 1] = '\0';
410                 return (host + 1);
411         } else
412                 return host;
413 }
414
415 char *
416 colon(char *cp)
417 {
418         int flag = 0;
419
420         if (*cp == ':')         /* Leading colon is part of file name. */
421                 return (0);
422         if (*cp == '[')
423                 flag = 1;
424
425         for (; *cp; ++cp) {
426                 if (*cp == '@' && *(cp+1) == '[')
427                         flag = 1;
428                 if (*cp == ']' && *(cp+1) == ':' && flag)
429                         return (cp+1);
430                 if (*cp == ':' && !flag)
431                         return (cp);
432                 if (*cp == '/')
433                         return (0);
434         }
435         return (0);
436 }
437
438 /* function to assist building execv() arguments */
439 void
440 addargs(arglist *args, char *fmt, ...)
441 {
442         va_list ap;
443         char *cp;
444         u_int nalloc;
445         int r;
446
447         va_start(ap, fmt);
448         r = vasprintf(&cp, fmt, ap);
449         va_end(ap);
450         if (r == -1)
451                 fatal("addargs: argument too long");
452
453         nalloc = args->nalloc;
454         if (args->list == NULL) {
455                 nalloc = 32;
456                 args->num = 0;
457         } else if (args->num+2 >= nalloc)
458                 nalloc *= 2;
459
460         args->list = xrealloc(args->list, nalloc, sizeof(char *));
461         args->nalloc = nalloc;
462         args->list[args->num++] = cp;
463         args->list[args->num] = NULL;
464 }
465
466 void
467 replacearg(arglist *args, u_int which, char *fmt, ...)
468 {
469         va_list ap;
470         char *cp;
471         int r;
472
473         va_start(ap, fmt);
474         r = vasprintf(&cp, fmt, ap);
475         va_end(ap);
476         if (r == -1)
477                 fatal("replacearg: argument too long");
478
479         if (which >= args->num)
480                 fatal("replacearg: tried to replace invalid arg %d >= %d",
481                     which, args->num);
482         xfree(args->list[which]);
483         args->list[which] = cp;
484 }
485
486 void
487 freeargs(arglist *args)
488 {
489         u_int i;
490
491         if (args->list != NULL) {
492                 for (i = 0; i < args->num; i++)
493                         xfree(args->list[i]);
494                 xfree(args->list);
495                 args->nalloc = args->num = 0;
496                 args->list = NULL;
497         }
498 }
499
500 /*
501  * Expands tildes in the file name.  Returns data allocated by xmalloc.
502  * Warning: this calls getpw*.
503  */
504 char *
505 tilde_expand_filename(const char *filename, uid_t uid)
506 {
507         const char *path;
508         char user[128], ret[MAXPATHLEN];
509         struct passwd *pw;
510         u_int len, slash;
511
512         if (*filename != '~')
513                 return (xstrdup(filename));
514         filename++;
515
516         path = strchr(filename, '/');
517         if (path != NULL && path > filename) {          /* ~user/path */
518                 slash = path - filename;
519                 if (slash > sizeof(user) - 1)
520                         fatal("tilde_expand_filename: ~username too long");
521                 memcpy(user, filename, slash);
522                 user[slash] = '\0';
523                 if ((pw = getpwnam(user)) == NULL)
524                         fatal("tilde_expand_filename: No such user %s", user);
525         } else if ((pw = getpwuid(uid)) == NULL)        /* ~/path */
526                 fatal("tilde_expand_filename: No such uid %d", uid);
527
528         if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
529                 fatal("tilde_expand_filename: Path too long");
530
531         /* Make sure directory has a trailing '/' */
532         len = strlen(pw->pw_dir);
533         if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
534             strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
535                 fatal("tilde_expand_filename: Path too long");
536
537         /* Skip leading '/' from specified path */
538         if (path != NULL)
539                 filename = path + 1;
540         if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
541                 fatal("tilde_expand_filename: Path too long");
542
543         return (xstrdup(ret));
544 }
545
546 /*
547  * Expand a string with a set of %[char] escapes. A number of escapes may be
548  * specified as (char *escape_chars, char *replacement) pairs. The list must
549  * be terminated by a NULL escape_char. Returns replaced string in memory
550  * allocated by xmalloc.
551  */
552 char *
553 percent_expand(const char *string, ...)
554 {
555 #define EXPAND_MAX_KEYS 16
556         struct {
557                 const char *key;
558                 const char *repl;
559         } keys[EXPAND_MAX_KEYS];
560         u_int num_keys, i, j;
561         char buf[4096];
562         va_list ap;
563
564         /* Gather keys */
565         va_start(ap, string);
566         for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
567                 keys[num_keys].key = va_arg(ap, char *);
568                 if (keys[num_keys].key == NULL)
569                         break;
570                 keys[num_keys].repl = va_arg(ap, char *);
571                 if (keys[num_keys].repl == NULL)
572                         fatal("percent_expand: NULL replacement");
573         }
574         va_end(ap);
575
576         if (num_keys >= EXPAND_MAX_KEYS)
577                 fatal("percent_expand: too many keys");
578
579         /* Expand string */
580         *buf = '\0';
581         for (i = 0; *string != '\0'; string++) {
582                 if (*string != '%') {
583  append:
584                         buf[i++] = *string;
585                         if (i >= sizeof(buf))
586                                 fatal("percent_expand: string too long");
587                         buf[i] = '\0';
588                         continue;
589                 }
590                 string++;
591                 if (*string == '%')
592                         goto append;
593                 for (j = 0; j < num_keys; j++) {
594                         if (strchr(keys[j].key, *string) != NULL) {
595                                 i = strlcat(buf, keys[j].repl, sizeof(buf));
596                                 if (i >= sizeof(buf))
597                                         fatal("percent_expand: string too long");
598                                 break;
599                         }
600                 }
601                 if (j >= num_keys)
602                         fatal("percent_expand: unknown key %%%c", *string);
603         }
604         return (xstrdup(buf));
605 #undef EXPAND_MAX_KEYS
606 }
607
608 /*
609  * Read an entire line from a public key file into a static buffer, discarding
610  * lines that exceed the buffer size.  Returns 0 on success, -1 on failure.
611  */
612 int
613 read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
614    u_long *lineno)
615 {
616         while (fgets(buf, bufsz, f) != NULL) {
617                 (*lineno)++;
618                 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
619                         return 0;
620                 } else {
621                         debug("%s: %s line %lu exceeds size limit", __func__,
622                             filename, *lineno);
623                         /* discard remainder of line */
624                         while (fgetc(f) != '\n' && !feof(f))
625                                 ;       /* nothing */
626                 }
627         }
628         return -1;
629 }
630
631 int
632 tun_open(int tun, int mode)
633 {
634 #if defined(CUSTOM_SYS_TUN_OPEN)
635         return (sys_tun_open(tun, mode));
636 #elif defined(SSH_TUN_OPENBSD)
637         struct ifreq ifr;
638         char name[100];
639         int fd = -1, sock;
640
641         /* Open the tunnel device */
642         if (tun <= SSH_TUNID_MAX) {
643                 snprintf(name, sizeof(name), "/dev/tun%d", tun);
644                 fd = open(name, O_RDWR);
645         } else if (tun == SSH_TUNID_ANY) {
646                 for (tun = 100; tun >= 0; tun--) {
647                         snprintf(name, sizeof(name), "/dev/tun%d", tun);
648                         if ((fd = open(name, O_RDWR)) >= 0)
649                                 break;
650                 }
651         } else {
652                 debug("%s: invalid tunnel %u", __func__, tun);
653                 return (-1);
654         }
655
656         if (fd < 0) {
657                 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
658                 return (-1);
659         }
660
661         debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
662
663         /* Set the tunnel device operation mode */
664         snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
665         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
666                 goto failed;
667
668         if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
669                 goto failed;
670
671         /* Set interface mode */
672         ifr.ifr_flags &= ~IFF_UP;
673         if (mode == SSH_TUNMODE_ETHERNET)
674                 ifr.ifr_flags |= IFF_LINK0;
675         else
676                 ifr.ifr_flags &= ~IFF_LINK0;
677         if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
678                 goto failed;
679
680         /* Bring interface up */
681         ifr.ifr_flags |= IFF_UP;
682         if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
683                 goto failed;
684
685         close(sock);
686         return (fd);
687
688  failed:
689         if (fd >= 0)
690                 close(fd);
691         if (sock >= 0)
692                 close(sock);
693         debug("%s: failed to set %s mode %d: %s", __func__, name,
694             mode, strerror(errno));
695         return (-1);
696 #else
697         error("Tunnel interfaces are not supported on this platform");
698         return (-1);
699 #endif
700 }
701
702 void
703 sanitise_stdfd(void)
704 {
705         int nullfd, dupfd;
706
707         if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
708                 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
709                 exit(1);
710         }
711         while (++dupfd <= 2) {
712                 /* Only clobber closed fds */
713                 if (fcntl(dupfd, F_GETFL, 0) >= 0)
714                         continue;
715                 if (dup2(nullfd, dupfd) == -1) {
716                         fprintf(stderr, "dup2: %s", strerror(errno));
717                         exit(1);
718                 }
719         }
720         if (nullfd > 2)
721                 close(nullfd);
722 }
723
724 char *
725 tohex(const void *vp, size_t l)
726 {
727         const u_char *p = (const u_char *)vp;
728         char b[3], *r;
729         size_t i, hl;
730
731         if (l > 65536)
732                 return xstrdup("tohex: length > 65536");
733
734         hl = l * 2 + 1;
735         r = xcalloc(1, hl);
736         for (i = 0; i < l; i++) {
737                 snprintf(b, sizeof(b), "%02x", p[i]);
738                 strlcat(r, b, hl);
739         }
740         return (r);
741 }
742
743 u_int64_t
744 get_u64(const void *vp)
745 {
746         const u_char *p = (const u_char *)vp;
747         u_int64_t v;
748
749         v  = (u_int64_t)p[0] << 56;
750         v |= (u_int64_t)p[1] << 48;
751         v |= (u_int64_t)p[2] << 40;
752         v |= (u_int64_t)p[3] << 32;
753         v |= (u_int64_t)p[4] << 24;
754         v |= (u_int64_t)p[5] << 16;
755         v |= (u_int64_t)p[6] << 8;
756         v |= (u_int64_t)p[7];
757
758         return (v);
759 }
760
761 u_int32_t
762 get_u32(const void *vp)
763 {
764         const u_char *p = (const u_char *)vp;
765         u_int32_t v;
766
767         v  = (u_int32_t)p[0] << 24;
768         v |= (u_int32_t)p[1] << 16;
769         v |= (u_int32_t)p[2] << 8;
770         v |= (u_int32_t)p[3];
771
772         return (v);
773 }
774
775 u_int16_t
776 get_u16(const void *vp)
777 {
778         const u_char *p = (const u_char *)vp;
779         u_int16_t v;
780
781         v  = (u_int16_t)p[0] << 8;
782         v |= (u_int16_t)p[1];
783
784         return (v);
785 }
786
787 void
788 put_u64(void *vp, u_int64_t v)
789 {
790         u_char *p = (u_char *)vp;
791
792         p[0] = (u_char)(v >> 56) & 0xff;
793         p[1] = (u_char)(v >> 48) & 0xff;
794         p[2] = (u_char)(v >> 40) & 0xff;
795         p[3] = (u_char)(v >> 32) & 0xff;
796         p[4] = (u_char)(v >> 24) & 0xff;
797         p[5] = (u_char)(v >> 16) & 0xff;
798         p[6] = (u_char)(v >> 8) & 0xff;
799         p[7] = (u_char)v & 0xff;
800 }
801
802 void
803 put_u32(void *vp, u_int32_t v)
804 {
805         u_char *p = (u_char *)vp;
806
807         p[0] = (u_char)(v >> 24) & 0xff;
808         p[1] = (u_char)(v >> 16) & 0xff;
809         p[2] = (u_char)(v >> 8) & 0xff;
810         p[3] = (u_char)v & 0xff;
811 }
812
813
814 void
815 put_u16(void *vp, u_int16_t v)
816 {
817         u_char *p = (u_char *)vp;
818
819         p[0] = (u_char)(v >> 8) & 0xff;
820         p[1] = (u_char)v & 0xff;
821 }
This page took 0.089229 seconds and 3 git commands to generate.