File: net.c

package info (click to toggle)
pcaputils 0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: ansic: 2,945; sh: 44; makefile: 38
file content (138 lines) | stat: -rw-r--r-- 3,557 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*

net.c - functions useful for programs which interact with the network

Copyright (C) 2008 Robert S. Edmonds 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

#include <errno.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>

#include "byte.h"
#include "net.h"
#include "scanfmt.h"
#include "uint.h"
#include "util.h"

/* functions */

u32 bswap32(u32 x){
	return  ((x << 24) & 0xff000000 ) |
		((x <<  8) & 0x00ff0000 ) |
		((x >>  8) & 0x0000ff00 ) |
		((x >> 24) & 0x000000ff );
}

u16 bswap16(u16 x){
	return (u16) ((x & 0xff) << 8 | (x & 0xff00) >> 8);
}

ipaddr_range_t sips_to_range(char *sip0, char *sip1){
	ipaddr_range_t ipr;
	scan_ip4(sip0, ipr.ip0);
	scan_ip4(sip1, ipr.ip1);
	return ipr;
}

char *human_readable_rate(u64 packets, u64 bytes, unsigned interval){
	char prefix_packets[] = "\0\0";
	char prefix_bytes[] = "\0\0";
	char *str;
	CALLOC(str, 64);

	double div_packets = ((double) packets) / interval;
	double div_bytes = ((double) bytes) / interval;

	if(div_packets >= 1E9){
		prefix_packets[0] = 'G';
		div_packets /= 1E9;
	}else if(div_packets >= 1E6){
		prefix_packets[0] = 'M';
		div_packets /= 1E6;
	}else if(div_packets >= 1E3){
		prefix_packets[0] = 'K';
		div_packets /= 1E3;
	}

	if(div_bytes >= 1E9){
		prefix_bytes[0] = 'G';
		div_bytes /= 1E9;
	}else if(div_bytes >= 1E6){
		prefix_bytes[0] = 'M';
		div_bytes /= 1E6;
	}else if(div_bytes >= 1E3){
		prefix_bytes[0] = 'K';
		div_bytes /= 1E3;
	}

	snprintf(str, 64, "%.2f %spps, %.2f %sBps",
		div_packets, prefix_packets, div_bytes, prefix_bytes);
	return str;
}

u16 random_unprivileged_port(void){
	long r = random();
	r &= 0xffff;
	r |= 0x400;
	return (u16) r;
}

bool gai4(const char *hostname, char ip[4]){
	struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = 0,
		.ai_protocol = 0
	};
	struct addrinfo *res;

	if(0 == getaddrinfo(hostname, NULL, &hints, &res)){
		byte_copy(ip, 4, (char *) &((struct sockaddr_in *) res->ai_addr)->sin_addr.s_addr);
		freeaddrinfo(res);
		return true;
	}else{
		DEBUG("getaddrinfo() returned: %s", strerror(errno));
	}
	return false;
}

bool gai6(const char *hostname, char ip[16]){
	struct addrinfo hints = {
		.ai_family = AF_INET6,
		.ai_socktype = 0,
		.ai_protocol = 0
	};
	struct addrinfo *res;

	if(0 == getaddrinfo(hostname, NULL, &hints, &res)){
		byte_copy(ip, 16, (char *) &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr);
		freeaddrinfo(res);
		return true;
	}else{
		DEBUG("getaddrinfo() returned: %s", strerror(errno));
	}
	return false;
}