get.c revision 1.1
1/*
2 * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 *    must display the following acknowledgement:
14 *	This product includes software developed by Mats O Jansson.
15 * 4. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef LINT
31static char rcsid[] = "$Id: get.c,v 1.1 1996/09/21 13:49:16 maja Exp $";
32#endif
33
34#include <sys/types.h>
35#include "common/mopdef.h"
36
37u_char
38mopGetChar(pkt, index)
39	register u_char *pkt;
40	register int    *index;
41{
42        u_char ret;
43
44	ret = pkt[*index];
45	*index = *index + 1;
46	return(ret);
47}
48
49u_short
50mopGetShort(pkt, index)
51	register u_char *pkt;
52	register int    *index;
53{
54        u_short ret;
55
56	ret = pkt[*index] + pkt[*index+1]*256;
57	*index = *index + 2;
58	return(ret);
59}
60
61u_long
62mopGetLong(pkt, index)
63	register u_char *pkt;
64	register int    *index;
65{
66        u_long ret;
67
68	ret = pkt[*index] +
69	      pkt[*index+1]*0x100 +
70	      pkt[*index+2]*0x10000 +
71	      pkt[*index+3]*0x1000000;
72	*index = *index + 4;
73	return(ret);
74}
75
76void
77mopGetMulti(pkt, index, dest, size)
78	register u_char *pkt,*dest;
79	register int    *index,size;
80{
81	int i;
82
83	for (i = 0; i < size; i++) {
84	  dest[i] = pkt[*index+i];
85	}
86	*index = *index + size;
87
88}
89
90int
91mopGetTrans(pkt, trans)
92	u_char	*pkt;
93	int	 trans;
94{
95	u_short	*ptype;
96
97	if (trans == 0) {
98		ptype = (u_short *)(pkt+12);
99		if (ntohs(*ptype) < 1600) {
100			trans = TRANS_8023;
101		} else {
102			trans = TRANS_ETHER;
103		}
104	}
105	return(trans);
106}
107
108void
109mopGetHeader(pkt, index, dst, src, proto, len, trans)
110	u_char	*pkt, **dst, **src;
111	int	*index, *len, trans;
112	u_short	*proto;
113{
114	*dst = pkt;
115	*src = pkt + 6;
116	*index = *index + 12;
117
118	switch(trans) {
119	case TRANS_ETHER:
120		*proto = (u_short)(pkt[*index]*256 + pkt[*index+1]);
121		*index = *index + 2;
122		*len   = (int)(pkt[*index+1]*256 + pkt[*index]);
123		*index = *index + 2;
124		break;
125	case TRANS_8023:
126		*len   = (int)(pkt[*index]*256 + pkt[*index+1]);
127		*index = *index + 8;
128		*proto = (u_short)(pkt[*index]*256 + pkt[*index+1]);
129		*index = *index + 2;
130		break;
131	}
132}
133
134u_short
135mopGetLength(pkt, trans)
136	u_char	*pkt;
137	int	 trans;
138{
139	switch(trans) {
140	case TRANS_ETHER:
141		return(pkt[15]*256 + pkt[14]);
142		break;
143	case TRANS_8023:
144		return(pkt[12]*256 + pkt[13]);
145		break;
146	}
147	return(0);
148}
149