socket.c
13 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*************************************************************************
*
* 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.2 $
*
*/
#include <sys/types.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>
#ifdef __sgi__
#include <bstring.h>
#else
#include <netinet/in.h>
#endif
#include "trace.h"
#include "simipc.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;
FILE *LogFp = stderr;
#endif /* PLI */
char IpcName[256];
static IpcPkt pkt;
#ifndef __sgi__
char *iosim_server;
int iosim_port;
#endif
/*
* 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));
#ifndef __sgi__
{
char *sport;
iosim_server = getenv("IOSIM_SERVER");
sport = getenv("IOSIM_PORT");
if (iosim_server == NULL || sport == NULL) {
fprintf(stderr, "Fatal error: please set environment variable: \n");
fprintf(stderr, " IOSIM_SERVER and IOSIM_PORT\n");
tf_putp(0, -1);
}
iosim_port = strtoul(sport, NULL, 0);
fprintf(stderr, "IOSIM server=%s port=%d\n", iosim_server, iosim_port);
}
#endif
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: ");
/* Here, we exit since it's most likely that the server
* is down
*/
exit(1);
}
#ifdef __sgi__
if (nread == 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcReceive: nread == 0!\n"));
break;
}
#endif
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: ");
/* Here, we exit since it's most likely that the server
* is down
*/
exit(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;
#ifdef __sgi__
struct sockaddr_un addr;
#else
struct sockaddr_in addr;
#endif
#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
#ifdef __sgi__
fd = socket(AF_UNIX, SOCK_STREAM, 0);
#else
fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
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) {
/*
* Unlink UNIX socket streams path if exists
*/
unlink(IpcName);
#ifdef __sgi__
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);
}
#else
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(iosim_port);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
_TRACE(DERROR, fprintf(LogFp,
"IpcOpen: bind failed, errno=%d\n", errno));
perror("binding stream socket");
tf_putp(0, -1);
}
#endif
/* 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;
#ifdef __sgi__
struct sockaddr_un from;
#else
struct sockaddr_in from;
#endif
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
#ifdef __sgi__
struct sockaddr_un addr;
#else
struct sockaddr_in addr;
#endif
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
#ifdef __sgi__
strcpy(addr.sun_path, IpcName);
addr.sun_family = AF_UNIX;
#else
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(iosim_server);
addr.sin_port = htons(iosim_port);
#endif
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;
#ifdef __sgi__
unsigned int buf[10];
#else
unsigned int buf[12];
#endif
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, %d", fd);
tf_putp(0, -1);
}
else {
#ifndef __sgi__
value_s.value.integer = ntohl((int)buf[PKT_CODE_OFS]);
acc_set_value(hcode, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_SIZE_OFS]);
acc_set_value(hsize, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_ADDR_OFS]);
acc_set_value(haddr, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS]);
acc_set_value(hdata0, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+1]);
acc_set_value(hdata1, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+2]);
acc_set_value(hdata2, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+3]);
acc_set_value(hdata3, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+4]);
acc_set_value(hdata4, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+5]);
acc_set_value(hdata5, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+6]);
acc_set_value(hdata6, &value_s, &delay_s);
value_s.value.integer = ntohl((int)buf[PKT_DATA_OFS+7]);
acc_set_value(hdata7, &value_s, &delay_s);
#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);
#endif
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);
#ifndef __sgi__
pkt.code = htonl((int)tf_getp(2));
pkt.size = htonl((int)tf_getp(3));
pkt.address = htonl((int)tf_getp(4));
pkt.data[0] = htonl((int)tf_getp(5));
pkt.data[1] = htonl((int)tf_getp(6));
pkt.data[2] = htonl((int)tf_getp(7));
pkt.data[3] = htonl((int)tf_getp(8));
pkt.data[4] = htonl((int)tf_getp(9));
pkt.data[5] = htonl((int)tf_getp(10));
pkt.data[6] = htonl((int)tf_getp(11));
pkt.data[7] = htonl((int)tf_getp(12));
#else
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);
#endif
_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 */