Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: get.c,v 1.8 2024/12/03 05:57:02 kalvisd 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 #if defined (HAVE_NBTOOL_CONFIG_H)
     28 # include "nbtool_config.h"
     29 #else
     30 # include "port.h"
     31 #endif /* defined (HAVE_NBTOOL_CONFIG_H) */
     32 #ifndef lint
     33 __RCSID("$NetBSD: get.c,v 1.8 2024/12/03 05:57:02 kalvisd Exp $");
     34 #endif
     35 
     36 #include "os.h"
     37 #include "get.h"
     38 #include "mopdef.h"
     39 
     40 u_char
     41 mopGetChar(const u_char *pkt, int *idx)
     42 {
     43         u_char ret;
     44 
     45 	ret = pkt[*idx];
     46 	*idx = *idx + 1;
     47 	return(ret);
     48 }
     49 
     50 u_short
     51 mopGetShort(const u_char *pkt, int *idx)
     52 {
     53         u_short ret;
     54 
     55 	ret = pkt[*idx] + pkt[*idx+1]*256;
     56 	*idx = *idx + 2;
     57 	return(ret);
     58 }
     59 
     60 u_int32_t
     61 mopGetLong(const u_char *pkt, int *idx)
     62 {
     63         u_int32_t ret;
     64 
     65 	ret = pkt[*idx] +
     66 	      pkt[*idx+1]*0x100 +
     67 	      pkt[*idx+2]*0x10000 +
     68 	      pkt[*idx+3]*0x1000000;
     69 	*idx = *idx + 4;
     70 	return(ret);
     71 }
     72 
     73 void
     74 mopGetMulti(const u_char *pkt, int *idx, u_char *dest, int size)
     75 {
     76 	int i;
     77 
     78 	for (i = 0; i < size; i++) {
     79 	  dest[i] = pkt[*idx+i];
     80 	}
     81 	*idx = *idx + size;
     82 
     83 }
     84 
     85 int
     86 mopGetTrans(const u_char *pkt, int trans)
     87 {
     88 	const u_short	*ptype;
     89 
     90 	if (trans == 0) {
     91 		ptype = (const u_short *)(pkt+12);
     92 		if (ntohs(*ptype) < 1600) {
     93 			trans = TRANS_8023;
     94 		} else {
     95 			trans = TRANS_ETHER;
     96 		}
     97 	}
     98 	return(trans);
     99 }
    100 
    101 void
    102 mopGetHeader(const u_char *pkt, int *idx, const u_char **dst, const u_char **src,
    103 	     u_short *proto, int *len, int trans)
    104 {
    105 	*dst = pkt;
    106 	*src = pkt + 6;
    107 	*idx = *idx + 12;
    108 
    109 	switch(trans) {
    110 	case TRANS_ETHER:
    111 		*proto = (u_short)(pkt[*idx]*256 + pkt[*idx+1]);
    112 		*idx = *idx + 2;
    113 		*len   = (int)(pkt[*idx+1]*256 + pkt[*idx]);
    114 		*idx = *idx + 2;
    115 		break;
    116 	case TRANS_8023:
    117 		*len   = (int)(pkt[*idx]*256 + pkt[*idx+1]);
    118 		*idx = *idx + 8;
    119 		*proto = (u_short)(pkt[*idx]*256 + pkt[*idx+1]);
    120 		*idx = *idx + 2;
    121 		break;
    122 	}
    123 }
    124 
    125 u_short
    126 mopGetLength(const u_char *pkt, int trans)
    127 {
    128 	switch(trans) {
    129 	case TRANS_ETHER:
    130 		return(pkt[15]*256 + pkt[14]);
    131 		break;
    132 	case TRANS_8023:
    133 		return(pkt[12]*256 + pkt[13]);
    134 		break;
    135 	}
    136 	return(0);
    137 }
    138