xref: /openbsd-src/usr.sbin/mopd/common/get.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: get.c,v 1.2 1996/09/21 19:11:35 maja 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
33 static char rcsid[] = "$OpenBSD: get.c,v 1.2 1996/09/21 19:11:35 maja Exp $";
34 #endif
35 
36 #include <sys/types.h>
37 #include "common/mopdef.h"
38 
39 u_char
40 mopGetChar(pkt, index)
41 	register u_char *pkt;
42 	register int    *index;
43 {
44         u_char ret;
45 
46 	ret = pkt[*index];
47 	*index = *index + 1;
48 	return(ret);
49 }
50 
51 u_short
52 mopGetShort(pkt, index)
53 	register u_char *pkt;
54 	register 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 
63 u_long
64 mopGetLong(pkt, index)
65 	register u_char *pkt;
66 	register 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 
78 void
79 mopGetMulti(pkt, index, dest, size)
80 	register u_char *pkt,*dest;
81 	register 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 
92 int
93 mopGetTrans(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 
110 void
111 mopGetHeader(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 
136 u_short
137 mopGetLength(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