Watt-32 tcp/ip  2.2 dev-rel.10
pcrarp.c
Go to the documentation of this file.
1 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include "copyrigh.h"
17 #include "wattcp.h"
18 #include "strings.h"
19 #include "language.h"
20 #include "misc.h"
21 #include "timer.h"
22 #include "rs232.h"
23 #include "pcsed.h"
24 #include "pctcp.h"
25 #include "pcdbug.h"
26 #include "pcrarp.h"
27 
28 #if defined(USE_RARP)
29 
30 WORD _rarptimeout = 5;
31 
32 static int _rarp_request (void)
33 {
34  rarp_Header *rarp = (rarp_Header*) _eth_formatpacket (&_eth_brdcast[0],
35  RARP_TYPE);
36  rarp->hwType = intel16 (_eth_get_hwtype(NULL,NULL));
37  rarp->protType = IP4_TYPE;
38  rarp->hwAddrLen = sizeof (mac_address);
39  rarp->protoAddrLen = sizeof (my_ip_addr);
40  rarp->opcode = RARP_REQUEST;
41  rarp->srcIPAddr = 0;
42  rarp->dstIPAddr = 0;
43  memcpy (rarp->srcEthAddr, _eth_addr, sizeof(mac_address));
44  memcpy (rarp->dstEthAddr, _eth_addr, sizeof(mac_address));
45 
46  return _eth_send (sizeof(*rarp), NULL, __FILE__, __LINE__);
47 }
48 
52 BOOL _rarp_handler (const rarp_Header *rh, BOOL brdcast)
53 {
54  SIO_TRACE (("_rarp_handler"));
55 
56  DEBUG_RX (NULL, rh);
57 
58  if (!brdcast && rh->opcode == RARP_REPLY && rh->protType == IP4_TYPE &&
59  !memcmp(rh->dstEthAddr,_eth_addr,sizeof(mac_address)))
60  {
61  my_ip_addr = intel (rh->dstIPAddr);
62  return (TRUE);
63  }
64  return (FALSE);
65 }
66 
67 /*
68  * _dorarp - Checks global variable _rarptimeout
69  * returns 1 on success and sets ip address
70  */
71 int _dorarp (void)
72 {
73  DWORD rarptimeout = set_timeout (1000 * _rarptimeout);
74  WORD magictimeout = Random (7000, 14000);
75 
76  outs (_LANG("Configuring through RARP..."));
77 
78  while (1)
79  {
80  DWORD sendtimeout;
81 
82  if (!_rarp_request())
83  break;
84 
85  sendtimeout = set_timeout (magictimeout);
86  magictimeout += Random (1000, 7000);
87 
88  while (!chk_timeout(sendtimeout))
89  {
90  const struct rarp_Header *rarp;
91  WORD eth_type;
92  BOOL bcast;
93 
94  if (chk_timeout(rarptimeout))
95  return (0);
96 
97  WATT_YIELD();
98 
99  rarp = (rarp_Header*) _eth_arrived (&eth_type, &bcast);
100  if (!rarp)
101  continue;
102 
103  DEBUG_RX (NULL, rarp);
104 
105  if (eth_type == RARP_TYPE && !bcast &&
106  rarp->opcode == RARP_REPLY && rarp->protType == IP4_TYPE &&
107  !memcmp(rarp->dstEthAddr,_eth_addr,sizeof(mac_address)))
108  {
109  my_ip_addr = intel (rarp->dstIPAddr);
110  _eth_free (rarp);
111  return (1);
112  }
113  _eth_free (rarp);
114  }
115  }
116  return (0);
117 }
118 #endif
unsigned W32_CALL Random(unsigned a, unsigned b)
Returns a random integer in range [a..b].
Definition: misc.c:742
mac_address _eth_brdcast
Link-layer broadcast address.
Definition: pcsed.c:51
void W32_CALL _eth_free(const void *pkt)
Free an input buffer once it is no longer needed.
Definition: pcsed.c:792
BYTE W32_CALL _eth_get_hwtype(BYTE *hwtype, BYTE *hwlen)
Fill in hardware address type/length for BOOTP/DHCP packets.
Definition: pcsed.c:751
int W32_CALL _eth_send(WORD len, const void *sock, const char *file, unsigned line)
_eth_send() does the actual transmission once we are complete with filling the buffer.
Definition: pcsed.c:306
Core definitions.
mac_address _eth_addr
Local link-layer source address.
Definition: pcsed.c:50
BOOL _rarp_handler(const rarp_Header *rh, BOOL brdcast)
Handle incoming RARP packets.
Definition: pcrarp.c:52
DWORD W32_CALL set_timeout(DWORD msec)
Return time for when given timeout (msec) expires.
Definition: timer.c:503
void *W32_CALL _eth_formatpacket(const void *mac_dest, WORD eth_type)
_eth_format_packet() places the next packet to be transmitted into the above link-layer output packet...
Definition: pcsed.c:135
DWORD my_ip_addr
our IP address
Definition: pctcp.c:70
BOOL W32_CALL chk_timeout(DWORD value)
Check if milli-sec value has expired:
Definition: timer.c:547
void *W32_CALL _eth_arrived(WORD *type_ptr, BOOL *broadcast)
Poll for arrival of new packets (IP/ARP/RARP/PPPoE protocol).
Definition: pcsed.c:1026