12632SN/A/*
22632SN/A * arp.h
32632SN/A *
42632SN/A * Address Resolution Protocol.
52632SN/A * RFC 826
62632SN/A *
72632SN/A * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
82632SN/A *
92632SN/A * $Id: arp.h,v 1.12 2003/03/16 17:39:17 dugsong Exp $
102632SN/A */
112632SN/A
122632SN/A#ifndef DNET_ARP_H
132632SN/A#define DNET_ARP_H
142632SN/A
152632SN/A#define ARP_HDR_LEN	8	/* base ARP header length */
162632SN/A#define ARP_ETHIP_LEN	20	/* base ARP message length */
172632SN/A
182632SN/A#ifndef __GNUC__
192632SN/A# define __attribute__(x)
202632SN/A# pragma pack(1)
212632SN/A#endif
222632SN/A
232632SN/A/*
242632SN/A * ARP header
252632SN/A */
262632SN/Astruct arp_hdr {
272632SN/A        uint16_t	ar_hrd;	/* format of hardware address */
282632SN/A        uint16_t	ar_pro;	/* format of protocol address */
292632SN/A        uint8_t		ar_hln;	/* length of hardware address (ETH_ADDR_LEN) */
302632SN/A        uint8_t		ar_pln;	/* length of protocol address (IP_ADDR_LEN) */
312632SN/A        uint16_t	ar_op;	/* operation */
322632SN/A};
332632SN/A
342632SN/A/*
352632SN/A * Hardware address format
362632SN/A */
372632SN/A#define ARP_HRD_ETH 	0x0001	/* ethernet hardware */
382632SN/A#define ARP_HRD_IEEE802	0x0006	/* IEEE 802 hardware */
392632SN/A
402632SN/A/*
412632SN/A * Protocol address format
422632SN/A */
432632SN/A#define ARP_PRO_IP	0x0800	/* IP protocol */
442632SN/A
452632SN/A/*
462632SN/A * ARP operation
472632SN/A */
482632SN/A#define	ARP_OP_REQUEST		1	/* request to resolve ha given pa */
492632SN/A#define	ARP_OP_REPLY		2	/* response giving hardware address */
502632SN/A#define	ARP_OP_REVREQUEST	3	/* request to resolve pa given ha */
512632SN/A#define	ARP_OP_REVREPLY		4	/* response giving protocol address */
522632SN/A
532632SN/A/*
542632SN/A * Ethernet/IP ARP message
552632SN/A */
562632SN/Astruct arp_ethip {
572632SN/A        uint8_t		ar_sha[ETH_ADDR_LEN];	/* sender hardware address */
582632SN/A        uint8_t		ar_spa[IP_ADDR_LEN];	/* sender protocol address */
592632SN/A        uint8_t		ar_tha[ETH_ADDR_LEN];	/* target hardware address */
602632SN/A        uint8_t		ar_tpa[IP_ADDR_LEN];	/* target protocol address */
612632SN/A};
622632SN/A
632632SN/A/*
642632SN/A * ARP cache entry
652632SN/A */
662632SN/Astruct arp_entry {
672632SN/A        struct addr	arp_pa;			/* protocol address */
682632SN/A        struct addr	arp_ha;			/* hardware address */
692632SN/A};
702632SN/A
712632SN/A#ifndef __GNUC__
722632SN/A# pragma pack()
732632SN/A#endif
742632SN/A
752632SN/A#define arp_pack_hdr_ethip(hdr, op, sha, spa, tha, tpa) do {	\
762632SN/A        struct arp_hdr *pack_arp_p = (struct arp_hdr *)(hdr);	\
772632SN/A        struct arp_ethip *pack_ethip_p = (struct arp_ethip *)	\
782632SN/A                ((uint8_t *)(hdr) + ARP_HDR_LEN);		\
792632SN/A        pack_arp_p->ar_hrd = htons(ARP_HRD_ETH);		\
802632SN/A        pack_arp_p->ar_pro = htons(ARP_PRO_IP);			\
812632SN/A        pack_arp_p->ar_hln = ETH_ADDR_LEN;			\
822632SN/A        pack_arp_p->ar_pln = IP_ADDR_LEN;			\
832632SN/A        pack_arp_p->ar_op = htons(op);				\
842632SN/A        memmove(pack_ethip_p->ar_sha, &(sha), ETH_ADDR_LEN);	\
852632SN/A        memmove(pack_ethip_p->ar_spa, &(spa), IP_ADDR_LEN);	\
862632SN/A        memmove(pack_ethip_p->ar_tha, &(tha), ETH_ADDR_LEN);	\
872632SN/A        memmove(pack_ethip_p->ar_tpa, &(tpa), IP_ADDR_LEN);	\
882632SN/A} while (0)
892632SN/A
902632SN/Atypedef struct arp_handle arp_t;
912632SN/A
922632SN/Atypedef int (*arp_handler)(const struct arp_entry *entry, void *arg);
932632SN/A
942632SN/A__BEGIN_DECLS
952632SN/Aarp_t	*arp_open(void);
962632SN/Aint	 arp_add(arp_t *arp, const struct arp_entry *entry);
972632SN/Aint	 arp_delete(arp_t *arp, const struct arp_entry *entry);
982632SN/Aint	 arp_get(arp_t *arp, struct arp_entry *entry);
992632SN/Aint	 arp_loop(arp_t *arp, arp_handler callback, void *arg);
1002632SN/Aarp_t	*arp_close(arp_t *arp);
1012632SN/A__END_DECLS
1022632SN/A
1032632SN/A#endif /* DNET_ARP_H */
104