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