-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms.c
276 lines (240 loc) · 5.66 KB
/
comms.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <comms.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#define TIMEOUT 5
#define NORMAL_PCK 8
#define DELETE_CONN_PCK 9
#define ACK_CONN_PCK 11
#define CONN_DATASIZE 256
/*
** All the information required to
** process an incoming message is contained
** in this struct. The messages transmitted are
** of fixed size.
*/
struct message{
int64_t size;
int64_t type;
} ;
struct connectionmessage {
int64_t size;
char data[CONN_DATASIZE];
} ;
typedef struct connectionmessage ConnectionMessage;
typedef struct message Message;
typedef struct listener {
int l_fd;
char * addr;
} Listener;
struct connection {
int w_fd;
int r_fd;
};
static int64_t write_msg(int w_fd,const int8_t * m,int64_t type,int64_t size);
static int64_t write_conn_msg(int w_fd,const char * m,int64_t size);
static Connection * newConnection(int w,int r);
static Listener * newListener(int fd,char * p);
Listener_p mm_listen(char * addr){
int l_fd;
if(addr==NULL){
return NULL;
}
if(mkfifo(addr, S_IWUSR | S_IRUSR)!=0){
return NULL;
}
l_fd=open(addr,O_RDWR);
if (l_fd < 0) {
remove(addr);
return NULL;
}
return newListener(l_fd,addr);
}
Connection * mm_connect(char * addr){
if(addr == NULL){
return NULL;
}
struct timeval to;
fd_set fds;
int w_fd=open(addr,O_WRONLY),ret;
Connection * c;
int pid = getpid();
char r_addr[20], w_addr[20];
sprintf(r_addr,"/tmp/r%d",pid);
sprintf(w_addr,"/tmp/w%d",pid);
if(mkfifo(r_addr,S_IWUSR | S_IRUSR) || mkfifo(w_addr,S_IWUSR | S_IRUSR)){
return NULL;
}
int size = strlen(r_addr)+strlen(w_addr)+2;
char * msg = malloc(size);
memcpy(msg,r_addr,strlen(r_addr)+1);
memcpy(msg+strlen(r_addr)+1,w_addr,strlen(w_addr)+1);
write_conn_msg(w_fd,msg,size);
int r=open(r_addr,O_RDONLY);
void * buf = malloc(sizeof(Message));
int w=open(w_addr,O_WRONLY);
to.tv_sec=TIMEOUT;
to.tv_usec=0;
FD_SET(r, &fds);
ret = select(r + 1, &fds, NULL, NULL, &to);
if (ret > 0) {
read(r,(char *)buf,sizeof(Message));
Message * msg = (Message *) buf;
if(msg->type==ACK_CONN_PCK){
puts("fifo\n");
c = newConnection(w,r);
free(buf);
return c;
}
}
free(buf);
close(w_fd);
return NULL;
}
void mm_disconnect(Connection * c){
int pid = getpid();
char buf[20];
snprintf(buf, 20, "%d", pid);
//write_msg(c->w_fd,buf,DELETE_CONN_PCK,strlen(buf)+1);
close(c->w_fd);
close(c->r_fd);
free(c);
}
void mm_disconnect_listener(Listener * l) {
close(l->l_fd);
remove(l->addr);
free(l->addr);
}
int64_t mm_write(Connection * c,const int8_t * m,int64_t size){
return write_msg(c->w_fd,m,NORMAL_PCK,size);
}
static int64_t write_msg(int w_fd,const int8_t * m,int64_t type,int64_t size){
Message msg;
memset(&msg,0,sizeof(Message));
msg.size=size;
msg.type=type;
write(w_fd,&msg,sizeof(Message));
if(size>0){
write(w_fd,m,size);
}
return size;
}
static int64_t write_conn_msg(int w_fd,const char * m,int64_t size){
ConnectionMessage msg ;
memset(&msg,0,sizeof(ConnectionMessage));
msg.size=size;
if(size>0){
memcpy(msg.data,m,size);
}
write(w_fd,&msg,sizeof(ConnectionMessage));
return size;
}
Connection * mm_accept(Listener_p l){
int64_t len;
void * msg = malloc(sizeof(ConnectionMessage));
if ((len = read(l->l_fd,msg,sizeof(ConnectionMessage))) < 0) {
return NULL;
}
ConnectionMessage * m = (ConnectionMessage *)msg;
char * buf = malloc(m->size);
memcpy(buf,m->data,m->size);
int w_fd,r_fd;
w_fd= open(buf,O_WRONLY);
r_fd= open(buf+strlen(buf)+1,O_RDONLY);
if (w_fd < 0 || r_fd < 0) {
return NULL;
}
Connection * c = newConnection(w_fd, r_fd);
write_msg(c->w_fd,NULL,ACK_CONN_PCK,0);
free(msg);
free(buf);
return c;
}
static int sel(int fd, struct timeval * timeout)
{
fd_set r_set;
int ret;
FD_ZERO(&r_set);
FD_SET(fd, &r_set);
ret = select(fd + 1, &r_set, NULL, NULL, timeout);
if (ret == 0) {
return 0;
}
if (ret > 0) {
return FD_ISSET(fd, &r_set);
}
return -1;
}
int mm_select(Connection * c, struct timeval * timeout)
{
return sel(c->r_fd, timeout);
}
int mm_select_accept(Listener * lp, struct timeval * timeout)
{
return sel(lp->l_fd, timeout);
}
int64_t mm_read(Connection * c, int8_t buf[], int64_t size)
{
int64_t len, total_len = 0, r_len;
Message m;
if ((len = read(c->r_fd,(char *) &m,sizeof(Message))) == 0) {
return 0;
}
if (len < 0 || size < m.size) {
return -1;
}
while (total_len < m.size) {
r_len = read(c->r_fd, &buf[total_len], m.size - total_len);
if (r_len == 0) {
return 0;
}
if (r_len < 0) {
return -1;
}
total_len += r_len;
}
/*
if(m.type == DELETE_CONN_PCK){
int o_pid=atoi(buf);
int pid = getpid();
char r_addr[20], w_addr[20];
sprintf(r_addr,"/tmp/r%d",o_pid);
sprintf(w_addr,"/tmp/w%d",o_pid);
remove(r_addr);
remove(w_addr);
return -1;
}
else if (m.type != NORMAL_PCK){
return -1;
}*/
return m.size;
}
static Connection * newConnection(int w,int r){
Connection * ans = malloc(sizeof(Connection));
if (ans == NULL) {
return NULL;
}
ans->w_fd=w;
ans->r_fd=r;
return ans;
}
static Listener * newListener(int fd,char * p){
Listener * ans = malloc(sizeof(Listener));
ans->l_fd=fd;
ans->addr=p;
return ans;
}
int mm_commtype(){
return COMM_FIFO;
}