Split DHCPv4/DHCPv6.

This commit is contained in:
bossiel 2010-02-10 18:22:32 +00:00
parent 2f4deff727
commit b9907a4ae7
8 changed files with 122 additions and 44 deletions

View File

@ -21,7 +21,7 @@
*/ */
/**@file tnet_dhcp.c /**@file tnet_dhcp.c
* @brief DHCP/BOOTP (RFC 2131 - Dynamic Host Configuration Protocol) utilities function for P-CSCF discovery(RFC 3319 and 3361). * @brief DHCP/BOOTP (RFC 2131 - Dynamic Host Configuration Protocol) utilities function for P-CSCF discovery(RFC 3319 and 3361).
* Also implement: RFC 3315, 3319, ... * Also implement: RFC 3315, 3118, 3319, 3825 (Geoconf), 4676 (Civic Addresses Configuration Information) ...
* *
* @author Mamadou Diop <diopmamadou(at)yahoo.fr> * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
* *
@ -39,6 +39,7 @@
// Useful link: http://support.microsoft.com/?scid=kb%3Ben-us%3B169289&x=21&y=14 // Useful link: http://support.microsoft.com/?scid=kb%3Ben-us%3B169289&x=21&y=14
// Another one: http://www.iana.org/assignments/bootp-dhcp-parameters/ // Another one: http://www.iana.org/assignments/bootp-dhcp-parameters/
// Another one: http://www.slideshare.net/raini/DHCP-Presentation-v102
// RFC 3319 Dynamic Host Configuration Protocol (DHCPv6) Options for Session Initiation Protocol (SIP) Servers // RFC 3319 Dynamic Host Configuration Protocol (DHCPv6) Options for Session Initiation Protocol (SIP) Servers
// RFC 3361 Dynamic Host Configuration Protocol (DHCP-for-IPv4) Option for Session Initiation Protocol (SIP) Servers // RFC 3361 Dynamic Host Configuration Protocol (DHCP-for-IPv4) Option for Session Initiation Protocol (SIP) Servers
@ -52,44 +53,30 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_reques
int ret; int ret;
struct timeval tv; struct timeval tv;
fd_set set; fd_set set;
tnet_fd_t clientFD = TNET_INVALID_FD;
uint64_t timeout = 0; uint64_t timeout = 0;
tsk_list_item_t *item; tsk_list_item_t *item;
const tnet_interface_t *iface; const tnet_interface_t *iface;
tnet_socket_t *localsocket4 = 0; tnet_socket_t *localsocket4 = 0;
tnet_socket_t *localsocket6 = 0;
struct sockaddr_storage server; struct sockaddr_storage server;
if(!ctx || !request) if(!ctx || !request)
{ {
goto bail; goto bail;
} }
if(ctx->use_ipv6) localsocket4 = TNET_SOCKET_CREATE(TNET_SOCKET_HOST_ANY, ctx->port_client, tnet_socket_type_udp_ipv4);
if(!TNET_SOCKET_IS_VALID(localsocket4))
{ {
localsocket6 = TNET_SOCKET_CREATE(TNET_SOCKET_HOST_ANY, ctx->port_client, tnet_socket_type_udp_ipv6); TSK_DEBUG_ERROR("Failed to create/bind DHCP client socket.");
if(TNET_SOCKET_IS_VALID(localsocket6)) goto bail;
{
clientFD = localsocket6->fd;
}
else goto bail;
}
else
{
localsocket4 = TNET_SOCKET_CREATE(TNET_SOCKET_HOST_ANY, ctx->port_client, tnet_socket_type_udp_ipv4);
if(TNET_SOCKET_IS_VALID(localsocket4))
{
clientFD = localsocket4->fd;
}
else goto bail;
} }
/* Always wait for 500ms before retransmission */ /* Always wait for 200ms before retransmission */
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = (500 * 1000); tv.tv_usec = (200 * 1000);
if(tnet_sockaddr_init("255.255.255.255"/* FIXME: IPv6 */, ctx->server_port, (ctx->use_ipv6 ? tnet_socket_type_udp_ipv6 : tnet_socket_type_udp_ipv4), &server)) if(tnet_sockaddr_init("255.255.255.255", ctx->server_port, tnet_socket_type_udp_ipv4, &server))
{ {
TNET_PRINT_LAST_ERROR("Failed to initialize the DHCP server address."); TNET_PRINT_LAST_ERROR("Failed to initialize the DHCP server address.");
goto bail; goto bail;
@ -102,7 +89,7 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_reques
#else #else
int yes = 1; int yes = 1;
#endif #endif
if(setsockopt(clientFD, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(int))) if(setsockopt(localsocket4->fd, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(int)))
{ {
TSK_DEBUG_ERROR("Failed to enable broadcast option [errno=%d].", tnet_geterrno()); TSK_DEBUG_ERROR("Failed to enable broadcast option [errno=%d].", tnet_geterrno());
goto bail; goto bail;
@ -126,19 +113,14 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_reques
/* Set FD */ /* Set FD */
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(clientFD, &set); FD_SET(localsocket4->fd, &set);
/* ciaddr */ /* ciaddr */
if(request->type == dhcp_type_inform){ if(request->type == dhcp_type_inform){
struct sockaddr_storage ss; struct sockaddr_storage ss;
if(!tnet_get_sockaddr(clientFD, &ss)){ if(!tnet_get_sockaddr(localsocket4->fd, &ss)){
if(ctx->use_ipv6){ uint32_t addr = htonl((*((uint32_t*)&((struct sockaddr_in*)&ss)->sin_addr)));
// FIXME memcpy(&request->ciaddr, &addr, 4);
}
else{
uint32_t addr = htonl((*((uint32_t*)&((struct sockaddr_in*)&ss)->sin_addr)));
memcpy(&request->ciaddr, &addr, 4);
}
} }
} }
@ -154,7 +136,7 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_reques
goto next_iface; goto next_iface;
} }
/* Send the request to the DHCP server */ /* Send the request to the DHCP server */
if((ret =tnet_sockfd_sendto(clientFD, (const struct sockaddr*)&server, output->data, output->size))<0) if((ret =tnet_sockfd_sendto(localsocket4->fd, (const struct sockaddr*)&server, output->data, output->size))<0)
{ {
TNET_PRINT_LAST_ERROR("Failed to send DHCP request."); TNET_PRINT_LAST_ERROR("Failed to send DHCP request.");
@ -162,7 +144,7 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_reques
goto next_iface; goto next_iface;
} }
/* wait for response */ /* wait for response */
if((ret = select(clientFD+1, &set, NULL, NULL, &tv))<0) if((ret = select(localsocket4->fd+1, &set, NULL, NULL, &tv))<0)
{ /* Error */ { /* Error */
TNET_PRINT_LAST_ERROR("select have failed."); TNET_PRINT_LAST_ERROR("select have failed.");
tsk_thread_sleep(150); // wait 150ms before trying the next iface. tsk_thread_sleep(150); // wait 150ms before trying the next iface.
@ -177,14 +159,14 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_reques
void* data = 0; void* data = 0;
/* Check how how many bytes are pending */ /* Check how how many bytes are pending */
if((ret = tnet_ioctlt(clientFD, FIONREAD, &len))<0) if((ret = tnet_ioctlt(localsocket4->fd, FIONREAD, &len))<0)
{ {
goto next_iface; goto next_iface;
} }
/* Receive pending data */ /* Receive pending data */
data = tsk_calloc(len, sizeof(uint8_t)); data = tsk_calloc(len, sizeof(uint8_t));
if((ret = tnet_sockfd_recv(clientFD, data, len, 0))<0) if((ret = tnet_sockfd_recv(localsocket4->fd, data, len, 0))<0)
{ {
TSK_FREE(data); TSK_FREE(data);
@ -217,7 +199,6 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_reques
bail: bail:
TSK_OBJECT_SAFE_FREE(localsocket4); TSK_OBJECT_SAFE_FREE(localsocket4);
TSK_OBJECT_SAFE_FREE(localsocket6);
return reply; return reply;
} }
@ -278,6 +259,7 @@ static void* tnet_dhcp_ctx_create(void * self, va_list * app)
ctx->hostname = tsk_strndup(host, strlen(host)); ctx->hostname = tsk_strndup(host, strlen(host));
} }
ctx->timeout = TNET_DHCP_TIMEOUT_DEFAULT; ctx->timeout = TNET_DHCP_TIMEOUT_DEFAULT;
ctx->max_msg_size = TNET_DHCP_MAX_MSG_SIZE;
ctx->port_client = TNET_DHCP_CLIENT_PORT; ctx->port_client = TNET_DHCP_CLIENT_PORT;
ctx->server_port = TNET_DHCP_SERVER_PORT; ctx->server_port = TNET_DHCP_SERVER_PORT;
ctx->interfaces = tnet_get_interfaces(); ctx->interfaces = tnet_get_interfaces();

View File

@ -21,7 +21,7 @@
*/ */
/**@file tnet_dhcp.h /**@file tnet_dhcp.h
* @brief DHCP (RFC 2131 - Dynamic Host Configuration Protocol) utilities function for P-CSCF discovery(RFC 3319 and 3361) * @brief DHCP (RFC 2131 - Dynamic Host Configuration Protocol) utilities function for P-CSCF discovery(RFC 3319 and 3361)
* Also implement: RFC 3315, 3319, ... * Also implement: RFC 3315, 3118, 3319, 3825 (Geoconf), 4676 (Civic Addresses Configuration Information)...
* *
* @author Mamadou Diop <diopmamadou(at)yahoo.fr> * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
* *
@ -56,6 +56,7 @@ TNET_BEGIN_DECLS
#define TNET_DHCP_VENDOR_ID_DEFAULT "doubango/v0.0.0" #define TNET_DHCP_VENDOR_ID_DEFAULT "doubango/v0.0.0"
#define TNET_DHCP_MAX_CODES 20 #define TNET_DHCP_MAX_CODES 20
#define TNET_DHCP_MAX_MSG_SIZE 1500
/** Parameter Request List (55) /** Parameter Request List (55)
*/ */
@ -74,11 +75,10 @@ typedef struct tnet_dhcp_ctx_s
char* vendor_id; char* vendor_id;
char* hostname; char* hostname;
uint16_t max_msg_size; /**< Option code 57. */
uint64_t timeout; uint64_t timeout;
unsigned use_ipv6;
tnet_port_t port_client; /**< Local port to bind to for incloming DHCP messages. Default: 68 */ tnet_port_t port_client; /**< Local port to bind to for incloming DHCP messages. Default: 68 */
tnet_port_t server_port; /**< Destination port for outgoing DHCP messages. Default: 64 */ tnet_port_t server_port; /**< Destination port for outgoing DHCP messages. Default: 64 */
tnet_interfaces_L_t *interfaces; tnet_interfaces_L_t *interfaces;

View File

@ -126,6 +126,17 @@ tsk_buffer_t* tnet_dhcp_message_serialize(const struct tnet_dhcp_ctx_s *ctx, con
tnet_dhcp_option_serializeex(dhcp_code_Class_Id, strlen(ctx->vendor_id), ctx->vendor_id, output); tnet_dhcp_option_serializeex(dhcp_code_Class_Id, strlen(ctx->vendor_id), ctx->vendor_id, output);
} }
/*== RFC 2132 - 9.10. Maximum DHCP Message Size (57)
Code Len Length
+-----+-----+-----+-----+
| 57 | 2 | l1 | l2 |
+-----+-----+-----+-----+
*/
if(TNET_DHCP_MESSAGE_IS_REQUEST(message) && ctx->max_msg_size){
_2bytes = ntohs(ctx->max_msg_size);
tnet_dhcp_option_serializeex(dhcp_code_DHCP_Max_Msg_Size, 2, &_2bytes, output);
}
/*== DHCP Options /*== DHCP Options
*/ */
{ {

View File

@ -65,7 +65,7 @@ static void* tnet_dhcp_option_sip4_create(void * self, va_list * app)
|'e'|'x'|'a'|'m'|'p'|'l'|'e'| 3 |'n'|'e'|'t'| 0 | +---+---+--- |'e'|'x'|'a'|'m'|'p'|'l'|'e'| 3 |'n'|'e'|'t'| 0 | +---+---+---
+---+---+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+
*/ */
size_t offset = 0; size_t offset = 1;
char* server = 0; char* server = 0;
payloadPtr++; payloadPtr++;
while((payloadPtr < payloadEnd) && !tnet_dns_rr_qname_deserialize(payload, (payloadEnd - payloadPtr), &server, &offset)) while((payloadPtr < payloadEnd) && !tnet_dns_rr_qname_deserialize(payload, (payloadEnd - payloadPtr), &server, &offset))
@ -74,7 +74,6 @@ static void* tnet_dhcp_option_sip4_create(void * self, va_list * app)
tsk_list_push_back_data(option->servers, (void*)&string); tsk_list_push_back_data(option->servers, (void*)&string);
TSK_FREE(server); TSK_FREE(server);
payloadPtr += offset; payloadPtr += offset;
break;// FIXME ==> check that the while loop is correct
} }
} }
else{ else{

View File

@ -0,0 +1,30 @@
/*
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou@yahoo.fr>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
/**@file tnet_dhcp6.c
* @brief Dynamic Host Configuration Protocol for IPv6 (DHCPv6) as per RFC 3315.
* Also implement: RFC 3319, 3633, 3646, 3736, 4242, 5007 ...
*
* @author Mamadou Diop <diopmamadou(at)yahoo.fr>
*
* @date Created: Sat Nov 8 16:54:58 2009 mdiop
*/
#include "tnet_dhcp6.h"

View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou@yahoo.fr>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
/**@file tnet_dhcp6.h
* @brief Dynamic Host Configuration Protocol for IPv6 (DHCPv6) as per RFC 3315.
* Also implement: RFC 3319, 3633, 3646, 3736, 4242, 5007 ...
*
* @author Mamadou Diop <diopmamadou(at)yahoo.fr>
*
* @date Created: Sat Nov 8 16:54:58 2009 mdiop
*/
#ifndef TNET_DHCP6_H
#define TNET_DHCP6_H
#include "tinyNET_config.h"
TNET_BEGIN_DECLS
TNET_END_DECLS
#endif /* TNET_DHCP6_H */

View File

@ -109,7 +109,7 @@ bail:
TSK_OBJECT_SAFE_FREE(reply); TSK_OBJECT_SAFE_FREE(reply);
TSK_OBJECT_SAFE_FREE(params); TSK_OBJECT_SAFE_FREE(params);
tsk_thread_sleep(1000); //tsk_thread_sleep(1000);
} }
void test_dhcp() void test_dhcp()

View File

@ -499,6 +499,14 @@
> >
</File> </File>
</Filter> </Filter>
<Filter
Name="dhcp6"
>
<File
RelativePath="..\..\tinyNET\src\dhcp6\tnet_dhcp6.c"
>
</File>
</Filter>
</Filter> </Filter>
<Filter <Filter
Name="include" Name="include"
@ -663,6 +671,14 @@
> >
</File> </File>
</Filter> </Filter>
<Filter
Name="dhcp6"
>
<File
RelativePath="..\..\tinyNET\src\dhcp6\tnet_dhcp6.h"
>
</File>
</Filter>
</Filter> </Filter>
</Files> </Files>
<Globals> <Globals>