xref: /openbsd-src/usr.sbin/mopd/common/get.c (revision 043fbe51c197dbbcd422e917b65f765d8b5f8874)
1*043fbe51Sderaadt /*	$OpenBSD: get.c,v 1.8 2009/10/27 23:59:52 deraadt Exp $ */
2e08c8b61Smaja 
3bff609efSmaja /*
43cd9cd14Smaja  * Copyright (c) 1993-2006 Mats O Jansson.  All rights reserved.
5bff609efSmaja  *
6bff609efSmaja  * Redistribution and use in source and binary forms, with or without
7bff609efSmaja  * modification, are permitted provided that the following conditions
8bff609efSmaja  * are met:
9bff609efSmaja  * 1. Redistributions of source code must retain the above copyright
10bff609efSmaja  *    notice, this list of conditions and the following disclaimer.
11bff609efSmaja  * 2. Redistributions in binary form must reproduce the above copyright
12bff609efSmaja  *    notice, this list of conditions and the following disclaimer in the
13bff609efSmaja  *    documentation and/or other materials provided with the distribution.
14bff609efSmaja  *
15bff609efSmaja  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16bff609efSmaja  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17bff609efSmaja  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18bff609efSmaja  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19bff609efSmaja  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20bff609efSmaja  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21bff609efSmaja  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22bff609efSmaja  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23bff609efSmaja  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24bff609efSmaja  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25bff609efSmaja  */
26bff609efSmaja 
27bff609efSmaja #include <sys/types.h>
28bff609efSmaja #include "common/mopdef.h"
29bff609efSmaja 
30bff609efSmaja u_char
mopGetChar(u_char * pkt,int * idx)313cd9cd14Smaja mopGetChar(u_char *pkt, int *idx)
32bff609efSmaja {
33bff609efSmaja 	u_char ret;
34bff609efSmaja 
353cd9cd14Smaja 	ret = pkt[*idx];
363cd9cd14Smaja 	*idx = *idx + 1;
37bff609efSmaja 	return (ret);
38bff609efSmaja }
39bff609efSmaja 
40bff609efSmaja u_short
mopGetShort(u_char * pkt,int * idx)413cd9cd14Smaja mopGetShort(u_char *pkt, int *idx)
42bff609efSmaja {
43bff609efSmaja 	u_short ret;
44bff609efSmaja 
453cd9cd14Smaja 	ret = pkt[*idx] + pkt[*idx+1]*256;
463cd9cd14Smaja 	*idx = *idx + 2;
473cd9cd14Smaja 	return (ret);
483cd9cd14Smaja }
493cd9cd14Smaja 
503cd9cd14Smaja u_short
mopGetNShort(u_char * pkt,int * idx)513cd9cd14Smaja mopGetNShort(u_char *pkt, int *idx)
523cd9cd14Smaja {
533cd9cd14Smaja 	u_short ret;
543cd9cd14Smaja 
553cd9cd14Smaja 	ret = pkt[*idx]*256 + pkt[*idx+1];
563cd9cd14Smaja 	*idx = *idx + 2;
57bff609efSmaja 	return (ret);
58bff609efSmaja }
59bff609efSmaja 
60bff609efSmaja u_long
mopGetLong(u_char * pkt,int * idx)613cd9cd14Smaja mopGetLong(u_char *pkt, int *idx)
62bff609efSmaja {
63bff609efSmaja 	u_long ret;
64bff609efSmaja 
653cd9cd14Smaja 	ret = pkt[*idx] + pkt[*idx+1]*0x100 + pkt[*idx+2]*0x10000 +
663cd9cd14Smaja 	    pkt[*idx+3]*0x1000000;
673cd9cd14Smaja 	*idx = *idx + 4;
68bff609efSmaja 	return (ret);
69bff609efSmaja }
70bff609efSmaja 
71bff609efSmaja void
mopGetMulti(u_char * pkt,int * idx,u_char * dest,int size)723cd9cd14Smaja mopGetMulti(u_char *pkt, int *idx, u_char *dest, int size)
73bff609efSmaja {
74bff609efSmaja 	int i;
75bff609efSmaja 
7627ce7e35Shenning 	for (i = 0; i < size; i++)
773cd9cd14Smaja 		dest[i] = pkt[*idx+i];
783cd9cd14Smaja 	*idx = *idx + size;
79bff609efSmaja }
80bff609efSmaja 
81bff609efSmaja int
mopGetTrans(u_char * pkt,int trans)8227ce7e35Shenning mopGetTrans(u_char *pkt, int trans)
83bff609efSmaja {
843cd9cd14Smaja 	u_short	ptype;
85bff609efSmaja 
86bff609efSmaja 	if (trans == 0) {
873cd9cd14Smaja 		ptype = pkt[12]*256 + pkt[13];
883cd9cd14Smaja 		if (ptype < 1600)
89bff609efSmaja 			trans = TRANS_8023;
9027ce7e35Shenning 		else
91bff609efSmaja 			trans = TRANS_ETHER;
92bff609efSmaja 	}
93bff609efSmaja 	return (trans);
94bff609efSmaja }
95bff609efSmaja 
96bff609efSmaja void
mopGetHeader(u_char * pkt,int * idx,u_char ** dst,u_char ** src,u_short * proto,int * len,int trans)973cd9cd14Smaja mopGetHeader(u_char *pkt, int *idx, u_char **dst, u_char **src,
9827ce7e35Shenning     u_short *proto, int *len, int trans)
99bff609efSmaja {
100bff609efSmaja 	*dst = pkt;
101bff609efSmaja 	*src = pkt + 6;
1023cd9cd14Smaja 	*idx = *idx + 12;
103bff609efSmaja 
104bff609efSmaja 	switch (trans) {
105bff609efSmaja 	case TRANS_ETHER:
1063cd9cd14Smaja 		*proto = mopGetNShort(pkt, idx);
1073cd9cd14Smaja 		*len   = (int)mopGetShort(pkt, idx);
108bff609efSmaja 		break;
109bff609efSmaja 	case TRANS_8023:
1103cd9cd14Smaja 		*len   = (int)mopGetNShort(pkt, idx);
1113cd9cd14Smaja 		*idx   = *idx + 6;
1123cd9cd14Smaja 		*proto = mopGetNShort(pkt, idx);
113bff609efSmaja 		break;
114bff609efSmaja 	}
115bff609efSmaja }
116bff609efSmaja 
117bff609efSmaja u_short
mopGetLength(u_char * pkt,int trans)11827ce7e35Shenning mopGetLength(u_char *pkt, int trans)
119bff609efSmaja {
120bff609efSmaja 	switch (trans) {
121bff609efSmaja 	case TRANS_ETHER:
1223cd9cd14Smaja 		return (pkt[14] + pkt[15]*256);
123bff609efSmaja 	case TRANS_8023:
124bff609efSmaja 		return (pkt[12]*256 + pkt[13]);
125bff609efSmaja 	}
126bff609efSmaja 	return (0);
127bff609efSmaja }
128