Home | History | Annotate | Line # | Download | only in common
get.c revision 1.4
      1 /*	$NetBSD: get.c,v 1.4 2009/04/17 04:16:57 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.4 2009/04/17 04:16:57 lukem Exp $");
     35 #endif
     36 
     37 #include "os.h"
     38 #include "get.h"
     39 #include "mopdef.h"
     40 
     41 u_char
     42 mopGetChar(pkt, idx)
     43 	u_char *pkt;
     44 	int    *idx;
     45 {
     46         u_char ret;
     47 
     48 	ret = pkt[*idx];
     49 	*idx = *idx + 1;
     50 	return(ret);
     51 }
     52 
     53 u_short
     54 mopGetShort(pkt, idx)
     55 	u_char *pkt;
     56 	int    *idx;
     57 {
     58         u_short ret;
     59 
     60 	ret = pkt[*idx] + pkt[*idx+1]*256;
     61 	*idx = *idx + 2;
     62 	return(ret);
     63 }
     64 
     65 u_int32_t
     66 mopGetLong(pkt, idx)
     67 	u_char *pkt;
     68 	int    *idx;
     69 {
     70         u_int32_t ret;
     71 
     72 	ret = pkt[*idx] +
     73 	      pkt[*idx+1]*0x100 +
     74 	      pkt[*idx+2]*0x10000 +
     75 	      pkt[*idx+3]*0x1000000;
     76 	*idx = *idx + 4;
     77 	return(ret);
     78 }
     79 
     80 void
     81 mopGetMulti(pkt, idx, dest, size)
     82 	u_char *pkt,*dest;
     83 	int    *idx,size;
     84 {
     85 	int i;
     86 
     87 	for (i = 0; i < size; i++) {
     88 	  dest[i] = pkt[*idx+i];
     89 	}
     90 	*idx = *idx + 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, idx, dst, src, proto, len, trans)
    114 	u_char	*pkt, **dst, **src;
    115 	int	*idx, *len, trans;
    116 	u_short	*proto;
    117 {
    118 	*dst = pkt;
    119 	*src = pkt + 6;
    120 	*idx = *idx + 12;
    121 
    122 	switch(trans) {
    123 	case TRANS_ETHER:
    124 		*proto = (u_short)(pkt[*idx]*256 + pkt[*idx+1]);
    125 		*idx = *idx + 2;
    126 		*len   = (int)(pkt[*idx+1]*256 + pkt[*idx]);
    127 		*idx = *idx + 2;
    128 		break;
    129 	case TRANS_8023:
    130 		*len   = (int)(pkt[*idx]*256 + pkt[*idx+1]);
    131 		*idx = *idx + 8;
    132 		*proto = (u_short)(pkt[*idx]*256 + pkt[*idx+1]);
    133 		*idx = *idx + 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