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