stun

simple point to point tunnel
git clone git://git.2f30.org/stun
Log | Files | Refs | README

commit f336dd71c2825c5a19d14af24dee25d12c1cb629
parent a51807f54473ef2f4458785dd2bb477eb6c35ae7
Author: sin <sin@2f30.org>
Date:   Wed, 30 Mar 2016 16:09:36 +0100

move util funcs towards the top

Diffstat:
Mstun.c | 162++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 81 insertions(+), 81 deletions(-)

diff --git a/stun.c b/stun.c @@ -152,6 +152,87 @@ logerr(char *msg, ...) } void +pack16(unsigned char *buf, uint16_t n) +{ + buf[0] = n >> 8 & 0xff; + buf[1] = n & 0xff; +} + +uint16_t +unpack16(unsigned char *buf) +{ + return buf[0] << 8 | buf[1]; +} + +void +pack64(unsigned char *buf, uint64_t n) +{ + buf[0] = n >> 56 & 0xff; + buf[1] = n >> 48 & 0xff; + buf[2] = n >> 40 & 0xff; + buf[3] = n >> 32 & 0xff; + buf[4] = n >> 24 & 0xff; + buf[5] = n >> 16 & 0xff; + buf[6] = n >> 8 & 0xff; + buf[7] = n & 0xff; +} + +uint64_t +unpack64(unsigned char *buf) +{ + return (uint64_t)buf[0] << 56 | + (uint64_t)buf[1] << 48 | + (uint64_t)buf[2] << 40 | + (uint64_t)buf[3] << 32 | + (uint64_t)buf[4] << 24 | + (uint64_t)buf[5] << 16 | + (uint64_t)buf[6] << 8 | + (uint64_t)buf[7]; +} + +int +writeall(int fd, void *buf, int len) +{ + unsigned char *p = buf; + int n, total = 0; + + while (len > 0) { + n = write(fd, p + total, len); + if (n < 0) { + logwarn("write failed"); + total = -1; + break; + } else if (n == 0) { + break; + } + total += n; + len -= n; + } + return total; +} + +int +readall(int fd, void *buf, int len) +{ + unsigned char *p = buf; + int n, total = 0; + + while (len > 0) { + n = read(fd, p + total, len); + if (n < 0) { + logwarn("read failed"); + total = -1; + break; + } else if (n == 0) { + break; + } + total += n; + len -= n; + } + return total; +} + +void revokeprivs(void) { struct passwd *pw; @@ -375,87 +456,6 @@ readdev(int fd, unsigned char *buf, int len) } #endif -void -pack16(unsigned char *buf, uint16_t n) -{ - buf[0] = n >> 8 & 0xff; - buf[1] = n & 0xff; -} - -uint16_t -unpack16(unsigned char *buf) -{ - return buf[0] << 8 | buf[1]; -} - -void -pack64(unsigned char *buf, uint64_t n) -{ - buf[0] = n >> 56 & 0xff; - buf[1] = n >> 48 & 0xff; - buf[2] = n >> 40 & 0xff; - buf[3] = n >> 32 & 0xff; - buf[4] = n >> 24 & 0xff; - buf[5] = n >> 16 & 0xff; - buf[6] = n >> 8 & 0xff; - buf[7] = n & 0xff; -} - -uint64_t -unpack64(unsigned char *buf) -{ - return (uint64_t)buf[0] << 56 | - (uint64_t)buf[1] << 48 | - (uint64_t)buf[2] << 40 | - (uint64_t)buf[3] << 32 | - (uint64_t)buf[4] << 24 | - (uint64_t)buf[5] << 16 | - (uint64_t)buf[6] << 8 | - (uint64_t)buf[7]; -} - -int -writeall(int fd, void *buf, int len) -{ - unsigned char *p = buf; - int n, total = 0; - - while (len > 0) { - n = write(fd, p + total, len); - if (n < 0) { - logwarn("write failed"); - total = -1; - break; - } else if (n == 0) { - break; - } - total += n; - len -= n; - } - return total; -} - -int -readall(int fd, void *buf, int len) -{ - unsigned char *p = buf; - int n, total = 0; - - while (len > 0) { - n = read(fd, p + total, len); - if (n < 0) { - logwarn("read failed"); - total = -1; - break; - } else if (n == 0) { - break; - } - total += n; - len -= n; - } - return total; -} - int writenet(int fd, unsigned char *pt, int len) {