File: dis_relay.c

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (154 lines) | stat: -rwxr-xr-x 4,152 bytes parent folder | download | duplicates (9)
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
/*
 *   DIS/x : An implementation of the IEEE 1278.1 protocol
 *
 *   Copyright (C) 1996, Riley Rainey (rainey@netcom.com)
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of either:
 *
 *   a) the GNU Library General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your
 *   option) any later version.  A description of the terms and conditions
 *   of the GLPL may be found in the "COPYING.LIB" file.
 *
 *   b) the "Artistic License" which comes with this Kit.  Information
 *   about this license may be found in the "Artistic" file.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Library General Public License or the Artistic License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   Information describing how to contact the author can be found in the
 *   README file.
 */
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <dis/dis.h>
#include <sys/uio.h>
#include <sys/socket.h>
#define BSD_COMP		/* keeps Solaris happy */
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <dis_relay.h>

SwitchClient *client_list = 0;

#if SYSCALL_PROTO
extern int recvmsg(int, struct msghdr *, int);
extern int sendmsg(int, struct msghdr *, int);
extern int socket(int domain, int type, int protocol);
extern int setsockopt(int s, int level, int optname, char *optval, int optlen);
extern int bind(int s, struct sockaddr *name, int namelen);
extern int ioctl(int, int,...);
extern void perror(const char *);
extern void bcopy(const void *, void *, int);
#endif

int
ReadPDUFrame(DISTransceiver * xcvr, char *pdu, int pdu_max, struct sockaddr *from)
{
    int size;
    struct msghdr msg;
    struct iovec vec;

    msg.msg_name = (caddr_t) from;
    msg.msg_namelen = sizeof(struct sockaddr);
    msg.msg_iov = &vec;
    msg.msg_iovlen = 1;
#ifdef HAVE_MSG_CONTROL
    msg.msg_control = (caddr_t) NULL;
    msg.msg_controllen = 0;
#endif    
#ifdef HAVE_MSG_ACCRIGHTS
    msg.msg_accrights = (caddr_t) NULL;
    msg.msg_accrightslen = 0;
#endif    
    vec.iov_base = (caddr_t) pdu;
    vec.iov_len = pdu_max;

    size = recvmsg(xcvr->s, &msg, 0);

    return size;
}

int
WritePDU(DISTransceiver * xcvr, char *buffer, int length, struct sockaddr *to)
{
    char *p;
    struct msghdr msg;
    struct iovec vec;
    int i, result;


    msg.msg_namelen = sizeof(struct sockaddr);
    msg.msg_iov = &vec;
    msg.msg_iovlen = 1;
#ifdef HAVE_MSG_CONTROL
    msg.msg_control = (caddr_t) NULL;
    msg.msg_controllen = 0;
#endif    
#ifdef HAVE_MSG_ACCRIGHTS
    msg.msg_accrights = (caddr_t) NULL;
    msg.msg_accrightslen = 0;
#endif    
    vec.iov_base = (caddr_t) & buffer;
    vec.iov_len = length;

    msg.msg_name = (caddr_t) to;
    if ((result = sendmsg(xcvr->s, &msg, 0)) == -1) {
	perror("on sendmsg");
    }

    return 0;
}

void
RelayPDUFrame(DISTransceiver * xcvr, char *buffer, int size, struct sockaddr *from)
{
    SwitchClient *p, *new;
    int found = 0;

    for (p = client_list; p; p=p->next) {
	if (memcmp(from, &p->addr, sizeof(struct sockaddr)) == 0) {
	    found = 1;
	    p->frame_count += 1;
	}
	else {
	    WritePDU(xcvr, buffer, size, &p->addr);
	}
    }
    if (found == 0) {
	if ((new = malloc(sizeof(SwitchClient)))) {
	    new->frame_count = 1;
	    memcpy(&new->addr, from, sizeof(struct sockaddr));
	    new->next = client_list;
	    client_list = new;
	}
	else {
	    fprintf(stderr, "memory allocation error\n");
	    exit(1);
	}
    }
}

int
main(int argc, char **argv)
{
    DISTransceiver *xcvr;
    struct sockaddr from;
    int size;
    char buf[2048];

    xcvr = DISOpenTransceiver(-1);
    while (1) {
	if ((size = ReadPDUFrame(xcvr, buf, sizeof(buf), &from)) > 0) {
	    RelayPDUFrame(xcvr, buf, size, &from);
	}
    }
}