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