get.c revision 1.8
1/*	$OpenBSD: get.c,v 1.8 2009/10/27 23:59:52 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1993-2006 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/types.h>
28#include "common/mopdef.h"
29
30u_char
31mopGetChar(u_char *pkt, int *idx)
32{
33	u_char ret;
34
35	ret = pkt[*idx];
36	*idx = *idx + 1;
37	return (ret);
38}
39
40u_short
41mopGetShort(u_char *pkt, int *idx)
42{
43	u_short ret;
44
45	ret = pkt[*idx] + pkt[*idx+1]*256;
46	*idx = *idx + 2;
47	return (ret);
48}
49
50u_short
51mopGetNShort(u_char *pkt, int *idx)
52{
53	u_short ret;
54
55	ret = pkt[*idx]*256 + pkt[*idx+1];
56	*idx = *idx + 2;
57	return (ret);
58}
59
60u_long
61mopGetLong(u_char *pkt, int *idx)
62{
63	u_long ret;
64
65	ret = pkt[*idx] + pkt[*idx+1]*0x100 + pkt[*idx+2]*0x10000 +
66	    pkt[*idx+3]*0x1000000;
67	*idx = *idx + 4;
68	return (ret);
69}
70
71void
72mopGetMulti(u_char *pkt, int *idx, u_char *dest, int size)
73{
74	int i;
75
76	for (i = 0; i < size; i++)
77		dest[i] = pkt[*idx+i];
78	*idx = *idx + size;
79}
80
81int
82mopGetTrans(u_char *pkt, int trans)
83{
84	u_short	ptype;
85
86	if (trans == 0) {
87		ptype = pkt[12]*256 + pkt[13];
88		if (ptype < 1600)
89			trans = TRANS_8023;
90		else
91			trans = TRANS_ETHER;
92	}
93	return (trans);
94}
95
96void
97mopGetHeader(u_char *pkt, int *idx, u_char **dst, u_char **src,
98    u_short *proto, int *len, int trans)
99{
100	*dst = pkt;
101	*src = pkt + 6;
102	*idx = *idx + 12;
103
104	switch (trans) {
105	case TRANS_ETHER:
106		*proto = mopGetNShort(pkt, idx);
107		*len   = (int)mopGetShort(pkt, idx);
108		break;
109	case TRANS_8023:
110		*len   = (int)mopGetNShort(pkt, idx);
111		*idx   = *idx + 6;
112		*proto = mopGetNShort(pkt, idx);
113		break;
114	}
115}
116
117u_short
118mopGetLength(u_char *pkt, int trans)
119{
120	switch (trans) {
121	case TRANS_ETHER:
122		return (pkt[14] + pkt[15]*256);
123	case TRANS_8023:
124		return (pkt[12]*256 + pkt[13]);
125	}
126	return (0);
127}
128