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