Update DHCP stack.
This commit is contained in:
parent
f019daef63
commit
e70de9b263
|
@ -29,21 +29,25 @@
|
|||
*/
|
||||
#include "tnet_dhcp.h"
|
||||
|
||||
#include "tsk_memory.h"
|
||||
#include "tsk_time.h"
|
||||
#include "tsk_debug.h"
|
||||
|
||||
// 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/
|
||||
|
||||
|
||||
tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, const tnet_dhcp_request_t* request)
|
||||
tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_request_t* request)
|
||||
{
|
||||
tsk_buffer_t *output = 0;
|
||||
tsk_buffer_t *output;
|
||||
tnet_dhcp_reply_t* reply = 0;
|
||||
int ret;
|
||||
struct timeval tv;
|
||||
fd_set set;
|
||||
tnet_fd_t clientFD = TNET_INVALID_FD;
|
||||
uint64_t timeout = 0;
|
||||
tsk_list_item_t *item;
|
||||
const tnet_interface_t *iface;
|
||||
|
||||
tnet_socket_t *localsocket4 = 0;
|
||||
tnet_socket_t *localsocket6 = 0;
|
||||
|
@ -54,16 +58,9 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, const tnet_dhcp_
|
|||
goto bail;
|
||||
}
|
||||
|
||||
/* Serialize and send to the server. */
|
||||
if(!(output = tnet_dhcp_message_serialize(request)))
|
||||
{
|
||||
TSK_DEBUG_ERROR("Failed to serialize the DHCP message.");
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if(ctx->use_ipv6)
|
||||
{
|
||||
localsocket6 = TNET_SOCKET_CREATE(TNET_SOCKET_HOST_ANY, ctx->port_client, tnet_socket_type_udp_ipv6);
|
||||
localsocket6 = TNET_SOCKET_CREATE("::", ctx->port_client, tnet_socket_type_udp_ipv6);
|
||||
if(TNET_SOCKET_IS_VALID(localsocket6))
|
||||
{
|
||||
clientFD = localsocket6->fd;
|
||||
|
@ -72,7 +69,7 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, const tnet_dhcp_
|
|||
}
|
||||
else
|
||||
{
|
||||
localsocket4 = TNET_SOCKET_CREATE(TNET_SOCKET_HOST_ANY, ctx->port_client, tnet_socket_type_udp_ipv4);
|
||||
localsocket4 = TNET_SOCKET_CREATE("0.0.0.0", ctx->port_client, tnet_socket_type_udp_ipv4);
|
||||
if(TNET_SOCKET_IS_VALID(localsocket4))
|
||||
{
|
||||
clientFD = localsocket4->fd;
|
||||
|
@ -88,40 +85,126 @@ tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, const tnet_dhcp_
|
|||
FD_ZERO(&set);
|
||||
FD_SET(clientFD, &set);
|
||||
|
||||
if(tnet_sockaddr_init("255.255.255.255"/* FIXME: IPv6 */, ctx->server_port, (ctx->use_ipv6 == AF_INET ? tnet_socket_type_udp_ipv4 : tnet_socket_type_udp_ipv6), &server))
|
||||
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))
|
||||
{
|
||||
TSK_DEBUG_ERROR("Failed to connect to initialize the DHCP server address.");
|
||||
TSK_DEBUG_ERROR("Failed to connect to initialize the DHCP server address [errno=%d].", tnet_geterrno());
|
||||
//TNET_PRINT_LAST_ERROR();
|
||||
//continue;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if((ret =tnet_sockfd_sendto(localsocket6->fd, (const struct sockaddr*)&server, output->data, output->size))<0)
|
||||
{
|
||||
TSK_DEBUG_ERROR("Failed to send the DHCP request to the remote peer [%d].", ret);
|
||||
goto bail;
|
||||
/* ENABLE BROADCASTING */
|
||||
{
|
||||
#if defined(SOLARIS)
|
||||
char yes = '1';
|
||||
#else
|
||||
int yes = 1;
|
||||
#endif
|
||||
if(setsockopt(clientFD, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(int)))
|
||||
{
|
||||
TSK_DEBUG_ERROR("Failed to enable broadcast option [errno=%d].", tnet_geterrno());
|
||||
goto bail;
|
||||
}
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
tsk_list_foreach(item, ctx->interfaces)
|
||||
{
|
||||
iface = item->data;
|
||||
|
||||
/* chaddr */
|
||||
memset(request->chaddr, 0, sizeof(request->chaddr));
|
||||
request->hlen = iface->mac_address_length > sizeof(request->chaddr) ? sizeof(request->chaddr) : iface->mac_address_length;
|
||||
memcpy(request->chaddr, iface->mac_address, request->hlen);
|
||||
|
||||
/* Serialize and send to the server. */
|
||||
if(!(output = tnet_dhcp_message_serialize(request)))
|
||||
{
|
||||
TSK_DEBUG_ERROR("Failed to serialize the DHCP message.");
|
||||
goto next_iface;
|
||||
}
|
||||
/* Send the request to the DHCP server */
|
||||
if((ret =tnet_sockfd_sendto(clientFD, (const struct sockaddr*)&server, output->data, output->size))<0)
|
||||
{
|
||||
TSK_DEBUG_ERROR("Failed to send the DHCP request to the remote peer [%d] [errno=%d].", ret, tnet_geterrno());
|
||||
//TNET_PRINT_LAST_ERROR();
|
||||
goto next_iface;
|
||||
}
|
||||
/* wait for response */
|
||||
if((ret = select(clientFD+1, &set, NULL, NULL, &tv))<0)
|
||||
{ /* Error */
|
||||
goto next_iface;
|
||||
}
|
||||
else if(ret == 0)
|
||||
{ /* timeout ==> do nothing */
|
||||
}
|
||||
else
|
||||
{ /* there is data to read */
|
||||
size_t len = 0;
|
||||
void* data = 0;
|
||||
|
||||
/* Check how how many bytes are pending */
|
||||
if((ret = tnet_ioctlt(clientFD, FIONREAD, &len))<0)
|
||||
{
|
||||
goto next_iface;
|
||||
}
|
||||
|
||||
/* Receive pending data */
|
||||
data = tsk_calloc(len, sizeof(uint8_t));
|
||||
if((ret = tnet_sockfd_recv(clientFD, data, len, 0))<0)
|
||||
{
|
||||
TSK_FREE(data);
|
||||
|
||||
TSK_DEBUG_ERROR("Recving DHCP dgrams failed with error code:%d", tnet_geterrno());
|
||||
goto next_iface;
|
||||
}
|
||||
|
||||
/* Parse the incoming response. */
|
||||
reply = tnet_dhcp_message_deserialize(data, len);
|
||||
TSK_FREE(data);
|
||||
|
||||
if(reply)
|
||||
{ /* response successfuly parsed */
|
||||
if(request->xid != reply->xid)
|
||||
{ /* Not same transaction id ==> continue*/
|
||||
TSK_OBJECT_SAFE_FREE(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next_iface:
|
||||
TSK_OBJECT_SAFE_FREE(output);
|
||||
if(reply){
|
||||
goto bail;
|
||||
}
|
||||
}
|
||||
|
||||
/* First time? ==> set timeout value */
|
||||
if(!timeout) timeout = tsk_time_epoch() + ctx->timeout;
|
||||
}
|
||||
while(timeout < tsk_time_epoch());
|
||||
|
||||
bail:
|
||||
TSK_OBJECT_SAFE_FREE(localsocket4);
|
||||
TSK_OBJECT_SAFE_FREE(localsocket6);
|
||||
|
||||
TSK_OBJECT_SAFE_FREE(output);
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
tnet_dhcp_reply_t* tnet_dhcp_query(tnet_dhcp_ctx_t* ctx, tnet_dhcp_params_t* params)
|
||||
{
|
||||
tnet_dhcp_reply_t* reply = 0;
|
||||
tnet_dhcp_request_t* request = TNET_DHCP_MESSAGE_CREATE();
|
||||
tnet_dhcp_request_t* request = TNET_DHCP_REQUEST_CREATE();
|
||||
|
||||
if(!ctx || !params || !request)
|
||||
{
|
||||
goto bail;
|
||||
}
|
||||
|
||||
request->op = dhcp_op_bootrequest;
|
||||
|
||||
//request->op = params->tag;
|
||||
request->type = dhcp_type_discover; // FIXME
|
||||
reply = tnet_dhcp_send_request(ctx, request);
|
||||
|
||||
bail:
|
||||
TSK_OBJECT_SAFE_FREE(request);
|
||||
|
@ -145,7 +228,12 @@ static void* tnet_dhcp_ctx_create(void * self, va_list * app)
|
|||
ctx->timeout = TNET_DHCP_TIMEOUT_DEFAULT;
|
||||
ctx->port_client = TNET_DHCP_CLIENT_PORT;
|
||||
ctx->server_port = TNET_DHCP_SERVER_PORT;
|
||||
//...
|
||||
ctx->interfaces = tnet_get_interfaces();
|
||||
|
||||
if(!ctx->interfaces || TSK_LIST_IS_EMPTY(ctx->interfaces))
|
||||
{
|
||||
TSK_DEBUG_ERROR("Failed to retrieve network interfaces.");
|
||||
}
|
||||
|
||||
tsk_safeobj_init(ctx);
|
||||
}
|
||||
|
@ -159,7 +247,7 @@ static void* tnet_dhcp_ctx_destroy(void * self)
|
|||
{
|
||||
tsk_safeobj_deinit(ctx);
|
||||
|
||||
//...
|
||||
TSK_OBJECT_SAFE_FREE(ctx->interfaces);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -172,3 +260,34 @@ static const tsk_object_def_t tnet_dhcp_ctx_def_s =
|
|||
0,
|
||||
};
|
||||
const void *tnet_dhcp_ctx_def_t = &tnet_dhcp_ctx_def_s;
|
||||
|
||||
//========================================================
|
||||
// [[DHCP PARAMS]] object definition
|
||||
//
|
||||
static void* tnet_dhcp_params_create(void * self, va_list * app)
|
||||
{
|
||||
tnet_dhcp_params_t *params = self;
|
||||
if(params)
|
||||
{
|
||||
params->tag = va_arg(*app, tnet_dhcp_option_tag_t);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
static void* tnet_dhcp_params_destroy(void * self)
|
||||
{
|
||||
tnet_dhcp_params_t *params = self;
|
||||
if(params)
|
||||
{
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
static const tsk_object_def_t tnet_dhcp_params_def_s =
|
||||
{
|
||||
sizeof(tnet_dhcp_params_t),
|
||||
tnet_dhcp_params_create,
|
||||
tnet_dhcp_params_destroy,
|
||||
0,
|
||||
};
|
||||
const void *tnet_dhcp_params_def_t = &tnet_dhcp_params_def_s;
|
||||
|
|
|
@ -42,7 +42,8 @@
|
|||
|
||||
TNET_BEGIN_DECLS
|
||||
|
||||
#define TNET_DHCP_CTX_CREATE() tsk_object_new(tnet_dhcp_ctx_def_t)
|
||||
#define TNET_DHCP_CTX_CREATE() tsk_object_new(tnet_dhcp_ctx_def_t)
|
||||
#define TNET_DHCP_PARAMS_CREATE(tag) tsk_object_new(tnet_dhcp_params_def_t, (tnet_dhcp_option_tag_t)tag)
|
||||
|
||||
/** Default timeout (in milliseconds) value for DHCP requests.
|
||||
*/
|
||||
|
@ -51,7 +52,7 @@ TNET_BEGIN_DECLS
|
|||
/**< Local port(client) to bind to for incoming DHCP messages as per RFC 2131 subclause 4.1. */
|
||||
#define TNET_DHCP_CLIENT_PORT 68
|
||||
/**< Destination port(Server) for outgoing DHCP messages as per RFC 2131 subclause 4.1. */
|
||||
#define TNET_DHCP_SERVER_PORT 64
|
||||
#define TNET_DHCP_SERVER_PORT 67
|
||||
|
||||
|
||||
typedef struct tnet_dhcp_params_s
|
||||
|
@ -72,13 +73,14 @@ typedef struct tnet_dhcp_ctx_s
|
|||
|
||||
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_interfaces_L_t *interfaces;
|
||||
|
||||
TSK_DECLARE_SAFEOBJ;
|
||||
}
|
||||
tnet_dhcp_ctx_t;
|
||||
|
||||
|
||||
tnet_dhcp_reply_t* tnet_dhcp_query(tnet_dhcp_ctx_t* ctx, tnet_dhcp_params_t* params);
|
||||
TINYNET_API tnet_dhcp_reply_t* tnet_dhcp_query(tnet_dhcp_ctx_t* ctx, tnet_dhcp_params_t* params);
|
||||
|
||||
TINYNET_GEXTERN const void *tnet_dhcp_ctx_def_t;
|
||||
TINYNET_GEXTERN const void *tnet_dhcp_params_def_t;
|
||||
|
|
|
@ -28,10 +28,34 @@
|
|||
*/
|
||||
#include "tnet_dhcp_message.h"
|
||||
|
||||
#include "../tnet_utils.h"
|
||||
|
||||
int tnet_dhcp_message_serialize_type(const tnet_dhcp_message_t *message, tsk_buffer_t* output)
|
||||
{
|
||||
uint8_t _1byte;
|
||||
if(message && output)
|
||||
{
|
||||
/* Option */
|
||||
_1byte = dhcp_tag_DHCP_Msg_Type;
|
||||
tsk_buffer_append(output, &(_1byte), 1);
|
||||
/* Length */
|
||||
_1byte = 1;
|
||||
tsk_buffer_append(output, &(_1byte), 1);
|
||||
/* Value */
|
||||
_1byte = message->type;
|
||||
tsk_buffer_append(output, &(_1byte), 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
tsk_buffer_t* tnet_dhcp_message_serialize(const tnet_dhcp_message_t *message)
|
||||
{
|
||||
tsk_buffer_t* output = 0;
|
||||
uint8_t _1byte;
|
||||
uint16_t _2bytes;
|
||||
uint32_t _4bytes;
|
||||
|
||||
/* Check message validity */
|
||||
if(!message)
|
||||
|
@ -41,6 +65,56 @@ tsk_buffer_t* tnet_dhcp_message_serialize(const tnet_dhcp_message_t *message)
|
|||
|
||||
output = TSK_BUFFER_CREATE_NULL();
|
||||
|
||||
/*== OP HTYPE HLEN HOPS */
|
||||
_4bytes = (((uint32_t)(message->op)) << 24) |
|
||||
(((uint32_t)(message->htype)) << 16) |
|
||||
(((uint16_t)(message->hlen)) << 8) | message->hops;
|
||||
_4bytes = ntohl(_4bytes);
|
||||
tsk_buffer_append(output, &(_4bytes), 4);
|
||||
|
||||
/*== XID */
|
||||
_4bytes = ntohl(message->xid);
|
||||
tsk_buffer_append(output, &(_4bytes), 4);
|
||||
/*== SECS */
|
||||
_2bytes = ntohs(message->secs);
|
||||
tsk_buffer_append(output, &(_2bytes), 2);
|
||||
/*== FLAGS */
|
||||
_2bytes = ntohs(message->flags);
|
||||
tsk_buffer_append(output, &(_2bytes), 2);
|
||||
/*== CIADDR */
|
||||
_4bytes = ntohl(message->ciaddr);
|
||||
tsk_buffer_append(output, &(_4bytes), 4);
|
||||
/*== YIADDR */
|
||||
_4bytes = ntohl(message->yiaddr);
|
||||
tsk_buffer_append(output, &(_4bytes), 4);
|
||||
/*== SIADDR */
|
||||
_4bytes = ntohl(message->siaddr);
|
||||
tsk_buffer_append(output, &(_4bytes), 4);
|
||||
/*== GIADDR */
|
||||
_4bytes = ntohl(message->giaddr);
|
||||
tsk_buffer_append(output, &(_4bytes), 4);
|
||||
/*== CHADDR */
|
||||
tsk_buffer_append(output, message->chaddr, sizeof(message->chaddr));
|
||||
/*== sname (unused) */
|
||||
tsk_buffer_append(output, message->sname, sizeof(message->sname));
|
||||
/*== file (unused) */
|
||||
tsk_buffer_append(output, message->file, sizeof(message->file));
|
||||
/*== Magic Cookie */
|
||||
_4bytes = ntohl(TNET_DHCP_MAGIC_COOKIE);
|
||||
tsk_buffer_append(output, &(_4bytes), 4);
|
||||
|
||||
/*== Message Type (option 53)
|
||||
*/
|
||||
tnet_dhcp_message_serialize_type(message, output);
|
||||
|
||||
/*== DHCP Options
|
||||
*/
|
||||
|
||||
|
||||
/*== DHCP Option End*/
|
||||
_1byte = dhcp_tag_End;
|
||||
tsk_buffer_append(output, &(_1byte), 1);
|
||||
|
||||
bail:
|
||||
return output;
|
||||
}
|
||||
|
@ -70,6 +144,10 @@ static void* tnet_dhcp_message_create(void * self, va_list * app)
|
|||
{
|
||||
static uint32_t __dhcpmessage_unique_xid = 0;
|
||||
|
||||
message->op = va_arg(*app, tnet_dhcp_message_op_t);
|
||||
message->htype = dhcp_htype_Ethernet_10Mb;
|
||||
message->hlen = 0x06;
|
||||
|
||||
message->xid = ++(__dhcpmessage_unique_xid);
|
||||
message->options = TSK_LIST_CREATE();
|
||||
}
|
||||
|
|
|
@ -38,7 +38,9 @@
|
|||
|
||||
TNET_BEGIN_DECLS
|
||||
|
||||
#define TNET_DHCP_MESSAGE_CREATE() tsk_object_new(tnet_dhcp_message_def_t)
|
||||
#define TNET_DHCP_MESSAGE_CREATE(opcode) tsk_object_new(tnet_dhcp_message_def_t, (tnet_dhcp_message_op_t)opcode)
|
||||
#define TNET_DHCP_REQUEST_CREATE() TNET_DHCP_MESSAGE_CREATE(dhcp_op_bootrequest)
|
||||
#define TNET_DHCP_REPLY_CREATE() TNET_DHCP_MESSAGE_CREATE(dhcp_op_bootreply)
|
||||
|
||||
#define TNET_DHCP_MAGIC_COOKIE 0x63825363 /**< DHCP magic cookie (99, 130, 83 and 99 in decimal).*/
|
||||
|
||||
|
@ -48,12 +50,12 @@ typedef enum tnet_dhcp_message_type_e
|
|||
{
|
||||
/**< DHCPDISCOVER - Client broadcast to locate available servers.
|
||||
*/
|
||||
dhcp_type_discover,
|
||||
dhcp_type_discover = 1,
|
||||
|
||||
/**< DHCPOFFER - Server to client in response to DHCPDISCOVER with
|
||||
offer of configuration parameters.
|
||||
*/
|
||||
dhcp_type_offer,
|
||||
dhcp_type_offer = 2,
|
||||
|
||||
/**< DHCPREQUEST - Client message to servers either (a) requesting
|
||||
offered parameters from one server and implicitly
|
||||
|
@ -62,37 +64,61 @@ typedef enum tnet_dhcp_message_type_e
|
|||
e.g., system reboot, or (c) extending the lease on a
|
||||
particular network address.
|
||||
*/
|
||||
dhcp_type_request,
|
||||
dhcp_type_request = 3,
|
||||
|
||||
/**< DHCPDECLINE - Client to server indicating network address is already
|
||||
in use.
|
||||
*/
|
||||
dhcp_type_decline = 4,
|
||||
|
||||
/**< DHCPACK - Server to client with configuration parameters,
|
||||
including committed network address.
|
||||
*/
|
||||
dhcp_type_ack,
|
||||
dhcp_type_ack = 5,
|
||||
|
||||
/**< DHCPNAK - Server to client indicating client's notion of network
|
||||
address is incorrect (e.g., client has moved to new
|
||||
subnet) or client's lease as expired
|
||||
*/
|
||||
dhcp_type_nack,
|
||||
|
||||
/**< DHCPDECLINE - Client to server indicating network address is already
|
||||
in use.
|
||||
*/
|
||||
dhcp_type_decline,
|
||||
dhcp_type_nack = 6,
|
||||
|
||||
/**< DHCPRELEASE - Client to server relinquishing network address and
|
||||
cancelling remaining lease.
|
||||
*/
|
||||
dhcp_type_release,
|
||||
dhcp_type_release = 7,
|
||||
|
||||
/**< DHCPINFORM - Client to server, asking only for local configuration
|
||||
parameters; client already has externally configured
|
||||
network address.
|
||||
*/
|
||||
dhcp_type_inform,
|
||||
dhcp_type_inform = 8,
|
||||
}
|
||||
tnet_dhcp_message_type_t;
|
||||
|
||||
/**
|
||||
* DHCP hardward types as per RFC 1340.
|
||||
*/
|
||||
typedef enum tnet_dhcp_message_htype_e
|
||||
{
|
||||
dhcp_htype_Ethernet_10Mb = 1, /**< Ethernet (10Mb) */
|
||||
dhcp_htype_Ethernet_3Mb = 2, /**< Experimental Ethernet (3Mb) */
|
||||
dhcp_htype_AX_25 = 3, /**< Amateur Radio AX.25 */
|
||||
dhcp_htype_Token_Ring = 4, /**< Proteon ProNET Token Ring */
|
||||
dhcp_htype_Chaos = 5, /**< Chaos */
|
||||
dhcp_htype_IEEE_802_Networks = 6, /**< IEEE 802 Networks */
|
||||
dhcp_htype_ARCNET = 7, /**< ARCNET */
|
||||
dhcp_htype_Hyperchannel = 8, /**< Hyperchannel */
|
||||
dhcp_htype_Lanstar = 9, /**< Lanstar */
|
||||
dhcp_htype_Autonet_Short_Address = 10, /**< Autonet Short Address */
|
||||
dhcp_htype_ALocalTalk = 11, /**< LocalTalk */
|
||||
dhcp_htype_LocalNet= 12, /**< LocalNet (IBM PCNet or SYTEK LocalNET) */
|
||||
dhcp_htype_Ultra_link = 13, /**< Ultra link */
|
||||
dhcp_htype_SMDS = 14, /**< SMDS */
|
||||
dhcp_htype_Frame_Relay = 15, /**< Frame Relay */
|
||||
dhcp_htype_ATM = 16, /**< Asynchronous Transmission Mode (ATM) */
|
||||
}
|
||||
tnet_dhcp_message_htype_t;
|
||||
|
||||
/** DHCP message OP code / message type.
|
||||
*/
|
||||
typedef enum tnet_dhcp_message_op_e
|
||||
|
@ -108,6 +134,10 @@ typedef struct tnet_dhcp_message_s
|
|||
{
|
||||
TSK_DECLARE_OBJECT;
|
||||
|
||||
/** DHCP message type. Mandatory.
|
||||
*/
|
||||
tnet_dhcp_message_type_t type;
|
||||
|
||||
/*
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
|
@ -149,8 +179,8 @@ typedef struct tnet_dhcp_message_s
|
|||
/**< Hardware address type, see ARP section in "Assigned Numbers" RFC; e.g., '1' = 10mb ethernet.
|
||||
For more information see RFC 1340.
|
||||
*/
|
||||
uint8_t htype;
|
||||
/**< Hardware address length (e.g. '6' for 10mb ethernet).
|
||||
tnet_dhcp_message_htype_t htype;
|
||||
/**< Hardware address length (e.g. '6' for 10mb ethernet). strlen(chaddr).
|
||||
*/
|
||||
uint8_t hlen;
|
||||
/**< Client sets to zero, optionally used by relay agents when booting via a relay agent.
|
||||
|
|
|
@ -92,7 +92,7 @@ static void* tnet_dhcp_option_create(void * self, va_list * app)
|
|||
tnet_dhcp_option_t *option = self;
|
||||
if(option)
|
||||
{
|
||||
tnet_dhcp_option_init(option, dhcp_tag_reserved);
|
||||
tnet_dhcp_option_init(option, dhcp_tag_null);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
|
|
@ -98,7 +98,7 @@ typedef enum tnet_dhcp_option_tag_e
|
|||
dhcp_tag_DHCP_Msg_Type= 53 ,/**< DHCP Msg Type 1 DHCP Message Type [RFC2132] */
|
||||
dhcp_tag_DHCP_Server_Id= 54 ,/**< DHCP Server Id 4 DHCP Server Identification [RFC2132] */
|
||||
dhcp_tag_Parameter_List= 55 ,/**< Parameter List N Parameter Request List [RFC2132] */
|
||||
dhcp_tag_DHCP_Message= 56 ,/**< DHCP Message N DHCP Error Message [RFC2132] */
|
||||
dhcp_tag_DHCP_Error_Message= 56 ,/**< DHCP Message N DHCP Error Message [RFC2132] */
|
||||
dhcp_tag_DHCP_Max_Msg_Size= 57 ,/**< DHCP Max Msg Size 2 DHCP Maximum Message Size [RFC2132] */
|
||||
dhcp_tag_Renewal_Time= 58 ,/**< Renewal Time 4 DHCP Renewal (T1) Time [RFC2132] */
|
||||
dhcp_tag_Rebinding_Time= 59 ,/**< Rebinding Time 4 DHCP Rebinding (T2) Time [RFC2132] */
|
||||
|
@ -209,7 +209,7 @@ typedef enum tnet_dhcp_option_tag_e
|
|||
dhcp_tag_Virtual_Subnet= 221 ,/**< Virtual Subnet Selection Option (Tentatively Assigned - 2005-06-23) */
|
||||
// 222-223 Unassigned [RFC3942]
|
||||
//224-254 Reserved (Private Use)
|
||||
dhcp_tag_reserved=224 ,
|
||||
dhcp_tag_null=224 ,
|
||||
dhcp_tag_End= 255 ,/**< End 0 None [RFC2132] */
|
||||
}
|
||||
tnet_dhcp_option_tag_t;
|
||||
|
|
|
@ -135,7 +135,7 @@ tnet_interfaces_L_t* tnet_get_interfaces()
|
|||
pAdapter = pAdapterInfo;
|
||||
while(pAdapter)
|
||||
{
|
||||
tnet_interface_t *iface = TNET_INTERFACE_CREATE(pAdapter->Description);
|
||||
tnet_interface_t *iface = TNET_INTERFACE_CREATE(pAdapter->Description, pAdapter->Address, pAdapter->AddressLength);
|
||||
tsk_list_push_back_data(ifaces, &(iface));
|
||||
|
||||
pAdapter = pAdapter->Next;
|
||||
|
@ -609,6 +609,11 @@ int tnet_sockfd_sendto(tnet_fd_t fd, const struct sockaddr *to, const void* buf,
|
|||
TSK_DEBUG_ERROR("Using invalid FD to send data.");
|
||||
return -1;
|
||||
}
|
||||
if(!buf || !size)
|
||||
{
|
||||
TSK_DEBUG_ERROR("Using invalid BUFFER.");
|
||||
return -2;
|
||||
}
|
||||
|
||||
#if TNET_HAVE_SS_LEN
|
||||
return sendto(fd, buf, size, 0, to, to->ss_len);
|
||||
|
@ -719,7 +724,15 @@ static void* tnet_interface_create(void * self, va_list * app)
|
|||
tnet_interface_t *iface = self;
|
||||
if(iface)
|
||||
{
|
||||
iface->name = tsk_strdup(va_arg(*app, const char*));
|
||||
const char* description = va_arg(*app, const char*);
|
||||
const uint8_t* mac_address = va_arg(*app, const uint8_t*);
|
||||
size_t mac_address_length = va_arg(*app, size_t);
|
||||
|
||||
iface->description = tsk_strdup(description);
|
||||
if((iface->mac_address = tsk_calloc(mac_address_length, sizeof(uint8_t)))){
|
||||
memcpy(iface->mac_address, mac_address, mac_address_length);
|
||||
}
|
||||
iface->mac_address_length = mac_address_length;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -729,7 +742,8 @@ static void* tnet_interface_destroy(void * self)
|
|||
tnet_interface_t *iface = self;
|
||||
if(iface)
|
||||
{
|
||||
TSK_FREE(iface->name);
|
||||
TSK_FREE(iface->description);
|
||||
TSK_FREE(iface->mac_address);
|
||||
}
|
||||
|
||||
return self;
|
||||
|
@ -739,10 +753,10 @@ static int tnet_interface_cmp(const void *if1, const void *if2)
|
|||
{
|
||||
const tnet_interface_t *iface1 = if1;
|
||||
const tnet_interface_t *iface2 = if2;
|
||||
|
||||
|
||||
if(iface1 && iface2)
|
||||
{
|
||||
return tsk_stricmp(iface1->name, iface1->name);
|
||||
return tsk_stricmp(iface1->description, iface1->description);
|
||||
}
|
||||
else if(!iface1 && !iface2) return 0;
|
||||
else return -1;
|
||||
|
|
|
@ -39,14 +39,16 @@
|
|||
|
||||
TNET_BEGIN_DECLS
|
||||
|
||||
#define TNET_INTERFACE_CREATE(name) tsk_object_new(tnet_interface_def_t, (const char*)name)
|
||||
#define TNET_ADDRESS_CREATE(ip) tsk_object_new(tnet_address_def_t, (const char*)ip)
|
||||
#define TNET_INTERFACE_CREATE(description, mac_address, mac_address_length) tsk_object_new(tnet_interface_def_t, (const char*)description, (const uint8_t*)mac_address, (size_t)mac_address_length)
|
||||
#define TNET_ADDRESS_CREATE(ip) tsk_object_new(tnet_address_def_t, (const char*)ip)
|
||||
|
||||
typedef struct tnet_interface_s
|
||||
{
|
||||
TSK_DECLARE_OBJECT;
|
||||
|
||||
char* name;
|
||||
|
||||
char* description;
|
||||
uint8_t* mac_address;
|
||||
size_t mac_address_length;
|
||||
}
|
||||
tnet_interface_t;
|
||||
typedef tsk_list_t tnet_interfaces_L_t;
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include "tnet_transport.h"
|
||||
#include "stun/tnet_stun.h"
|
||||
#include "dns/tnet_dns.h"
|
||||
#include "dhcp/tnet_dhcp.h"
|
||||
|
||||
#include "test_sockets.h"
|
||||
#include "test_transport.h"
|
||||
|
@ -37,18 +38,19 @@
|
|||
#include "test_nat.h"
|
||||
#include "test_ifaces.h"
|
||||
#include "test_dns.h"
|
||||
#include "test_dhcp.h"
|
||||
|
||||
#define RUN_TEST_LOOP 1
|
||||
|
||||
#define RUN_TEST_ALL 0
|
||||
#define RUN_TEST_SOCKETS 0 /* FIXME: Android */
|
||||
#define RUN_TEST_TRANSPORT 1
|
||||
#define RUN_TEST_TRANSPORT 0
|
||||
#define RUN_TEST_AUTH 0
|
||||
#define RUN_TEST_STUN 0
|
||||
#define RUN_TEST_NAT 0
|
||||
#define RUN_TEST_IFACES 0
|
||||
#define RUN_TEST_DNS 0
|
||||
#define RUN_TEST_DHCP 0
|
||||
#define RUN_TEST_DHCP 1
|
||||
|
||||
#ifdef _WIN32_WCE
|
||||
int _tmain(int argc, _TCHAR* argv[])
|
||||
|
@ -95,6 +97,10 @@ int main()
|
|||
test_dns();
|
||||
#endif
|
||||
|
||||
#if RUN_TEST_ALL || RUN_TEST_DHCP
|
||||
test_dhcp();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/* Cleanup the network stack */
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
#ifndef TNET_TEST_DHCP_H
|
||||
#define TNET_TEST_DHCP_H
|
||||
|
||||
void test_dhcp_discover(tnet_dhcp_ctx_t *ctx)
|
||||
{
|
||||
tnet_dhcp_params_t *params = TNET_DHCP_PARAMS_CREATE(dhcp_tag_null);
|
||||
|
||||
tnet_dhcp_reply_t *reply = tnet_dhcp_query(ctx, params);
|
||||
|
||||
TSK_OBJECT_SAFE_FREE(reply);
|
||||
TSK_OBJECT_SAFE_FREE(params);
|
||||
}
|
||||
|
||||
void test_dhcp_offer()
|
||||
{
|
||||
}
|
||||
|
||||
void test_dhcp_request()
|
||||
{
|
||||
}
|
||||
|
||||
void test_dhcp_inform()
|
||||
{
|
||||
}
|
||||
|
||||
void test_dhcp()
|
||||
{
|
||||
tnet_dhcp_ctx_t *ctx = TNET_DHCP_CTX_CREATE();
|
||||
test_dhcp_discover(ctx);
|
||||
|
||||
TSK_OBJECT_SAFE_FREE(ctx);
|
||||
}
|
||||
|
||||
#endif /* TNET_TEST_DHCP_H */
|
|
@ -30,7 +30,7 @@ void test_ifaces_dump_ifaces()
|
|||
tsk_list_foreach(item, ifaces)
|
||||
{
|
||||
const tnet_interface_t *iface = item->data;
|
||||
TSK_DEBUG_INFO("Interface: %s", iface->name);
|
||||
TSK_DEBUG_INFO("Interface: %s", iface->description);
|
||||
}
|
||||
|
||||
TSK_OBJECT_SAFE_FREE(ifaces);
|
||||
|
|
|
@ -370,6 +370,10 @@
|
|||
RelativePath="..\..\tinyNET\test\test_auth.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\tinyNET\test\test_dhcp.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\tinyNET\test\test_dns.h"
|
||||
>
|
||||
|
@ -390,10 +394,6 @@
|
|||
RelativePath="..\..\tinyNET\test\test_transport.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\tinyNET\test\tnet_dhcp.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
|
|
Loading…
Reference in New Issue