xref: /netbsd/usr.sbin/mopd/common/get.c (revision bf9ec67e)
1 /*	$NetBSD: get.c,v 1.3 1997/10/16 23:24:38 lukem 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 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: get.c,v 1.3 1997/10/16 23:24:38 lukem Exp $");
35 #endif
36 
37 #include "os.h"
38 #include "get.h"
39 #include "mopdef.h"
40 
41 u_char
42 mopGetChar(pkt, index)
43 	u_char *pkt;
44 	int    *index;
45 {
46         u_char ret;
47 
48 	ret = pkt[*index];
49 	*index = *index + 1;
50 	return(ret);
51 }
52 
53 u_short
54 mopGetShort(pkt, index)
55 	u_char *pkt;
56 	int    *index;
57 {
58         u_short ret;
59 
60 	ret = pkt[*index] + pkt[*index+1]*256;
61 	*index = *index + 2;
62 	return(ret);
63 }
64 
65 u_int32_t
66 mopGetLong(pkt, index)
67 	u_char *pkt;
68 	int    *index;
69 {
70         u_int32_t ret;
71 
72 	ret = pkt[*index] +
73 	      pkt[*index+1]*0x100 +
74 	      pkt[*index+2]*0x10000 +
75 	      pkt[*index+3]*0x1000000;
76 	*index = *index + 4;
77 	return(ret);
78 }
79 
80 void
81 mopGetMulti(pkt, index, dest, size)
82 	u_char *pkt,*dest;
83 	int    *index,size;
84 {
85 	int i;
86 
87 	for (i = 0; i < size; i++) {
88 	  dest[i] = pkt[*index+i];
89 	}
90 	*index = *index + size;
91 
92 }
93 
94 int
95 mopGetTrans(pkt, trans)
96 	u_char	*pkt;
97 	int	 trans;
98 {
99 	u_short	*ptype;
100 
101 	if (trans == 0) {
102 		ptype = (u_short *)(pkt+12);
103 		if (ntohs(*ptype) < 1600) {
104 			trans = TRANS_8023;
105 		} else {
106 			trans = TRANS_ETHER;
107 		}
108 	}
109 	return(trans);
110 }
111 
112 void
113 mopGetHeader(pkt, index, dst, src, proto, len, trans)
114 	u_char	*pkt, **dst, **src;
115 	int	*index, *len, trans;
116 	u_short	*proto;
117 {
118 	*dst = pkt;
119 	*src = pkt + 6;
120 	*index = *index + 12;
121 
122 	switch(trans) {
123 	case TRANS_ETHER:
124 		*proto = (u_short)(pkt[*index]*256 + pkt[*index+1]);
125 		*index = *index + 2;
126 		*len   = (int)(pkt[*index+1]*256 + pkt[*index]);
127 		*index = *index + 2;
128 		break;
129 	case TRANS_8023:
130 		*len   = (int)(pkt[*index]*256 + pkt[*index+1]);
131 		*index = *index + 8;
132 		*proto = (u_short)(pkt[*index]*256 + pkt[*index+1]);
133 		*index = *index + 2;
134 		break;
135 	}
136 }
137 
138 u_short
139 mopGetLength(pkt, trans)
140 	u_char	*pkt;
141 	int	 trans;
142 {
143 	switch(trans) {
144 	case TRANS_ETHER:
145 		return(pkt[15]*256 + pkt[14]);
146 		break;
147 	case TRANS_8023:
148 		return(pkt[12]*256 + pkt[13]);
149 		break;
150 	}
151 	return(0);
152 }
153