-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathnethelper.c
176 lines (150 loc) · 3.88 KB
/
nethelper.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#define _POSIX_C_SOURCE 201112L // needed for struct addrinfo on Linux
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <math.h>
#include "nethelper.h"
#define INFO(...) printf(__VA_ARGS__); printf("\r\n")
#define ERR(...) printf(__VA_ARGS__); printf("\r\n")
#define DEBUG 1
#ifdef DEBUG
#define DBG(...) fprintf(stderr,__VA_ARGS__); fprintf(stderr,"\r\n")
#define DBGX(...) fprintf(stderr,__VA_ARGS__);
#else
#define DBG(...) {};
#define DBGX(...) {};
#endif
int setupAddress(
char *host,
char *port,
struct addrinfo **output,
int socktype,
int protocolFamily
) {
// check parameters
if(host==NULL||port==NULL||output==NULL) {
return -1;
}
// create hints for address lookup
struct addrinfo hints;
memset(&hints,0x00,sizeof(struct addrinfo));
hints.ai_flags = 0;
// check if host is numeric
int hostIsNumeric = 1;
for(unsigned int i=0; i<strlen(host); i++) {
if(host[i]>'9'||host[i]<'.') {
hostIsNumeric = 0;
break;
}
}
// check if port is numeric
int portIsNumeric = 1;
for(unsigned int i=0; i<strlen(port); i++) {
if(port[i]>'9'||port[i]<'.') {
portIsNumeric = 0;
break;
}
}
// deal with wildcard binding
if(!hostIsNumeric) {
if(strcmp(host,"*")==0||strcmp(host,"all")==0) {
hints.ai_flags |= AI_PASSIVE;
}
}
// set socket type
if((socktype==SOCK_DGRAM)||(socktype==SOCK_STREAM)||(socktype==SOCK_RAW)) {
hints.ai_socktype = socktype;
} else {
DBG("Unsupported socket type");
return -1;
}
DBG("Host/Port numeric? %d %d",hostIsNumeric,portIsNumeric);
// avoid doing respective lookups when either host and port or numeric
if(hostIsNumeric) {
hints.ai_flags |= AI_NUMERICHOST;
}
if(portIsNumeric) {
hints.ai_flags |= AI_NUMERICSERV;
}
// use IPv4 or IPv6 according to instruction
// try to ensure IP version works on operating system
hints.ai_flags |= AI_ADDRCONFIG;
if(protocolFamily==PF_INET||protocolFamily==PF_INET6) {
hints.ai_family = protocolFamily;
} else {
// let OS decide
hints.ai_family = PF_UNSPEC;
}
int error = getaddrinfo(host,port,&hints,output);
if(error) {
DBG("Error getting address info: %s.",gai_strerror(error));
return error;
}
return 0;
}
void printAddressStructures(struct addrinfo *addr) {
int count = 0;
while(addr) {
DBG("Address %d:",count++);
DBGX(" ");
switch(addr->ai_family) {
case AF_INET:
DBGX("IPv4");
break;
case AF_INET6:
DBGX("IPv6");
break;
default:
DBGX("Unknown address family");
break;
}
switch(addr->ai_socktype) {
case SOCK_DGRAM:
DBGX(", UDP");
break;
case SOCK_STREAM:
DBGX(", TCP");
break;
case SOCK_RAW:
DBGX(", RAW");
break;
default:
DBGX(", Unknown socket type.");
break;
}
// print out address host and port
struct sockaddr_in *v4Addr;
struct sockaddr_in6 *v6Addr;
char straddr[INET6_ADDRSTRLEN];
switch(addr->ai_family) {
case AF_INET:
v4Addr = (struct sockaddr_in*)addr->ai_addr;
DBGX(", %s:%d",inet_ntoa(v4Addr->sin_addr),ntohs(v4Addr->sin_port));
break;
case AF_INET6:
v6Addr = (struct sockaddr_in6*)addr->ai_addr;
DBGX(", %s:%d",inet_ntop(AF_INET6,&v6Addr->sin6_addr,straddr,sizeof(straddr)),ntohs(v6Addr->sin6_port));
break;
}
DBG(" ");
addr = addr->ai_next;
}
}
void printAddress(struct addrinfo *addr) {
// print out bound address
if(addr->ai_family==AF_INET) {
struct sockaddr_in *v4Addr = (struct sockaddr_in*)addr->ai_addr;
INFO("UDP socket: %s:%d",inet_ntoa(v4Addr->sin_addr),ntohs(v4Addr->sin_port));
} else if(addr->ai_family==AF_INET6) {
char straddr[INET6_ADDRSTRLEN];
struct sockaddr_in6 *v6Addr = (struct sockaddr_in6*)addr->ai_addr;
INFO("UDP socket: %s:%d",inet_ntop(AF_INET6,&v6Addr->sin6_addr,straddr,sizeof(straddr)),ntohs(v6Addr->sin6_port));
}
}