40 #define _________________________________________LOCAL_TYPES_SECTION________________________________________________________________________ 44 MessageType_Discover = 1 ,
45 MessageType_Offer = 2 ,
46 MessageType_Request = 3 ,
47 MessageType_Declide = 4 ,
48 MessageType_Acknowledge = 5 ,
50 MessageType_Release = 7 ,
51 MessageType_Inform = 8 ,
54 #undef _________________________________________LOCAL_TYPES_SECTION________________________________________________________________________ 62 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 68 static uint8_t*
ReadOption ( uint8_t * OptionsArray , uint32_t * LeftBytes , uint8_t * outData );
72 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 80 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 114 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
118 && ErrorCondition( isaddresscorrect(Message) , oC_ErrorCode_WrongAddress )
130 bzero( &info,
sizeof(info) );
133 ErrorCondition ( packet !=
NULL , oC_ErrorCode_AllocationError )
147 errorCode = oC_ErrorCode_None;
181 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
185 && ErrorCondition( isram(outMessage) , oC_ErrorCode_OutputAddressNotInRAM )
193 ErrorCondition ( packet !=
NULL , oC_ErrorCode_AllocationError )
199 errorCode = oC_ErrorCode_None;
201 oC_SaveIfFalse(
"DHCP::ReceiveMessage - ", packet ==
NULL || kfree(packet, 0), oC_ErrorCode_ReleaseError);
234 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
235 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
242 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
243 && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
244 && ErrorCode (
oC_Udp_ReservePort(&udpPort, oC_KTime_CalculateTimeout(endTimestamp)) )
254 errorCode = oC_ErrorCode_None;
288 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
293 && ErrorCondition( hardwareType != 0 , oC_ErrorCode_UnknownHardwareType )
301 bzero( &info,
sizeof( info ) );
302 bzero( &hwAddress,
sizeof( hwAddress ) );
305 ErrorCondition ( message !=
NULL , oC_ErrorCode_AllocationError )
310 uint8_t* arrayEndPointer = &message->
Options[0];
311 uint32_t leftBytes =
sizeof(message->
Options);
313 MessageType_t messageType = MessageType_Discover;
314 uint8_t clientId[7] = {0};
315 uint8_t requestedList[] = {
326 info.
DhcpIP = IP(255,255,255,255);
327 info.
IP = IP(0,0,0,0);
329 info.
NetIP = IP(0,0,0,0);
341 message->
XID = (uint32_t)Netif;
350 clientId[0] = hardwareType;
391 oC_SaveIfFalse(
"DHCP::Discovery - cannot release message memory - ", message ==
NULL || kfree(message,0), oC_ErrorCode_ReleaseError );
425 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
435 bzero( &info,
sizeof( info ) );
438 ErrorCondition ( message !=
NULL , oC_ErrorCode_AllocationError )
442 oC_Timestamp_t startTime = oC_KTime_GetTimestamp();
443 oC_Timestamp_t endTime = startTime + Timeout;
444 oC_Timestamp_t currentTime = oC_KTime_GetTimestamp();
445 MessageType_t messageType = 0;
455 uint32_t leftBytes =
sizeof(message->
Options);
456 uint8_t* options = message->
Options;
463 memcpy(&tempInfo,&info,
sizeof(tempInfo));
473 && message->
XID == ((uint32_t)Netif)
483 memset(data,0,
sizeof(data));
486 options =
ReadOption(options,&leftBytes,data);
493 memcpy(&messageType, data,
sizeof(messageType));
502 memcpy(&tempInfo.
NetIP, data,
sizeof(tempInfo.
NetIP));
512 memcpy(&tempInfo.
DnsIP, data,
sizeof(tempInfo.
DnsIP));
521 memcpy(&tempInfo.
MTU, data,
sizeof(tempInfo.
MTU));
526 memcpy(&tempInfo.
NtpIP, data,
sizeof(tempInfo.
NtpIP));
531 uint32_t leaseTime = 0;
533 memcpy(&leaseTime, data,
sizeof(uint32_t));
537 tempInfo.
LeaseTime = (oC_Time_t)leaseTime;
544 if(messageType == MessageType_Offer)
546 memcpy(&info,&tempInfo,
sizeof(info));
553 currentTime = oC_KTime_GetTimestamp();
555 if(currentTime <= endTime)
557 Timeout = endTime - currentTime;
561 errorCode = oC_ErrorCode_Timeout;
570 if(messageType == MessageType_Offer)
575 oC_SaveIfFalse(
"DHCP::Offer - cannot release message memory - ", message ==
NULL || free(message,0) , oC_ErrorCode_ReleaseError);
606 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
611 && ErrorCondition( hardwareType != 0 , oC_ErrorCode_UnknownHardwareType )
619 bzero( &info,
sizeof( info ) );
620 bzero( &hwAddress,
sizeof( hwAddress ) );
623 ErrorCondition( message !=
NULL , oC_ErrorCode_AllocationError )
628 uint8_t* arrayEndPointer = &message->
Options[0];
629 uint32_t leftBytes =
sizeof(message->
Options);
632 MessageType_t messageType = MessageType_Request;
643 message->
XID = (uint32_t)Netif;
680 oC_SaveIfFalse(
"DHCP::Request - cannot release message memory - ", message ==
NULL || free(message,0) , oC_ErrorCode_ReleaseError);
714 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
724 bzero( &info,
sizeof( info ) );
727 ErrorCondition ( message !=
NULL , oC_ErrorCode_AllocationError )
731 oC_Timestamp_t startTime = oC_KTime_GetTimestamp();
732 oC_Timestamp_t endTime = startTime + Timeout;
733 oC_Timestamp_t currentTime = oC_KTime_GetTimestamp();
734 MessageType_t messageType = 0;
744 uint32_t leftBytes =
sizeof(message->
Options);
745 uint8_t* options = message->
Options;
752 memcpy(&tempInfo,&info,
sizeof(tempInfo));
756 && message->
XID == ((uint32_t)Netif)
766 memset(data,0,
sizeof(data));
769 options =
ReadOption(options,&leftBytes,data);
776 memcpy(&messageType, data,
sizeof(messageType));
785 memcpy(&tempInfo.
NetIP, data,
sizeof(tempInfo.
NetIP));
795 memcpy(&tempInfo.
DnsIP, data,
sizeof(tempInfo.
DnsIP));
804 memcpy(&tempInfo.
MTU, data,
sizeof(tempInfo.
MTU));
809 memcpy(&tempInfo.
NtpIP, data,
sizeof(tempInfo.
NtpIP));
814 uint32_t leaseTime = 0;
816 memcpy(&leaseTime, data,
sizeof(uint32_t));
820 tempInfo.
LeaseTime = (oC_Time_t)leaseTime;
828 if(messageType == MessageType_Acknowledge || messageType == MessageType_NAK)
830 memcpy(&info,&tempInfo,
sizeof(info));
839 currentTime = oC_KTime_GetTimestamp();
841 if(currentTime <= endTime)
843 Timeout = endTime - currentTime;
847 errorCode = oC_ErrorCode_Timeout;
855 if(messageType == MessageType_Acknowledge || messageType == MessageType_NAK)
868 oC_SaveIfFalse(
"DHCP::Acknowledge - cannot release message memory - ", message ==
NULL || free(message,0) , oC_ErrorCode_ReleaseError);
875 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 882 #define _________________________________________LOCAL_FUNCTIONS_SECTION___________________________________________________________________ 899 oC_SaveError(
"Dhcp::TranslateToDhcpHardwareType: unsupported hardware type - %d\n", HardwareType);
920 kdebuglog(oC_LogType_Error ,
"Dhcp::GetHardwareAddressLength: unsupported hardware type - %d\n", HardwareType);
943 uint32_t realOptionSize = Length + 2;
945 if((*LeftBytes) >= (realOptionSize + 1))
947 OptionsArray[0] = (uint8_t)Option;
948 OptionsArray[1] = Length;
952 for(uint32_t i = 2; i < realOptionSize; i++ )
954 OptionsArray[i] = Data[i-2];
958 (*LeftBytes) = (*LeftBytes) - realOptionSize;
959 OptionsArray += realOptionSize;
963 kdebuglog(oC_LogType_Error,
"Dhcp::AddOption - Cannot add option %d (length %d) - there is no more space in array\n", Option, Length);
976 bool nextOptionFound =
false;
983 nextOptionFound =
true;
986 return nextOptionFound;
994 static uint8_t*
ReadOption( uint8_t * OptionsArray , uint32_t * LeftBytes , uint8_t * outData )
998 uint32_t realOptionSize = 2;
1002 realOptionSize += length;
1006 for(uint32_t i = 2;i < realOptionSize; i++)
1008 outData[i-2] = OptionsArray[i];
1012 OptionsArray = &OptionsArray[realOptionSize];
1013 *LeftBytes -= realOptionSize;
1016 return OptionsArray;
1047 #undef _________________________________________LOCAL_FUNCTIONS_SECTION___________________________________________________________________
oC_ErrorCode_t oC_Dhcp_RequestIp(oC_Netif_t Netif, oC_Time_t Timeout)
performs DHCP IP request procedure
uint32_t GIAddr
Gateway IP Address - a router IP
oC_Net_Ipv4_t NetIP
IP of the local network (router IP)
oC_ErrorCode_t oC_Dhcp_ReceiveOffer(oC_Netif_t Netif, oC_Time_t Timeout)
receives offer from the DHCP server
uint8_t Options[oC_DHCP_OPTIONS_BUFFER_SIZE]
Additional options array.
Contains definitions of DHCP.
oC_ErrorCode_t oC_Netif_ReadHardwareAddress(oC_Netif_t Netif, oC_Net_HardwareAddress_t *outHardwareAddress)
reads hardware address of the interface
bool oC_Udp_IsPortReserved(oC_Udp_Port_t Port, oC_Process_t Process)
checks if the given UDP port is reserved
oC_Udp_Port_t
stores UDP port number
static uint32_t GetHardwareAddressLength(oC_Dhcp_HardwareType_t HardwareType)
returns number of bytes required for storing hardware address
Length in bytes for option oC_Dhcp_Option_MessageType.
uint32_t YIAddr
Your IP Address - the server fills it as the proposed IP address for the client
oC_Time_t LeaseTime
IP lease time.
The router option specifies a list of IP addresses on the client subnetwork.
uint32_t MagicCookie
Magic cookie - set to oC_DHCP_MAGIC_COOKIE (0x63825363) to identify the information as vendor indepen...
uint32_t Hops
Set to 0 by a client when transmitting a request and used by relay agents to control the forwarding o...
Broadcast flag - a client does not know its own IP address so it send the message as broadcast...
Contains interface for netif object.
Bootstrap Protocol (BOOTP) client; also used by Dynamic Host Configuration Protocol (DHCP) ...
struct PACKED oC_Dhcp_Message_t
stores DHCP message
oC_Net_Ipv4_t DnsIP
IP of the local DNS server.
Used by the DHCP client to request values for specified configuration parameters. ...
uint32_t Secs
Seconds - In BOOTP this field was vaguely defined and not always used.
Address to use in client subnetwork for sending broadcast packets.
static void ConvertFromNetworkEndianess(oC_Dhcp_Message_t *Message)
converts message from network endianess
oC_ErrorCode_t oC_Netif_ReadHardwareAddressOf(oC_Netif_t Netif, oC_Net_HardwareAddress_t *outHardwareAddress, const oC_Net_Address_t *Address, oC_Time_t Timeout)
reads hardware address of the given IP address in LAN
uint32_t oC_Net_Ipv4_t
stores IPv4 address
bool oC_Netif_IsCorrect(oC_Netif_t Netif)
checks if the Netif object is correct
Destination IP address of DHCP server.
uint8_t oC_Net_MacAddress_t[MAC_ADDRESS_LENGTH]
stores MAC address
uint32_t Operation
Specifies type of the message.
oC_Net_HardwareType_t
stores hardware type of the network interface
oC_ErrorCode_t oC_Dhcp_SendDiscovery(oC_Netif_t Netif, oC_Time_t Timeout)
sends DHCP discovery message to DHCP server
oC_Net_Ipv4_t NtpIP
Main Network Time Protocol Server available in the subnetwork.
oC_Net_Ipv4_t Netmask
Local network mask.
uint8_t DefaultTTL
Default TTL value for the IPv4 packets.
Length in bytes for option oC_Dhcp_Option_RequestedIp.
oC_ErrorCode_t oC_Dhcp_SendRequest(oC_Netif_t Netif, oC_Time_t Timeout)
sends request message to the DHCP server
oC_Net_Ipv4_t BroadcastIP
IP that should be used for broadcasting.
#define oC_DHCP_END_OPTION
Value for marking last entry in the options array.
bool oC_Udp_Packet_Delete(oC_Udp_Packet_t **Packet)
release memory allocated for a UDP packet
oC_ErrorCode_t oC_Udp_ReservePort(oC_Udp_Port_t *Port, oC_Time_t Timeout)
reserves a UDP port
uint32_t XID
Transaction ID Generated by the client to allow it to match up the request with replies received from...
static bool FindNextOption(uint8_t *OptionsArray, uint32_t LeftBytes, oC_Dhcp_Option_t *outOption, oC_Dhcp_OptionLength_t *outLength)
checks if is there any more option in the options array
oC_MemorySize_t MTU
MTU (Maximum Transmission Unit) for the local network.
oC_ErrorCode_t oC_Udp_ReleasePort(oC_Udp_Port_t Port, oC_Time_t Timeout)
releases a port
uint32_t SIAddr
Server IP Address - the server fills it with its own address, that should be used for the states afte...
oC_ErrorCode_t oC_Dhcp_ReceiveAcknowledge(oC_Netif_t Netif, oC_Time_t Timeout)
receives ACK message from DHCP server
static uint32_t oC_Net_ConvertUint32FromNetworkEndianess(uint32_t v)
converts uint32_t from network byte order
Specifies MTU for the interface.
oC_Timestamp_t IpExpiredTimestamp
Timestamp of IP expiration.
The option allows to request the specified static IP.
static void ConvertToNetworkEndianess(oC_Dhcp_Message_t *Message)
converts message to network endianess
Specifies subnetwork mask.
oC_ErrorCode_t oC_Netif_SetIpv4Info(oC_Netif_t Netif, const oC_Net_Ipv4Info_t *Info)
sets informations about IPv4
The end option marks end of the options array.
uint8_t CHAddr[16]
Client Hardware Address - the hardware address of the client used for identification and communicatio...
static uint32_t oC_Net_ConvertUint32ToNetworkEndianess(uint32_t v)
converts uint32_t to network byte order
oC_ErrorCode_t oC_Udp_Packet_ReadData(const oC_Udp_Packet_t *Packet, void *outData, uint16_t Size)
reads data of the UDP packet
oC_ErrorCode_t oC_Dhcp_ReceiveMessage(oC_Netif_t Netif, oC_Dhcp_Message_t *outMessage, oC_Time_t Timeout)
receives DHCP message from DHCP server
List of Network Time Protocol Servers available in the subnetwork.
char SName[64]
Server Name - The server can optionally put its name in this field
oC_Udp_Packet_t * oC_Udp_Packet_New(Allocator_t PacketAllocator, oC_Net_PacketType_t Type, const void *Data, uint16_t Size)
allocates memory for a UDP packet
uint32_t HardwareAddressLength
Length of the hardware address in bytes.
Length in bytes for option oC_Dhcp_Option_ServerId.
The file with memory manager interface.
bool oC_Netif_IsConfigured(oC_Netif_t Netif)
checks if the driver is configured
Bootstrap Protocol (BOOTP) server; also used by Dynamic Host Configuration Protocol (DHCP) ...
Network interface is DOWN (cable is not connected)
oC_Dhcp_Option_t
Stores DHCP options that can be added into the DHCP message.
List of DNS servers in the subnetwork.
uint32_t HardwareType
Hardware Type - specifies type of the hardware used in the local network.
#define oC_DHCP_OPTIONS_BUFFER_SIZE
stores maximum number of bytes reserved for options in DHCP message
oC_ErrorCode_t oC_Udp_Send(oC_Netif_t Netif, oC_Udp_Port_t LocalPort, const oC_Net_Address_t *Destination, oC_Udp_Packet_t *Packet, oC_Time_t Timeout)
sends UDP packet
oC_Net_Ipv4_t GatewayIP
Default gateway IP.
uint32_t CIAddr
Client IP Address - If a client has IP address and it is valid (while BOUND, RENEWING or REBINDING st...
Used by the DHCP clients to specify their unique ID.
oC_Net_HardwareType_t oC_Netif_GetHardwareType(oC_Netif_t Netif)
returns hardware type of the network interface
oC_Dhcp_HardwareType_t
stores type of hardware for oC_Dhcp_Message_t type
oC_ErrorCode_t oC_Udp_Receive(oC_Netif_t Netif, oC_Udp_Port_t LocalPort, oC_Udp_Packet_t *Packet, oC_Time_t Timeout)
receives UDP packet
oC_ErrorCode_t oC_Netif_ReadIpv4Info(oC_Netif_t Netif, oC_Net_Ipv4Info_t *outInfo)
reads IPv4 informations
Allows the client to request a lease time for the IP address.
char DhcpServerName[DHCP_SERVER_NAME_LENGTH]
Name of the local DHCP server.
Used to mark type of the message - 1 for Discover, 2 - Offer, 3 - Request, 4 - Declide, 5 - Pack , 6 - Pnak, 7 - Release, 8 - Inform
oC_Net_LinkStatus_t oC_Netif_GetLinkStatus(oC_Netif_t Netif)
returns last known link status of the network interface
oC_Net_Ipv4_t IP
IP of the network interface.
static uint8_t * AddOption(uint8_t *OptionsArray, uint32_t *LeftBytes, oC_Dhcp_Option_t Option, oC_Dhcp_OptionLength_t Length, uint8_t *Data)
adds option to the options array
oC_ErrorCode_t oC_Dhcp_SendMessage(oC_Netif_t Netif, oC_Dhcp_Message_t *Message, uint16_t Size, oC_Time_t Timeout)
sends DHCP message to DHCP server
stores network interface IPv4 informations
Specifies default TTL value for packets.
static uint8_t * ReadOption(uint8_t *OptionsArray, uint32_t *LeftBytes, uint8_t *outData)
reads option in the Options array and returns pointer to the next option
oC_Net_AddressType_t Type
Type of the address stored inside.
Specifies name of the client.
static oC_Dhcp_HardwareType_t TranslateToDhcpHardwareType(oC_Net_HardwareType_t HardwareType)
translates oC_Net_HardwareType_t to oC_Dhcp_HardwareType_t
uint32_t Flags
Flags of the message, look at oC_Dhcp_Flags_t type for more.
oC_Net_Ipv4_t DhcpIP
IP of the local DHCP server.
The file with interface of kernel time module.
oC_Net_HardwareAddress_t HardwareRouterAddress
HW address of the local router.
#define NULL
pointer to a zero
#define oC_DHCP_MAGIC_COOKIE
Magic cookie for the DHCP message.
oC_Dhcp_OptionLength_t
stores length for options from oC_Dhcp_Option_t type