-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockinfo.c
172 lines (151 loc) · 4.35 KB
/
sockinfo.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
#include "sockinfo.h"
#include "config.h"
#include "net.h"
const char *sockinfo_family_name(TSockInfo *SockInfo)
{
const char *sock_family="";
int size, val;
switch (SockInfo->family)
{
case AF_PACKET :
sock_family="raw";
break;
case AF_UNSPEC :
case AF_UNIX :
sock_family="unix";
break;
case AF_NETLINK :
sock_family="netlink";
break;
case AF_INET :
if (SockInfo->socket > -1)
{
size=sizeof(val);
getsockopt(SockInfo->socket, SOL_SOCKET, SO_TYPE, &val, &size);
if (val==SOCK_STREAM) sock_family="tcp";
else if (val==SOCK_DGRAM) sock_family="udp";
else sock_family="ip4";
}
else sock_family="ip4";
break;
case AF_INET6 :
if (SockInfo->socket > -1)
{
size=sizeof(val);
getsockopt(SockInfo->socket, SOL_SOCKET, SO_TYPE, &val, &size);
if (val==SOCK_STREAM) sock_family="tcp6";
else if (val==SOCK_DGRAM) sock_family="udp6";
else sock_family="ip6";
}
else sock_family="ip6";
break;
case AF_IPX :
sock_family="ipx";
break;
case AF_X25 :
sock_family="x25";
break;
case AF_IRDA :
sock_family="irda";
break;
case AF_BLUETOOTH :
sock_family="bluetooth";
break;
default:
sock_family="other";
break;
/*
case AF_FILE :
case AF_APPLETALK :
case AF_NETROM :
case AF_BRIDGE :
case AF_ATMPVC :
case AF_X25 :
case AF_ROSE :
case AF_DECnet :
case AF_NETBEUI :
case AF_SECURITY :
case AF_KEY :
case AF_ASH :
case AF_ECONET :
case AF_ATMSVC :
case AF_RDS :
case AF_SNA :
case AF_PPPOX :
case AF_WANPIPE :
case AF_LLC :
case AF_CAN :
case AF_TIPC :
case AF_IUCV :
case AF_RXRPC :
case AF_ISDN :
case AF_PHONET :
case AF_IEEE802154 :
case AF_CAIF :
case AF_ALG :
case AF_NFC :
case AF_VSOCK :
*/
}
return(sock_family);
}
TSockInfo *enhancer_createSockInfo(int FuncID, int socket, const struct sockaddr *sa, int salen)
{
struct sockaddr_in6 *sa_in6;
struct sockaddr_in *sa_in;
struct sockaddr_un *sa_un;
struct ucred SockCreds;
TSockInfo *sockinfo;
if (! sa) return(NULL);
sockinfo=(TSockInfo *) calloc(1, sizeof(TSockInfo));
sockinfo->socket=socket;
sockinfo->sa=sa;
sockinfo->salen=salen;
sockinfo->pid=-1;
sockinfo->uid=-1;
sockinfo->gid=-1;
if (sockinfo->sa) sockinfo->family=sockinfo->sa->sa_family;
switch (sockinfo->family)
{
case AF_INET:
sa_in=(struct sockaddr_in *) sa;
sockinfo->port=ntohs(sa_in->sin_port);
sockinfo->address=(char *) realloc(sockinfo->address, INET6_ADDRSTRLEN+1);
inet_ntop(AF_INET,&sa_in->sin_addr, sockinfo->address,INET_ADDRSTRLEN);
break;
case AF_INET6:
sa_in6=(struct sockaddr_in6 *) sa;
sockinfo->port=ntohs(sa_in6->sin6_port);
sockinfo->address=(char *) realloc(sockinfo->address, INET6_ADDRSTRLEN+1);
inet_ntop(AF_INET6,&sa_in6->sin6_addr, sockinfo->address,INET6_ADDRSTRLEN);
enhancer_strrep(sockinfo->address, ':', '.');
break;
case AF_UNIX:
if (sockinfo->sa &&
((FuncID == FUNC_CONNECT) || (FuncID == FUNC_BIND) || (FuncID == FUNC_ACCEPT) )
)
{
sa_un=(struct sockaddr_un *) sockinfo->sa;
if (strvalid(sa_un->sun_path)) sockinfo->address=enhancer_strcpy(sockinfo->address, sa_un->sun_path);
if (sockinfo->socket > -1)
{
salen=sizeof(struct ucred);
getsockopt(sockinfo->socket, SOL_SOCKET, SO_PEERCRED, & SockCreds, &salen);
sockinfo->pid=SockCreds.pid;
sockinfo->uid=SockCreds.uid;
sockinfo->gid=SockCreds.gid;
}
}
break;
}
return(sockinfo);
}
void enhancer_destroySockInfo(TSockInfo *SI)
{
if (SI)
{
destroy(SI->address);
destroy(SI->redirect);
free(SI);
}
}