socket.c
9.61 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*************************************************************************
*
* File: socket.c
*
* These routines form a simple IPC mechanism to communicate between Verilog
* and C programs. This file is compiled in this directory with PLI defined
* for interfacing with the Verilog files and re-compiled in the 'src'
* directory for interfacing with the C-based I/O test driver.
*
* $Revision: 1.1 $
*
*/
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
/*XXX#include <bstring.h>*/
#include "simipc.h"
#include "trace.h"
#ifdef PLI
#include "acc_user.h"
#include "vcsuser.h"
static s_setval_value value_s = { accIntVal };
static s_setval_delay delay_s = { { accRealTime, 0, 0, 0 }, accNoDelay };
#endif /* PLI */
#ifndef PLI
#define acc_initialize()
#define acc_configure(a, b)
#define acc_set_value(a, b, c)
#define tf_putp(a,b) return(b)
extern FILE *LogFp;
#else /* !PLI */
unsigned long Dflags = 0;
#ifdef IRIX
FILE *LogFp = stderr;
#else /* linux */
#define LogFp stderr
#endif /* IRIX */
#endif /* PLI */
char IpcName[256];
static IpcPkt pkt;
/*
* int IpcInit(char *name)
*/
#ifdef PLI
void
IpcInit(void)
{
char *name;
#else
int
IpcInit(char *name)
{
#endif
acc_initialize();
acc_configure(accDevelopmentVersion, "1.6");
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
name = (char *)tf_getcstringp(1);
#endif
if ((name == NULL) || (strcmp(name, "NULL") == 0)) {
sprintf(IpcName, DEFAULT_NAME_PATH, getenv("USER"));
}
else
sprintf(IpcName, "/tmp/%s", name);
_TRACE(DALL, fprintf(LogFp, "IpcInit: ipc_name=%s\n", IpcName));
tf_putp(0, 0);
} /* IpcInit */
int
IpcReceive(int fd, char *buf, int nbytes)
{
int nleft, nread;
_TRACE(DREAD, fprintf(LogFp,
"IpcReceive: fd=%d, buf=0x%08x, nbytes=%d\n", fd, buf, nbytes));
bzero(buf, nbytes);
nleft = nbytes;
while (nleft > 0) {
nread = recv(fd, buf, nleft, 0);
if (nread < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcReceive: recv failed, errno=%d\n", errno));
perror("recv failed: ");
return(-1);
}
if (nread == 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcReceive: nread == 0!\n"));
break;
}
nleft -= nread;
buf += nread;
}
_TRACE(DREAD, fprintf(LogFp,
"IpcReceive: return=%d, nbytes=%d, nleft=%d\n", nbytes - nleft,
nbytes, nleft));
return( (nleft > 0) ? -1 : 0);
} /* IpcReceive */
int
IpcSend(int fd, char *buf, int nbytes)
{
int nleft, nwritten;
int ret;
_TRACE(DWRITE, fprintf(LogFp,"ipcWrite: fd=%d, buf=0x%08x, nbytes=%d\n",
fd, buf, nbytes));
nleft = nbytes;
while (nleft > 0) {
nwritten = send(fd, buf, nleft, 0);
if (nwritten <= 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcSend: send failed, errno=%d\n", errno));
perror("send failed: ");
return(-1);
}
nleft -= nwritten;
buf += nwritten;
}
_TRACE(DWRITE, fprintf(LogFp,
"ipcWrite: return=%d, nbytes=%d, nleft=%d\n", nbytes - nleft,
nbytes, nleft));
return( (nleft > 0) ? -1 : 0);
} /* IpcSend */
/*
* int IpcOpen(int type)
*/
#ifdef PLI
void
IpcOpen(void)
{
int type;
#else
int
IpcOpen(int type)
{
#endif
int fd;
struct sockaddr_un addr;
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
type = (int)tf_getp(1);
#endif
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: socket failed, errno=%d\n", errno));
perror("opening stream socket");
tf_putp(0, -1);
}
if (type == IPC_SERVER) {
/*
* Arrange to have ourselves die when parent dies
*/
#ifdef IRIX
if (prctl(PR_TERMCHILD) == -1) {
#else /* linux */
if (prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) {
#endif
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: prctl failed, errno=%d\n", errno));
perror("prctl(PR_TERMCHILD)");
tf_putp(0, -1);
}
/*
* Unlink UNIX socket streams path if exists
*/
unlink(IpcName);
strcpy(addr.sun_path, IpcName);
addr.sun_family = AF_UNIX;
/* Bind the name to the socket */
if (bind(fd, (struct sockaddr *)&addr,
strlen(addr.sun_path) + sizeof(addr.sun_family)) < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: bind failed, errno=%d\n", errno));
perror("binding stream socket");
tf_putp(0, -1);
}
/* Listen on the socket */
if (listen(fd, 5) == -1) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: listen failed, errno=%d\n", errno));
perror("listening on stream socket");
tf_putp(0, -1);
}
}
tf_putp(0, fd);
} /* IpcOpen */
/*
* int IpcAccept(int fd)
*/
#ifdef PLI
void
IpcAccept(void)
{
int fd;
#else
int
IpcAccept(int fd)
{
#endif
int newfd;
struct sockaddr_un from;
int fromlen = sizeof (from);
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
#endif
newfd = accept(fd, (struct sockaddr *)&from, &fromlen);
if (newfd < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcAccept: accept failed, errno=%d\n", errno));
/*
perror("accepting stream socket");
*/
tf_putp(0, -1);
}
else {
tf_putp(0, newfd);
}
} /* IpcAccept */
/*
* int IpcConnect(int fd)
*/
#ifdef PLI
void
IpcConnect(void)
{
int fd;
#else
int
IpcConnect(int fd)
{
#endif
struct sockaddr_un addr;
int addrlen = sizeof (addr);
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
#endif
strcpy(addr.sun_path, IpcName);
addr.sun_family = AF_UNIX;
if (connect(fd, (struct sockaddr *)&addr, addrlen) < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcConnect: connect failed, errno=%d\n", errno));
/*
perror("connecting stream socket");
*/
tf_putp(0, -1);
}
else {
tf_putp(0, 0);
}
} /* IpcConnect */
/*
* int IpcClose(int fd)
*/
#ifdef PLI
void
IpcClose(void)
{
int fd;
#else
int
IpcClose(int fd)
{
#endif
#ifdef PLI
if (tf_nump() != 1) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
#endif
close(fd);
tf_putp(0, 0);
} /* IpcClose */
#ifdef PLI
/*
* int IpcReceivePli(int fd, int *code, int *size, int *address,
* int *data0, int *data1, int *data2, int *data3,
* int *data4, int *data5, int *data6, int *data7)
*/
void
IpcReceivePli(void)
{
int fd;
unsigned int buf[10];
handle hcode, hsize, haddr,
hdata0, hdata1, hdata2, hdata3,
hdata4, hdata5, hdata6, hdata7;
if (tf_nump() != 12) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
fd = (int)tf_getp(1);
hcode = acc_handle_tfarg(2);
hsize = acc_handle_tfarg(3);
haddr = acc_handle_tfarg(4);
hdata0 = acc_handle_tfarg(5);
hdata1 = acc_handle_tfarg(6);
hdata2 = acc_handle_tfarg(7);
hdata3 = acc_handle_tfarg(8);
hdata4 = acc_handle_tfarg(9);
hdata5 = acc_handle_tfarg(10);
hdata6 = acc_handle_tfarg(11);
hdata7 = acc_handle_tfarg(12);
if (IpcReceive(fd, (char *)buf, sizeof(IpcPkt)) < 0) {
tf_error("IpcReceivePli: IpcReceive failed");
tf_putp(0, -1);
}
else {
value_s.value.integer = (int)buf[PKT_CODE_OFS];
acc_set_value(hcode, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_SIZE_OFS];
acc_set_value(hsize, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_ADDR_OFS];
acc_set_value(haddr, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS];
acc_set_value(hdata0, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS+1];
acc_set_value(hdata1, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS+2];
acc_set_value(hdata2, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS+3];
acc_set_value(hdata3, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS+4];
acc_set_value(hdata4, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS+5];
acc_set_value(hdata5, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS+6];
acc_set_value(hdata6, &value_s, &delay_s);
value_s.value.integer = (int)buf[PKT_DATA_OFS+7];
acc_set_value(hdata7, &value_s, &delay_s);
tf_putp(0, 0);
}
} /* IpcReceivePli */
/*
* int IpcSendPli(int fd, int code, int size, int address,
* int data0, int data1, int data2, int data3)
* int data4, int data5, int data6, int data7)
*/
void
IpcSendPli(void)
{
#define MSG_1 "\t\tdata:0x%08x, 0x%08x, 0x%08x, 0x%08x, \
0x%08x, 0x%08x, 0x%08x, 0x%08x\n"
int fd;
IpcPkt pkt;
int ret;
if (tf_nump() != 12) {
tf_error("illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
bzero(&pkt, sizeof(IpcPkt));
pkt.length = sizeof(IpcPkt);
fd = (int)tf_getp(1);
pkt.code = (int)tf_getp(2);
pkt.size = (int)tf_getp(3);
pkt.address = (int)tf_getp(4);
pkt.data[0] = (int)tf_getp(5);
pkt.data[1] = (int)tf_getp(6);
pkt.data[2] = (int)tf_getp(7);
pkt.data[3] = (int)tf_getp(8);
pkt.data[4] = (int)tf_getp(9);
pkt.data[5] = (int)tf_getp(10);
pkt.data[6] = (int)tf_getp(11);
pkt.data[7] = (int)tf_getp(12);
_TRACE(DWRITE, fprintf(LogFp, "IpcSendPli: fd=%d, len=%d\n",
fd, pkt.length));
_TRACE(DWRITE, fprintf(LogFp,
"IpcSendPli: pkt: code=%d, size=%d, addr=0x%08x\n",
pkt.code, pkt.size, pkt.address));
_TRACE(DWRITE, fprintf(LogFp, MSG_1,
pkt.data[0], pkt.data[1], pkt.data[2], pkt.data[3],
pkt.data[4], pkt.data[5], pkt.data[6], pkt.data[7]));
ret = IpcSend(fd, (char *)&pkt, sizeof(IpcPkt));
if (ret < 0)
tf_error("IpcSendPli: IpcSend failed");
tf_putp(0, ret);
} /* IpcSendPli */
#endif /* PLI */