stun

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

util.c (2574B)


      1 #include <sys/types.h>
      2 
      3 #include <netinet/in.h>
      4 #include <netinet/ip.h>
      5 #include <netdb.h>
      6 
      7 #include <fcntl.h>
      8 #include <grp.h>
      9 #include <pwd.h>
     10 #include <stdint.h>
     11 #include <stdio.h>
     12 #include <unistd.h>
     13 
     14 #include "warp.h"
     15 
     16 void
     17 pack16(unsigned char *buf, uint16_t n)
     18 {
     19 	buf[0] = n >> 8 & 0xff;
     20 	buf[1] = n & 0xff;
     21 }
     22 
     23 uint16_t
     24 unpack16(unsigned char *buf)
     25 {
     26 	return buf[0] << 8 | buf[1];
     27 }
     28 
     29 void
     30 pack64(unsigned char *buf, uint64_t n)
     31 {
     32 	buf[0] = n >> 56 & 0xff;
     33 	buf[1] = n >> 48 & 0xff;
     34 	buf[2] = n >> 40 & 0xff;
     35 	buf[3] = n >> 32 & 0xff;
     36 	buf[4] = n >> 24 & 0xff;
     37 	buf[5] = n >> 16 & 0xff;
     38 	buf[6] = n >> 8 & 0xff;
     39 	buf[7] = n & 0xff;
     40 }
     41 
     42 uint64_t
     43 unpack64(unsigned char *buf)
     44 {
     45 	return (uint64_t)buf[0] << 56 |
     46 	       (uint64_t)buf[1] << 48 |
     47 	       (uint64_t)buf[2] << 40 |
     48 	       (uint64_t)buf[3] << 32 |
     49 	       (uint64_t)buf[4] << 24 |
     50 	       (uint64_t)buf[5] << 16 |
     51 	       (uint64_t)buf[6] << 8  |
     52 	       (uint64_t)buf[7];
     53 }
     54 
     55 void
     56 revokeprivs(char *user)
     57 {
     58 	struct passwd *pw;
     59 
     60 	if (!(pw = getpwnam(user)))
     61 		fatalx("no %s user", user);
     62 	if (setgroups(1, &pw->pw_gid) < 0 ||
     63 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0 ||
     64 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
     65 		fatalx("failed to revoke privs");
     66 }
     67 
     68 int
     69 setnonblock(int fd, int mode)
     70 {
     71 	int flags;
     72 
     73 	flags = fcntl(fd, F_GETFL);
     74 	if (mode)
     75 		flags |= O_NONBLOCK;
     76 	else
     77 		flags &= ~O_NONBLOCK;
     78 	return fcntl(fd, F_SETFL, flags);
     79 }
     80 
     81 char *
     82 saddr_ntop(struct sockaddr *sa, socklen_t salen)
     83 {
     84 	static char buf[512];
     85 	char host[NI_MAXHOST], port[NI_MAXSERV];
     86 	int ret;
     87 
     88 	if ((ret = getnameinfo(sa, salen, host, sizeof(host),
     89 	                       port, sizeof(port),
     90 	                       NI_NUMERICHOST | NI_NUMERICSERV))) {
     91 		if (ret == EAI_SYSTEM) {
     92 			logwarnx("getnameinfo failed");
     93 			return NULL;
     94 		} else {
     95 			logwarnx("getnameinfo: %s", gai_strerror(ret));
     96 			return NULL;
     97 		}
     98 	}
     99 	snprintf(buf, sizeof(buf), "[%s]:%s", host, port);
    100 	return buf;
    101 }
    102 
    103 char *
    104 peer_ntop(int fd)
    105 {
    106 	struct sockaddr_storage ss;
    107 	socklen_t sslen = sizeof(ss);
    108 
    109 	if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
    110 		logwarn("getpeername");
    111 		return NULL;
    112 	}
    113 	return saddr_ntop((struct sockaddr *)&ss, sslen);
    114 }
    115 
    116 int
    117 ipversion(unsigned char *pkt)
    118 {
    119 	struct ip *ip = (struct ip *)pkt;
    120 
    121 	switch (ip->ip_v) {
    122 	case 4:
    123 		return AF_INET;
    124 	case 6:
    125 		return AF_INET6;
    126 	default:
    127 		logwarnx("unknown protocol version: %d", (int)ip->ip_v);
    128 		break;
    129 	}
    130 	return -1;
    131 }
    132 
    133 int
    134 mypledge(const char *promises, const char *paths[])
    135 {
    136 #if defined(__OpenBSD__)
    137 	return pledge(promises, paths);
    138 #endif
    139 	return 0;
    140 }