-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
98 lines (86 loc) · 2.54 KB
/
net.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
#include "common.h"
#include "debug.h"
#include <string.h>
#include <ctype.h>
int tcp_connect(const char *addrs , const char *port) {
debug("Connecting to %s:%s" , addrs , port);
int sock , err;
struct addrinfo hints = {} , *res , *p;
hints.ai_socktype= SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
if ((err = getaddrinfo(addrs , port , &hints , &res))) {
fatal("tcp_connect failed %s:%s : %s\n" , addrs , port , gai_strerror(err));
}
debug("Getaddrinfo successfull");
p = res;
do {
debug("ptr == %p" , p );
if ((sock = socket(p->ai_family , p->ai_socktype , p->ai_protocol)) < 0)
continue;
debug("socket created");
if (!connect(sock , p->ai_addr , p->ai_addrlen))
break;
debug("connect failed");
close(sock);
} while((p=p->ai_next)!=NULL);
if (p==NULL)
fatal("tcp_connect failed %s:%s " , addrs , port);
freeaddrinfo(res);
return sock;
}
int tcp_listen(const char *addrs , const char *port){
int sock , err;
const int on = 1;
struct addrinfo hints = {}, *res , *p;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
if ((err = getaddrinfo(addrs , port , &hints ,&res))) {
fatal("tcp_listen failed %s:%s %s\n" , addrs , port , gai_strerror(err));
}
p = res;
do {
if ((sock = socket(p->ai_family , p->ai_socktype , p->ai_protocol)) < 0)
continue;
Setsockopt(sock , SOL_SOCKET , SO_REUSEADDR , (void *)&on , sizeof(on));
if (!bind(sock , p->ai_addr , p->ai_addrlen))
break;
close(sock);
} while((p=p->ai_next)!=NULL);
if (p==NULL)
fatal("tcp_listen failed %s:%s " , addrs , port);
freeaddrinfo(res);
Listen(sock , 5);
return sock;
}
void dump_data(char *buff , long len){
for(long i=0 ; i<len ; i+= 16){
printf("0x%08lx | " , i);
for(int j = 0 ; j<16 ; j++){
if (i+j < len) printf("%02hhx " , buff[i+j]);
else printf(" ");
}
printf("| ");
for(int j = 0 ; j<16 ; j++){
if (i+j < len && isgraph(buff[i+j])) printf("%c" , (unsigned char)buff[i+j]);
else printf(".");
}
puts("");
}
}
const char *sock_ntop(const struct sockaddr *src , char *dst , int maxsize){
char temp[128] = {};
switch(src->sa_family){
case AF_INET:
snprintf(dst , maxsize, "%s:%d" ,
inet_ntop(AF_INET , &((struct sockaddr_in *)src)->sin_addr , temp , INET_ADDRSTRLEN) ,
ntohs(((struct sockaddr_in *)src)->sin_port));
return dst;
case AF_INET6:
snprintf(dst , maxsize, "%s:%d",
inet_ntop(AF_INET6 , &((struct sockaddr_in6 *)src)->sin6_addr , temp , INET6_ADDRSTRLEN) ,
ntohs(((struct sockaddr_in6 *)src)->sin6_port));
return dst;
}
return NULL;
}