-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.c
153 lines (121 loc) · 2.44 KB
/
interface.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
/*
*2012.02.19 by Aaron
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int create_server(char *domain)
{
int listen_fd;
struct sockaddr_un srv_addr;
listen_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(listen_fd<0){
return 0;
}
srv_addr.sun_family=AF_UNIX;
strncpy(srv_addr.sun_path, domain, sizeof(srv_addr.sun_path)-1);
unlink(domain);
if(bind(listen_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr))<0){
close(listen_fd);
unlink(domain);
return -1;
}
return listen_fd;
}
int send_data_my(char *domain, void *data, int len)
{
int client_fd;
struct sockaddr_un srv_addr;
char *pdata = data;
client_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(client_fd<0){
return -1;
}
srv_addr.sun_family = AF_UNIX;
strncpy(srv_addr.sun_path, domain, sizeof(srv_addr.sun_path)-1);
if(connect(client_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr))<0){
close(client_fd);
return -1;
}
#define SZ_4K 4*1024
while(len>0){
if(len>SZ_4K){
write(client_fd, pdata, SZ_4K);
len -= SZ_4K;
pdata += SZ_4K;
}else{
write(client_fd, pdata, len);
len -= len;
}
}
#undef SZ_4K
close(client_fd);
return 0;
}
int connect_local(char *domain)
{
int client_fd;
struct sockaddr_un srv_addr;
client_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(client_fd<0){
return -1;
}
srv_addr.sun_family = AF_UNIX;
strncpy(srv_addr.sun_path, domain, sizeof(srv_addr.sun_path)-1);
if(connect(client_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr))<0){
close(client_fd);
return -1;
}
return client_fd;
}
int send_local(int socket, char *pbuf, int buf_len)
{
if(socket<=0)
return -1;
#define SZ_4K 4*1024
while(buf_len>0){
if(buf_len>SZ_4K){
write(socket, pbuf, SZ_4K);
buf_len -= SZ_4K;
pbuf += SZ_4K;
}else{
write(socket, pbuf, buf_len);
buf_len -= buf_len;
}
}
#undef SZ_4K
return 0;
}
int recv_local(int socket, char *pbuf, int buf_len)
{
char *p_tmp;
int reallen = 0;
fd_set readset;
int ret = 0;
struct timeval timeout;
if(socket<=0||pbuf==NULL){
return -1;
}
p_tmp = pbuf;
FD_ZERO(&readset);
FD_SET(socket, &readset);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
ret = select(socket+1, &readset, NULL, NULL, &timeout);
if(ret<=0){
return reallen;
}
if(FD_ISSET(socket, &readset)){
reallen = read(socket, p_tmp, buf_len);
}
p_tmp[reallen] = '\n';
return reallen;
}
void close_local(int socket)
{
if(socket<=0)
return;
close(socket);
return;
}