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