-
Notifications
You must be signed in to change notification settings - Fork 9
/
arphdr.c
60 lines (52 loc) · 1.36 KB
/
arphdr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* ARP header conversion routines
* Copyright 1991 Phil Karn, KA9Q
*/
#include "global.h"
#include "mbuf.h"
#include "arp.h"
/* Copy a host format arp structure into mbuf for transmission */
struct mbuf *
htonarp(arp)
register struct arp *arp;
{
struct mbuf *bp;
register uint8 *buf;
if(arp == (struct arp *)NULL)
return NULL;
bp = ambufw(ARPLEN + 2 * arp->hwalen);
buf = bp->data;
buf = put16(buf,arp->hardware);
buf = put16(buf,arp->protocol);
*buf++ = arp->hwalen;
*buf++ = arp->pralen;
buf = put16(buf,arp->opcode);
memcpy(buf,arp->shwaddr,(uint16)arp->hwalen);
buf += arp->hwalen;
buf = put32(buf,arp->sprotaddr);
memcpy(buf,arp->thwaddr,(uint16)arp->hwalen);
buf += arp->hwalen;
buf = put32(buf,arp->tprotaddr);
bp->cnt = buf - bp->data;
return bp;
}
/* Convert an incoming ARP packet into a host-format structure */
int
ntoharp(
struct arp *arp,
struct mbuf **bpp
){
if(arp == (struct arp *)NULL || bpp == NULL)
return -1;
arp->hardware = pull16(bpp);
arp->protocol = pull16(bpp);
arp->hwalen = PULLCHAR(bpp);
arp->pralen = PULLCHAR(bpp);
arp->opcode = pull16(bpp);
pullup(bpp,arp->shwaddr,(uint16)arp->hwalen);
arp->sprotaddr = pull32(bpp);
pullup(bpp,arp->thwaddr,(uint16)arp->hwalen);
arp->tprotaddr = pull32(bpp);
/* Get rid of anything left over */
free_p(bpp);
return 0;
}