original files of m241

This commit is contained in:
neelimap
2026-07-30 11:19:44 +05:30
parent 170c2b3f3a
commit afc7070c1f
12254 changed files with 7655111 additions and 0 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,575 @@
/*
******************************************************************************
* INTERPEAK SOURCE FILE
*
*
* INTERPEAK_COPYRIGHT_STRING
*
* Design and implementation by Zhangqing Luo <Zhangqing.luo@windriver.com>
******************************************************************************
*/
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
#include "ipdhcpc_config.h"
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#ifdef IPDHCPC_INCLUDE_CMD
#define IPCOM_USE_CLIB_PROTO
#include <ipcom.h>
#include <ipcom_cstyle.h>
#include <ipcom_time.h>
#include <ipcom_err.h>
#include <ipcom_getopt.h>
#include <ipcom_inet.h>
#include <ipcom_os.h>
#include <ipcom_shell.h>
#include <ipcom_sock.h>
#include <ipcom_syslog.h>
#include <ipcom_tmo2.h>
#include <ipcom_type.h>
#include "ipdhcpc_h.h"
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
#define IPDHCPC_MAX_CMD (sizeof(ipdhcpc_cmd_callback)/sizeof(Ipdhcpc_cmd_cb))
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
typedef struct Ipdhcpc_cmd_cb_struct
{
char* cmd;
Ip_err(*cmd_cb)(int argc, char *argv[]);
}
Ipdhcpc_cmd_cb;
/*
****************************************************************************
* 6 EXTERN PROTOTYPES
****************************************************************************
*/
IP_EXTERN Ipdhcpc_data ipdhcpc;
/*
****************************************************************************
* 7 LOCAL PROTOTYPES
****************************************************************************
*/
IP_STATIC void ipdhcpc_printf (const char *format, /*args*/ ...);
IP_STATIC void ipdhcpc_errmsg_print (Ip_err code);
IP_STATIC void ipdhcpc_state_print (int state);
IP_STATIC void ipdhcpc_cmd_print_usage (void);
IP_STATIC Ip_err ipdhcpc_handle_cmd_renew (int argc, char *argv[]);
IP_STATIC Ip_err ipdhcpc_handle_cmd_inform (int argc, char *argv[]);
IP_STATIC Ip_err ipdhcpc_handle_cmd_interface (int argc, char *argv[]);
IP_STATIC Ip_err ipdhcpc_handle_cmd_lease (int argc, char *argv[]);
/*
****************************************************************************
* 8 DATA
****************************************************************************
*/
/*
*===========================================================================
* ipdhcpc_cmd_cb
*===========================================================================
* Description: Ipdhcpc command callback struct.
*/
IP_STATIC Ipdhcpc_cmd_cb ipdhcpc_cmd_callback[] =
{
{
"renew",
ipdhcpc_handle_cmd_renew
},
{
"inform",
ipdhcpc_handle_cmd_inform
},
{
"lease",
ipdhcpc_handle_cmd_lease
},
{
"interface",
ipdhcpc_handle_cmd_interface
}
};
/*
****************************************************************************
* 9 STATIC FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* ipdhcpc_printf
*===========================================================================
* Description: Format and print the message.
* Parameters: message formatted
* Returns: -
*
*/
IP_STATIC void
ipdhcpc_printf (const char *format, /*args*/ ...)
{
char *buf;
va_list ap;
Ip_s32 retval;
buf = ipcom_malloc(IPCOM_STDIO_BUFSIZE);
ip_assert(buf);
va_start(ap, format);
retval = ipcom_vsnprintf(buf, IPCOM_STDIO_BUFSIZE, format, ap);
retval = IP_MIN(retval, (int) IPCOM_STDIO_BUFSIZE);
va_end(ap);
if(retval > 0)
{
(void)ipcom_printf(buf);
}
ipcom_free(buf);
}
/*
*===========================================================================
* ipdhcpc_errmsg_print
*===========================================================================
* Description: Prints error messages in clear text.
* Parameters: code - error code
* Returns: -
*
*/
IP_STATIC void
ipdhcpc_errmsg_print(Ip_err code)
{
switch(code)
{
case IPCOM_ERR_INVALID_ARG:
ipdhcpc_printf("failed :: invalid argument"IP_LF);
break;
case IPCOM_ERR_INVALID_CONFIG:
ipdhcpc_printf("failed :: invalid config"IP_LF);
break;
case IPCOM_ERR_NOT_FOUND:
ipdhcpc_printf("failed :: not found"IP_LF);
break;
case IPCOM_ERR_NO_MEMORY:
ipdhcpc_printf("failed :: out of memory"IP_LF);
break;
case IPCOM_ERR_ALREADY_INSTALLED:
ipdhcpc_printf("failed :: already exists"IP_LF);
break;
case IPCOM_ERR_NOT_OPENED:
ipdhcpc_printf("failed :: ipdhcpc not open"IP_LF);
break;
case IPCOM_ERR_PERMISSION_DENIED:
ipdhcpc_printf("failed :: not allowed"IP_LF);
break;
case IPCOM_ERR_ALREADY_STARTED:
ipdhcpc_printf ("failed::already in processing");
break;
default:
break;
}
}
/*
*===========================================================================
* ipdhcpc_state_print
*===========================================================================
* Description: Prints state of the dhcp
* Parameters: state code
* Returns: -
*
*/
IP_STATIC void
ipdhcpc_state_print (int state)
{
switch(state)
{
case IPDHCPC_STATE_INIT:
ipdhcpc_printf(" State: Init"IP_LF);
break;
case IPDHCPC_STATE_INIT_REBOOT:
ipdhcpc_printf(" State: Init reboot"IP_LF);
break;
case IPDHCPC_STATE_SELECTING:
ipdhcpc_printf(" State: Selecting"IP_LF);
break;
case IPDHCPC_STATE_REBOOTING:
ipdhcpc_printf(" State: Rebooting"IP_LF);
break;
case IPDHCPC_STATE_REQUESTING:
ipdhcpc_printf(" State: Requesting"IP_LF);
break;
case IPDHCPC_STATE_BOUND:
ipdhcpc_printf(" State: Bound"IP_LF);
break;
case IPDHCPC_STATE_RENEWING:
ipdhcpc_printf(" State: Renewing"IP_LF);
break;
case IPDHCPC_STATE_REBINDING:
ipdhcpc_printf(" State: Rebinding"IP_LF);
break;
case IPDHCPC_STATE_STOPPED:
ipdhcpc_printf(" State: Stopped"IP_LF);
break;
case IPDHCPC_STATE_INFORMING:
ipdhcpc_printf(" State: Informing"IP_LF);
break;
default:
break;
}
}
/*
*===========================================================================
* ipdhcpc_handle_cmd_renew
*===========================================================================
* Description: Handles the renew command.
* Parameters: argc - number of command line arguments
* argv - array of command line arguments
*
* Returns: IPCOM_SUCCESS - the operation was successful
* IPCOM_ERR... - failed
*
*/
IP_STATIC Ip_err
ipdhcpc_handle_cmd_renew (int argc, char *argv[])
{
Ip_err retval = IPCOM_SUCCESS;
if (argc<1)
IPDHCPC_LEAVE(IPCOM_ERR_INVALID_ARG);
/*got the interface name*/
retval = ipdhcpc_renew_get (argv[0]);
ipdhcpc_printf(IP_LF);
ipdhcpc_errmsg_print(retval);
leave:
return retval;
}
/*
*===========================================================================
* ipdhcpc_handle_cmd_inform
*===========================================================================
* Description: Handles the inform command.
* Parameters: argc - number of command line arguments
* argv - array of command line arguments
*
* Returns: IPCOM_SUCCESS - the operation was successful
* IPCOM_ERR... - failed
*
*/
IP_STATIC Ip_err
ipdhcpc_handle_cmd_inform (int argc, char *argv[])
{
Ip_err retval = IPCOM_SUCCESS;
if (argc<1)
IPDHCPC_LEAVE(IPCOM_ERR_INVALID_ARG);
retval = ipdhcpc_inform_get (argv[0], argv[1]);
ipdhcpc_printf(IP_LF);
ipdhcpc_errmsg_print(retval);
leave:
return retval;
}
/*
*===========================================================================
* ipdhcpc_handle_cmd_interface
*===========================================================================
* Description: Handles the interface command.
* Parameters: argc - number of command line arguments
* argv - array of command line arguments
*
* Returns: IPCOM_SUCCESS - the operation was successful
* IPCOM_ERR... - failed
*
*/
IP_STATIC Ip_err
ipdhcpc_handle_cmd_interface (int argc, char *argv[])
{
Ip_err retval = IPCOM_SUCCESS;
Ipdhcpc_if *ift = IP_NULL;
(void)argc;
if (ipcom_strcmp ("list", argv[0])==0)
{
char * status = IP_NULL;
int i = 0;
IPDHCPC_LOCK();
/*show all interfaces status*/
for (ift = ipdhcpc.ift_head; ift != IP_NULL; ift = ift->next)
{
ipdhcpc_printf (""IP_LF);
ipdhcpc_printf ("Details about interface"IP_LF);
ipdhcpc_printf (" Name: %s"IP_LF, ift->ifname);
ipdhcpc_printf (" Index: %d"IP_LF,ift->ifindex);
ipdhcpc_printf (" HWaddr: ");
for (;i<ift->hlen-1; i++)
ipdhcpc_printf ("%x%x:",ift->chaddr[i]>>4, ift->chaddr[i]&0xf);
ipdhcpc_printf ("%x%x",ift->chaddr[i]>>4, ift->chaddr[i]&0xf);
ipdhcpc_printf (IP_LF);
status = ift->isdown?"Down":"Up";
ipdhcpc_printf (" Status: %s"IP_LF,status);
status = ift->information_only?"Enabled":"Disabled";
ipdhcpc_printf (" Information Only:%s"IP_LF,status);
if (ift->handle != IP_NULL)
/*show state*/
ipdhcpc_state_print (ift->handle->priv->state);
ipdhcpc_printf (""IP_LF);
}
IPDHCPC_UNLOCK();
}
else
retval = IPCOM_ERR_INVALID_ARG;
ipdhcpc_printf (IP_LF);
ipdhcpc_errmsg_print (retval);
return retval;
}
/*
*===========================================================================
* ipdhcpc_handle_cmd_lease
*===========================================================================
* Description: Handles the lease command.
* Parameters: argc - number of command line arguments
* argv - array of command line arguments
*
* Returns: IPCOM_SUCCESS - the operation was successful
* IPCOM_ERR... - failed
*
*/
IP_STATIC Ip_err
ipdhcpc_handle_cmd_lease (int argc, char *argv[])
{
Ip_err retval = IPCOM_SUCCESS;
Ipdhcpc_if *ift = IP_NULL;
struct Ip_in_addr addr;
(void)argc;
if (ipcom_strcmp ("list", argv[0])==0)
{
IPDHCPC_LOCK();
for (ift = ipdhcpc.ift_head; ift != IP_NULL; ift = ift->next)
{
if (ift->information_only == IP_TRUE)
{
ipdhcpc_printf (IP_LF);
ipdhcpc_printf (" Interface %s is in INFORMATION_ONLY MODE, no lease from server"IP_LF, ift->ifname);
}
else if (ift->handle != IP_NULL)
{
if (ift->handle->priv->state>=IPDHCPC_STATE_BOUND)
{
ipdhcpc_printf (""IP_LF);
ipdhcpc_printf ("Lease:"IP_LF);
ipdhcpc_printf (" Index: %d"IP_LF, ift->ifindex);
addr.s_addr = ift->handle->info.ipaddr_n;
ipdhcpc_printf (" Client address: %s"IP_LF, ipcom_inet_ntoa(addr));
addr.s_addr = ift->handle->info.serveraddr_n;
ipdhcpc_printf (" Server address: %s"IP_LF,ipcom_inet_ntoa(addr));
ipdhcpc_printf (" Duration: %ds"IP_LF, ift->lease.tlease);
ipdhcpc_printf (""IP_LF);
}
else
{
ipdhcpc_printf (IP_LF);
ipdhcpc_printf ("Performing dhcp");
ipdhcpc_state_print (ift->handle->priv->state);
}
}
else
ipdhcpc_printf ("Not started"IP_LF);
}
IPDHCPC_UNLOCK();
}
else
retval = IPCOM_ERR_INVALID_ARG;
ipdhcpc_printf (IP_LF);
ipdhcpc_errmsg_print (retval);
return retval;
}
/*
*===========================================================================
* ipdhcpc_cmd_print_usage
*===========================================================================
* Description: Prints the documentation for the command.
* Parameters:
* Returns: IPCOM_SUCCESS - the operation was successful
*
*
*/
IP_STATIC void
ipdhcpc_cmd_print_usage(void)
{
ipdhcpc_printf (IP_LF);
ipdhcpc_printf ("NAME" IP_LF);
ipdhcpc_printf (" dhcpc - Control Interpeak DHCP client"IP_LF);
ipdhcpc_printf (IP_LF);
ipdhcpc_printf ("SYNOPSIS" IP_LF);
ipdhcpc_printf (" dhcpc renew {name}"IP_LF);
ipdhcpc_printf (" dhcpc inform {name} [addr] "IP_LF);
ipdhcpc_printf (" dhcpc lease list "IP_LF);
ipdhcpc_printf (" dhcpc interface list"IP_LF);
ipdhcpc_printf (IP_LF);
ipdhcpc_printf ("DESCRIPTION" IP_LF);
ipdhcpc_printf (" renew Trigger renew event manually."IP_LF);
ipdhcpc_printf (" inform Trigger INFORM message. if no address set, select primary address as default"IP_LF);
ipdhcpc_printf (" lease List current data in lease database."IP_LF);
ipdhcpc_printf (" interface List interfaces used by the dhcp client."IP_LF);
ipdhcpc_printf (IP_LF);
ipdhcpc_printf ("EXAMPLES" IP_LF);
ipdhcpc_printf (" dhcpc renew eth0"IP_LF);
ipdhcpc_printf (" dhcpc inform eth0 128.224.158.82"IP_LF);
ipdhcpc_printf (" dhcpc inform eth0"IP_LF);
ipdhcpc_printf (" dhcpc lease list"IP_LF);
ipdhcpc_printf (" dhcpc interface list"IP_LF);
}
/*
****************************************************************************
* 10 GLOBAL FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* ipdhcpc_cmd_dhcpc
*===========================================================================
* Description: Shell command used to start,stop and configure the DHCP
* server.
* Parameters: argc - The number of arguments passed to the command.
* argv - The arguments passed to the command.
* Returns: 0 = success,
*
*/
IP_GLOBAL int
ipdhcpc_cmd_dhcpc(int argc, char **argv)
{
Ip_err retval;
int i;
retval = IPCOM_ERR_INVALID_ARG;
if(argc < 2)
IPDHCPC_LEAVE(IPCOM_ERR_INVALID_ARG);
for(i = 0; i < (int)IPDHCPC_MAX_CMD; i++)
{
if(ipcom_strcmp(argv[1], ipdhcpc_cmd_callback[i].cmd) == 0)
{
retval = ipdhcpc_cmd_callback[i].cmd_cb(argc-2, &argv[2]);
break;
}
}
leave:
if(retval == IPCOM_ERR_INVALID_ARG)
ipdhcpc_cmd_print_usage();
return retval;
}
/*
****************************************************************************
* 11 PUBLIC FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
/* #ifdef IPDHCPC_INCLUDE_CMD */
#endif
@@ -0,0 +1,532 @@
/*
******************************************************************************
* HEADER FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipdhcpc_h.h,v $ $Revision: 1.6.16.7 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/ipdhcpc_h.h,v $
* $Author: jwang9 $ $Date: 2016-01-04 08:50:32 $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Lennart Bang <lob@interpeak.se>
* and Jonas Rendel <jonas@interpeak.se>
******************************************************************************
*/
#ifndef IPDHCPC_H_H
#define IPDHCPC_H_H
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#include <ipcom_type.h>
#include <ipcom_cstyle.h>
#include <ipcom_tmo.h>
#include <ipcom_os.h>
#include <ipcom_tags.h>
#include "ipdhcpc.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/* DHCP pkt opcode: */
#define IPDHCPC_OP_BOOTREQUEST 1
#define IPDHCPC_OP_BOOTREPLY 2
/* DHCP pkt htype & hlen: */
#define IPDHCPC_HTYPE_ETHERNET 1
#define IPDHCPC_HLEN_ETHERNET 6
/* DHCP pkt message types: */
#define IPDHCPC_MSG_INVALID 0
#define IPDHCPC_MSG_DISCOVER 1
#define IPDHCPC_MSG_OFFER 2
#define IPDHCPC_MSG_REQUEST 3
#define IPDHCPC_MSG_DECLINE 4
#define IPDHCPC_MSG_ACK 5
#define IPDHCPC_MSG_NAK 6
#define IPDHCPC_MSG_RELEASE 7
#define IPDHCPC_MSG_INFORM 8
/* DHCP client message bitvalues: */
#define IPDHCPC_MB_DISCOVER 0x01
#define IPDHCPC_MB_OFFER 0x02
#define IPDHCPC_MB_REQUEST 0x04
#define IPDHCPC_MB_DECLINE 0x08
#define IPDHCPC_MB_ACK 0x10
#define IPDHCPC_MB_NAK 0x20
#define IPDHCPC_MB_RELEASE 0x40
#define IPDHCPC_MB_INFORM 0x80
/* DHCP client states: */
#define IPDHCPC_STATE_INIT 1
#define IPDHCPC_STATE_INIT_REBOOT 2
#define IPDHCPC_STATE_SELECTING 3
#define IPDHCPC_STATE_REBOOTING 4
#define IPDHCPC_STATE_REQUESTING 5
#define IPDHCPC_STATE_INFORMING 6
#define IPDHCPC_STATE_BOUND 7
#define IPDHCPC_STATE_RENEWING 8
#define IPDHCPC_STATE_REBINDING 9
#define IPDHCPC_STATE_STOPPED 10
#define IPDHCPC_STATE_ASSIGNING 11
/*DHCP event states*/
#define IPDHCPC_IF_EVENT_INFORM 1
#define IPDHCPC_IF_EVENT_RENEW 2
/* DHCP ports. */
#define IPDHCPC_SERVER_PORT 67
#define IPDHCPC_CLIENT_PORT 68
/* Minimum BOOTP header length. RFC1542:p5 */
#define IPDHCPC_PKT_MINLEN 300
#define IPDHCPC_RAW_HSIZE (sizeof(Ipcom_udp_header) + sizeof(Ipcom_ip_header))
/* DHCP Header size without options, as per RFC 2131 */
#define IPDHCPC_MESSAGE_DHCP_HEADER_SIZE ip_offsetof(Ipdhcpc_pkt, options)
/* DHCP Header size including IP, UDP, DHCP */
#define IPDHCPC_MESSAGE_HEADER_SIZE (IPDHCPC_RAW_HSIZE + IPDHCPC_MESSAGE_DHCP_HEADER_SIZE)
/* DHCP Max size of one option. Arbitrary. Used to alloc buffer holding options from sysvar struct */
#define IPDHCPC_MAX_OPTION_SIZE (ipdhcpc.max_message_size - IPDHCPC_MESSAGE_HEADER_SIZE)
#define IPDHCPC_LEFT_OPTION_SPACE(occupy_byte) (IPDHCPC_MAX_OPTION_SIZE - occupy_byte)
/*
*===========================================================================
* DHCP FLAGS FIELD
*===========================================================================
* See RFC 2132.
*/
#define IPDHCPC_FLAGS_BROADCAST 0X8000
/*
* DHCP mode of operation
*===========================================================================
* Defines the different modes the client can operate in.
*/
#define IPDHCPC_MODE_ALLOC_DYNAMIC 1
#define IPDHCPC_MODE_REUSE_DYNAMIC 2
#define IPDHCPC_MODE_RELEASE_DYNAMIC 3
#define IPDHCPC_MODE_INFORM_STATIC 4
/* overload option value */
#define IPDHCPC_OVERLOAD_NONE (0)
#define IPDHCPC_OVERLOAD_FILE (1)
#define IPDHCPC_OVERLOAD_SNAME (2)
#define IPDHCPC_OVERLOAD_BOTH (3)
#define IPDHCPC_OPT_CODE (0)
#define IPDHCPC_OPT_LEN (1)
#define IPDHCPC_OPT_DATA (2)
#define IPDHCPC_OPT_MIN_LEN (2)
#define IPDHCPC_PKT_HDR_SNAME_LEN (64)
#define IPDHCPC_PKT_HDR_FILE_LEN (128)
/* MACROS */
#define IPDHCPC_LEAVE(xerrval) do { retval = xerrval; goto leave; } while(0)
#define IPDHCPC_ISLOCKED_ASSERT() ip_assert(ipdhcpc.code_mutex_count == 1)
#define IPDHCPC_LOCK() do { ipcom_mutex_lock(ipdhcpc.code_mutex); \
ip_assert(ipdhcpc.code_mutex_count == 0); \
ipdhcpc.code_mutex_count++; \
} while(0)
#define IPDHCPC_UNLOCK() do { ip_assert(ipdhcpc.code_mutex_count == 1); \
ipdhcpc.code_mutex_count--; \
ipcom_mutex_unlock(ipdhcpc.code_mutex); \
} while(0)
#define IPDHCPC_ISINIT(handle) (IPDHCPC_STATE_BOUND > ((handle)->priv->state))
#define IPDHCPC_MAX_CID_LEN (254) /*the maximum length of option 61 is 255, the data is filled 0 at the beginning, so the real length is 255-1*/
#define IPDHCPC_CID_CFG_DELIMITER ":"
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
*===========================================================================
* Ipdhcpc_handle_priv
*===========================================================================
*/
typedef struct Ipdhcpc_handle_priv_struct
{
struct Ipdhcpc_handle_struct *next;
Ipcom_tmo tmo;
int tmo_ignore;
Ipcom_tmo tmo_t2;
int tmo2_ignore;
int retrans_counter;
int mode;
int state;
Ip_u32 xid_n;
Ip_u16 in_optlen;
Ip_u8 msgtype;
Ip_u8 pad;
/* Cached server info. */
Ip_u32 serverid_n; /* Server IP address */
Ip_u32 gw_n; /* Gateway IP address */
Ip_u32 yiaddr; /* 'your' (client) IP address */
}
Ipdhcpc_handle_priv;
/*
*===========================================================================
* Ipdhcpc_options
*===========================================================================
*/
typedef struct Ipdhcpc_options_struct
{
Ip_u8 optcode;
Ip_u8 minlen;
Ip_u8 maxlen;
Ip_u8 msgmust;
Ip_u8 msgmustnot;
}
Ipdhcpc_options;
/*
*===========================================================================
* Ipdhcpc_pkt
*===========================================================================
*/
IP_PACKED typedef struct Ipdhcpc_pkt_struct
{
Ip_u8 op;
Ip_u8 htype;
Ip_u8 hlen;
Ip_u8 hops;
Ip_u32 xid;
Ip_u16 secs;
Ip_u16 flags;
Ip_u32 ciaddr; /* Client IP address */
Ip_u32 yiaddr; /* 'your' (client) IP address. */
Ip_u32 siaddr; /* IP address of next server to use in bootstrap */
Ip_u32 giaddr; /* Relay agent IP address */
Ip_u8 chaddr[16]; /* Client hardware address. */
char sname[IPDHCPC_PKT_HDR_SNAME_LEN]; /* Optional server host name */
char file[IPDHCPC_PKT_HDR_FILE_LEN]; /* Boot file name */
Ip_u8 options[1];
}
Ipdhcpc_pkt;
/*
*===========================================================================
* Ipdhcpc_lease
*===========================================================================
*/
typedef struct Ipdhcpc_lease_struct
{
Ip_time_t t1;
Ip_time_t t2;
Ip_time_t tlease;
}
Ipdhcpc_lease;
/*
*===========================================================================
* Ipdhcpc_if
*===========================================================================
*/
typedef struct Ipdhcpc_if_struct
{
struct Ipdhcpc_if_struct *next;
Ip_u32 ifindex;
char ifname[IP_IFNAMSIZ];
int num_opened;
/* ifopen configuration. */
/* Time stuff. */
Ip_time_t time_addr_req; /* same as time when ifopen() was made. */
/* Interface stuff. */
Ip_u8 chaddr[16]; /* Client hardware address. */
Ip_u8 hlen;
Ip_u8 pad;
/* Config. */
char *clientid;
/* Interface lease. */
Ipdhcpc_handle *handle;
Ipdhcpc_lease lease;
Ip_u8 clientid_len;
Ip_bool isdown;
/*whether or not it is in inform only mode*/
Ip_bool information_only;
}
Ipdhcpc_if;
/*
*===========================================================================
* Ipdhcpc_data
*===========================================================================
*/
typedef struct Ipdhcpc_data_struct
{
Ip_bool opened;
Ipcom_mutex code_mutex; /* Mutex to protect IPDHCPC global data. */
int code_mutex_count;
Ip_fd fd;
Ip_fd ipd_fd;
Ip_fd rt_fd;
Ip_fd pkt_fd;
char buf[16];
struct Ip_sockaddr_in addr;
Ip_u32 last_brd_ifindex;
int open_interfaces;
Ip_u8 *in_raw;
Ipdhcpc_pkt *in_pkt;
Ip_ssize_t in_pkt_len;
Ipdhcpc_pkt *out_pkt;
Ipdhcpc_if *ift_head;
Ipdhcpc_handle *handle_head;
Ip_u16 max_message_size;
Ip_bool shut_down;
Ip_bool cid_uuid_type;
}
Ipdhcpc_data;
/*
*===========================================================================
* Ipdhcpc_ifopen
*===========================================================================
*/
typedef struct Ipdhcpc_ifopen_struct
{
Ip_u32 api_version; /* Currently version 1. Used for future compability. */
char clientid[IPDHCPC_CLIENT_ID_MAXSIZE+1]; /* must be null terminated! */
Ip_tag conf[1]; /* None available, included for future compability. */
}
Ipdhcpc_ifopen;
/*
****************************************************************************
* 6 FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* Ipdhcpc_cmd_dhcpc
*===========================================================================
* Description: Install IPDHCPC shell commands
* Parameters:
* Returns:
*
*/
#ifdef IPDHCPC_INCLUDE_CMD
IP_GLOBAL int ipdhcpc_cmd_dhcpc(int argc, char **argv);
#endif
/*
*===========================================================================
* ipdhcpc_ifopen
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_err ipdhcpc_ifopen(Ip_u32 ifindex, Ipdhcpc_ifopen *config);
/*
*===========================================================================
* ipdhcpc_ifclose
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_err ipdhcpc_ifclose(Ip_u32 ifindex);
/*
*===========================================================================
* ipdhcpc_if_isopen
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_bool ipdhcpc_if_isopen(Ip_u32 ifindex);
/*
*===========================================================================
* ipdhcpc_ifrequest
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_err ipdhcpc_ifrequest(Ip_u32 ifindex, Ipdhcpc_handle *handle);
/*
*===========================================================================
* ipdhcpc_iflease
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_err ipdhcpc_iflease(Ip_u32 ifindex, Ipdhcpc_handle *handle);
/*
*===========================================================================
* ipdhcpc_ifunlease
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_err ipdhcpc_ifunlease(Ip_u32 ifindex, Ipdhcpc_handle **handlep);
/*
*===========================================================================
* ipdhcpc_handle_malloc
*===========================================================================
* Description: Mallocate and initialize a DHCP client request handle for
* use with ipdhcpc_ifrequest().
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_err ipdhcpc_handle_malloc(Ip_u32 ifindex,
int mode,
Ipdhcpc_handle **handlep);
/*
*===========================================================================
* ipdhcpc_handle_free
*===========================================================================
* Description: Free a DHCP client handle.
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_err ipdhcpc_handle_free(Ip_u32 ifindex, Ipdhcpc_handle *handle);
/*
*===========================================================================
* ipdhcpc_option_sprintf
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL int ipdhcpc_option_sprintf(Ip_u8 *option, char *buf, int buflen);
/*
*===========================================================================
* ipdhcpc_option_find
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_u8 *ipdhcpc_option_find(Ipdhcpc_handle *handle, Ip_u8 code);
/*
*===========================================================================
* ipdhcpc_option_get_first
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_u8 *ipdhcpc_option_get_first(Ip_u8 *options);
/*
*===========================================================================
* ipdhcpc_option_limit_get_next
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL Ip_u8 *
ipdhcpc_option_limit_get_next(Ip_u8 * options, Ip_u16 *size);
#ifdef __cplusplus
}
#endif
#endif
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,512 @@
/*
******************************************************************************
* SOURCE FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipdnsc_cmd_nslookup.c,v $ $Revision: 1.23.10.2 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/ipdnsc_cmd_nslookup.c,v $
* $Author: jren $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
* nslookup shell command.
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
#include "ipdnsc_config.h"
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#define IPCOM_USE_CLIB_PROTO
#include <ipcom_clib.h>
#include <ipcom_getopt.h>
#include "ipdnsc.h"
#include "ipdnsc_h.h"
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
****************************************************************************
* 6 EXTERN PROTOTYPES
****************************************************************************
*/
/*
****************************************************************************
* 7 LOCAL PROTOTYPES
****************************************************************************
*/
IP_PUBLIC int ipdnsc_cmd_nslookup(int argc, char **argv);
/*
****************************************************************************
* 8 DATA
****************************************************************************
*/
/*
****************************************************************************
* 9 LOCAL FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* ipdnsc_ping_print_usage
*===========================================================================
* Description: Prints usage information for the nslookup shell command.
* Parameters: None.
* Returns: Nothing.
*/
IP_STATIC void
ipdnsc_nslookup_print_usage(void)
{
ipcom_printf("\n");
ipcom_printf("NAME\n");
ipcom_printf(" nslookup - query Internet name servers\n");
ipcom_printf("\n");
ipcom_printf("SYNOPSIS\n");
ipcom_printf(" nslookup [-f] [-c] [-v version] [-t timeout] [-r retries] host [nameserver]\n");
ipcom_printf("\n");
ipcom_printf("DESCRIPTION\n");
ipcom_printf(" nslookup queries an Internet name server for information abouts hosts.\n");
ipcom_printf(" host may be a domain name or an Internet v4 or v6 address.\n");
ipcom_printf(" [nameserver] is optional and provides the possibility to use a\n");
ipcom_printf(" nameserver other than the default. [nameserver] must be specified\n");
ipcom_printf(" by its Internet address.\n");
ipcom_printf("\n");
ipcom_printf(" -f Flush the DNS resolver cache and then exit immediately.\n");
ipcom_printf(" -c Use the DNS resolver's local cache. Default is to always\n");
ipcom_printf(" perform the name server lookup and not use the cache.\n");
ipcom_printf(" -v version\n");
ipcom_printf(" Specifies the type of address that is requested in a\n");
ipcom_printf(" name to address lookup. Allowed versions are 4 and 6.\n");
ipcom_printf(" If the -v flag is not given, nslookup first tries to find an\n");
ipcom_printf(" ipv6 address and then, if not found, an ipv4 address.\n");
ipcom_printf(" -t timeout\n");
ipcom_printf(" Specifies how long to wait for an answer from the nameserver\n");
ipcom_printf(" before retrying.\n");
ipcom_printf(" -r retries\n");
ipcom_printf(" Specifies the number of retries to use if the nameserver\n");
ipcom_printf(" fails to answer.\n");
#ifdef IPDNSC_USE_TEST
ipcom_printf(" -u do dns update test.\n");
#endif
ipcom_printf("\n");
ipcom_printf("RETURN VALUES\n");
ipcom_printf(" The nslookup command will exit with 0 on success, and non-zero on errors.\n");
}
/*
****************************************************************************
* 10 PUBLIC FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* ipcom_cmd_nslookup
*===========================================================================
* Description: Shell command nslookup.
* Parameters: argc - number of arguments.
* argv - list of arguments.
* Returns: 0 = success.
* -1 = failed.
*/
IP_PUBLIC int
ipdnsc_cmd_nslookup(int argc, char **argv)
{
struct Ip_hostent *he1, *he2 = IP_NULL;
struct Ip_in_addr host_addr;
#ifdef IPCOM_USE_INET6
struct Ip_in6_addr host_addr6;
#endif
Ipdnsc_ns_spec dns;
int address_lookup, use_cache, i, version, host_family = IP_AF_INET;
Ip_s32 err_num = IPDNSC_NO_RECOVERY;
char buf[40];
if(argc <= 1)
{
/* Interactive mode not supported - user must supply a hostname or internet
address and optionally a name server */
goto usage;
}
/* Get the default name server parameters */
if (ipdnsc_default(&dns))
{
ipcom_printf("Could not find name server.\n");
return -1;
}
/* Use only the primary name server */
ipcom_memset(&dns.secondary_dns.addr, 0, sizeof(dns.secondary_dns.addr));
dns.secondary_dns.family = 0;
ipcom_memset(&dns.tertiary_dns.addr, 0, sizeof(dns.tertiary_dns.addr));
dns.tertiary_dns.family = 0;
ipcom_memset(&dns.quaternary_dns.addr, 0, sizeof(dns.quaternary_dns.addr));
dns.quaternary_dns.family = 0;
address_lookup = IP_FALSE;
use_cache = IP_FALSE;
version = 0;
ipcom_getopt_clear();
while ((i = ipcom_getopt(argc, argv, "umnfcv:t:r:")) != -1)
{
switch (i)
{
case 'f':
ipdnsc_cache_flush();
return 0;
case 'c':
use_cache = IP_TRUE;
break;
case 'v':
version = ipcom_atoi(ip_optarg);
if(version != 4 && version != 6)
{
ipcom_printf("Version must be either 4 or 6\n");
return -1;
}
break;
case 't':
dns.timeout = ipcom_atoi(ip_optarg);
if(dns.timeout < 1 || dns.timeout > 60)
{
ipcom_printf("Timeout must be between 1 and 60 seconds\n");
return -1;
}
break;
case 'r':
dns.retry = ipcom_atoi(ip_optarg);
if (dns.retry < 0 || dns.retry > 5)
{
ipcom_printf("Retries must be between 0 and 5.\n");
return -1;
}
break;
#ifdef IPDNSC_USE_TEST
case 'u':
ipdnsc_test_update();
return 0;
case 'm': /* test ipdnsc_getipnodebyaddr */
#ifdef IPCOM_USE_INET6
{
struct Ip_hostent *he_v4mappedv6 = IP_NULL;
struct Ip_in6_addr ipaddr;
int error_num, addr_num = 0;
/* Set the address */
(void)ipcom_inet_pton(IP_AF_INET6, "::ffff:10.1.2.46", &ipaddr);
he_v4mappedv6 = ipdnsc_getipnodebyaddr(&ipaddr, IPDNSC_IN6ADDRSZ, IP_AF_INET6, &error_num);
if(he_v4mappedv6 != IP_NULL)
{
/* Print the hostname */
ipcom_printf("Name: %s\n", he_v4mappedv6->h_name);
/* Print the addresses */
if(he_v4mappedv6->h_addr_list != IP_NULL)
while(he_v4mappedv6->h_addr_list[addr_num] != IP_NULL)
{
if(ipcom_inet_ntop(he_v4mappedv6->h_addrtype, he_v4mappedv6->h_addr_list[addr_num], buf, sizeof(buf)))
{
if(!addr_num)
ipcom_printf("Address: %s\n", buf);
else
ipcom_printf(" %s\n", buf);
}
addr_num++;
}
ipdnsc_hostent_free(he_v4mappedv6);
}
}
#endif
return 0;
case 'n': /* test ipdnsc_getipnodebyname */
#ifdef IPCOM_USE_INET6
{
struct Ip_hostent *he_v4mappedv6 = IP_NULL;
int error_num, addr_num = 0;
he_v4mappedv6 = ipdnsc_getipnodebyname("v4mappedv6.interpeak.se", IP_AF_INET6, IPDNSC_AI_V4MAPPED | IPDNSC_AI_ALL, &error_num);
if(he_v4mappedv6 != IP_NULL)
{
/* Print the hostname */
ipcom_printf("Name: %s\n", he_v4mappedv6->h_name);
/* Print the addresses */
if(he_v4mappedv6->h_addr_list != IP_NULL)
while(he_v4mappedv6->h_addr_list[addr_num] != IP_NULL)
{
if(ipcom_inet_ntop(he_v4mappedv6->h_addrtype, he_v4mappedv6->h_addr_list[addr_num], buf, sizeof(buf)))
{
if(!addr_num)
ipcom_printf("Address: %s\n", buf);
else
ipcom_printf(" %s\n", buf);
}
addr_num++;
}
ipdnsc_hostent_free(he_v4mappedv6);
}
}
#endif
return 0;
#endif
default:
ipcom_printf("'%c' is a unknown switch\n", i);
return -1;
}
}
/* Get the hostname or host address */
if(argv[ip_optind])
{
/* Check if first argument is a presentation form ipv4 or ipv6 address */
if(ipcom_inet_pton(IP_AF_INET, argv[ip_optind], &host_addr) > 0)
{
address_lookup = IP_TRUE;
host_family = IP_AF_INET;
}
#ifdef IPCOM_USE_INET6
else if(ipcom_inet_pton(IP_AF_INET6, argv[ip_optind], &host_addr6) > 0)
{
address_lookup = IP_TRUE;
host_family = IP_AF_INET6;
}
#endif
}
else
{
ipcom_printf("Interactive mode not supported.\n");
return -1;
}
/* Get the optional name server */
if(argv[ip_optind+1])
{
/* Configure the primary DNS */
dns.primary_dns.family = IP_AF_INET;
if (ipcom_inet_pton (dns.primary_dns.family,
argv[ip_optind+1],
&dns.primary_dns.addr) != 1)
{
#ifdef IPCOM_USE_INET6
dns.primary_dns.family = IP_AF_INET6;
if(ipdnsc_get_ipv6_addrconfig(&dns.primary_dns.addr.inaddr6, &dns.primary_dns.ifindex, argv[ip_optind+1]) < 0)
#endif
/* Bad address */
dns.primary_dns.family = 0;
}
if(dns.primary_dns.family == 0)
{
ipcom_printf("Name server must be an internet address in presentation form.\n");
return -1;
}
}
/* Lookup the address of the DNS server. Never use the cache */
he1 = ipdnsc_resolve_addr((Ip_u8 *)&dns.primary_dns.addr, dns.primary_dns.family, IP_FALSE, &dns, &err_num);
if(!he1)
{
if(ipcom_inet_ntop(dns.primary_dns.family, &dns.primary_dns.addr, buf, sizeof(buf)))
{
ipcom_printf("*** Can't find server name for address %s : ", buf);
}
switch(err_num)
{
case IPDNSC_HOST_NOT_FOUND:
ipcom_printf("Non-existent host/domain.\n");
break;
case IPDNSC_TRY_AGAIN:
ipcom_printf("Try Again.\n");
break;
case IPDNSC_NO_RECOVERY:
ipcom_printf("No Recovery.\n");
break;
case IPDNSC_NO_DATA:
ipcom_printf("No Data.\n");
break;
default:
ipcom_printf("Unknown(%d)\n", err_num);
break;
}
/* Print the hostname */
ipcom_printf("Server: unknown\n");
/* Print the address */
if(ipcom_inet_ntop(dns.primary_dns.family, &dns.primary_dns.addr, buf, sizeof(buf)))
ipcom_printf("Address: %s\n", buf);
else
return -1;
}
else
{
/* Print the hostname */
ipcom_printf("Server: %s\n", he1->h_name);
/* Print the address */
if(ipcom_inet_ntop(he1->h_addrtype, he1->h_addr_list[0], buf, sizeof(buf)))
{
ipcom_printf("Address: %s\n", buf);
}
else
{
ipdnsc_hostent_free(he1);
return -1;
}
}
ipcom_printf("\n");
/* Lookup the hostname or host address. */
if(address_lookup)
{
if(host_family == IP_AF_INET)
{
he2 = ipdnsc_resolve_addr((Ip_u8 *)&host_addr, host_family, use_cache, &dns, &err_num);
}
#ifdef IPCOM_USE_INET6
else if(host_family == IP_AF_INET6)
{
he2 = ipdnsc_resolve_addr((Ip_u8 *)&host_addr6, host_family, use_cache, &dns, &err_num);
}
#endif
}
else
{
if(version == 4)
{
host_family = IP_AF_INET;
he2 = ipdnsc_resolve_name((Ip_u8 *)argv[ip_optind], host_family, use_cache, &dns, &err_num);
}
else if(version == 6)
{
#ifdef IPCOM_USE_INET6
host_family = IP_AF_INET6;
he2 = ipdnsc_resolve_name((Ip_u8 *)argv[ip_optind], host_family, use_cache, &dns, &err_num);
#else
he2 = IP_NULL;
err_num = IPDNSC_NO_RECOVERY;
#endif
}
else
{
#ifdef IPCOM_USE_INET6
/* First try to find a v6 address */
host_family = IP_AF_INET6;
he2 = ipdnsc_resolve_name((Ip_u8 *)argv[ip_optind], host_family, use_cache, &dns, &err_num);
if(!he2 && (err_num == IPDNSC_HOST_NOT_FOUND || err_num == IPDNSC_NO_ADDRESS))
{
host_family = IP_AF_INET;
/* If not found try a v4 address */
he2 = ipdnsc_resolve_name((Ip_u8 *)argv[ip_optind], host_family, use_cache, &dns, &err_num);
}
#else
host_family = IP_AF_INET;
he2 = ipdnsc_resolve_name((Ip_u8 *)argv[ip_optind], host_family, use_cache, &dns, &err_num);
#endif
}
}
if(!he2)
{
ipcom_printf("*** %s can't find %s : ", he1 != IP_NULL ? he1->h_name : buf, argv[ip_optind]);
switch(err_num)
{
case IPDNSC_HOST_NOT_FOUND:
ipcom_printf("Non-existent host/domain.\n");
break;
case IPDNSC_TRY_AGAIN:
ipcom_printf("Try Again.\n");
break;
case IPDNSC_NO_RECOVERY:
ipcom_printf("No Recovery.\n");
break;
case IPDNSC_NO_DATA:
ipcom_printf("No Data.\n");
break;
default:
ipcom_printf("Unknown(%d)\n", err_num);
break;
}
ipcom_printf("\n");
ipdnsc_hostent_free(he1);
return 0;
}
/* Print the hostname */
ipcom_printf("Name: %s\n", he2->h_name);
/* Print the addresses */
i=0;
if(he2->h_addr_list != IP_NULL)
while(he2->h_addr_list[i] != IP_NULL)
{
if(ipcom_inet_ntop(he2->h_addrtype, he2->h_addr_list[i], buf, sizeof(buf)))
{
if(!i)
ipcom_printf("Address: %s\n", buf);
else
ipcom_printf(" %s\n", buf);
}
i++;
}
/* Print the aliases */
i=0;
if(he2->h_aliases != IP_NULL)
while(he2->h_aliases[i] != IP_NULL)
{
if(!i)
ipcom_printf("Aliases: %s\n", he2->h_aliases[i]);
else
ipcom_printf(" %s\n", he2->h_aliases[i]);
i++;
}
ipcom_printf("\n");
ipdnsc_hostent_free(he1);
ipdnsc_hostent_free(he2);
return 0;
usage:
ipdnsc_nslookup_print_usage();
return 0;
}
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
@@ -0,0 +1,278 @@
/*
******************************************************************************
* HEADER FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipdnsc_h.h,v $ $Revision: 1.12.10.4 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/ipdnsc_h.h,v $
* $Author: jren $ $Date: 2015-11-16 02:52:29 $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Ulf Olofsson <ulf@interpeak.se>
******************************************************************************
*/
#ifndef IPDNSC_H_H
#define IPDNSC_H_H
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
* DNS client local header file.
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
#include <ipdnsc.h>
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/* Syslog facility */
#ifdef IPCOM_SYSLOG_FACILITY
#undef IPCOM_SYSLOG_FACILITY
#endif
#define IPCOM_SYSLOG_FACILITY IPCOM_LOG_NONE
/* Syslog priority */
#ifdef IPCOM_SYSLOG_PRIORITY
#undef IPCOM_SYSLOG_PRIORITY
#endif
#if defined(_WRS_CONFIG_IPDNSC_SYSLOG_PRIORITY)
#define IPCOM_SYSLOG_PRIORITY _WRS_CONFIG_IPDNSC_SYSLOG_PRIORITY
#else
#define IPCOM_SYSLOG_PRIORITY IPCOM_LOG_WARNING
#endif
/* Query flags */
#define IPDNSC_QR_FLAG 0x8000
#define IPDNSC_OPCODE_FLAG 0x7800
#define IPDNSC_UP_FLAG 0x2800
#define IPDNSC_AA_FLAG 0x0400
#define IPDNSC_TC_FLAG 0x0200
#define IPDNSC_RD_FLAG 0x0100
#define IPDNSC_RA_FLAG 0x0080
#define IPDNSC_ZERO_FLAG 0x0040
#define IPDNSC_RCODE_FLAG 0x000f
/* Query return codes */
#define IPDNSC_OK 0 /* General OK */
#define IPDNSC_ERR_GENERAL -1 /* General error */
#define IPDNSC_SERV_TIMEOUT -2 /* Server did not respond in time */
#define IPDNSC_SERV_NONAME -3 /* The name in the query does not exist */
#define IPDNSC_SERV_FORMAT -4 /* Format error */
#define IPDNSC_SERV_FAIL -5 /* Name server error */
#define IPDNSC_SERV_NOTIMPL -6 /* Not implemented */
#define IPDNSC_SERV_REFUSED -7 /* Refused */
#define IPDNSC_SERV_YXRRSET -8 /*exist*/
/* MAX name server number */
#define IPDNSC_MAX_NAMESERVER 3
/* DNS defaults */
#define IPDNSC_DEFAULT_DNS_PORT 53
#define IPDNSC_DEFAULT_DNS_TIMEOUT 10
#define IPDNSC_DEFAULT_DNS_RETRY 2
/* Maximum size */
#define IPDNSC_MAXLABEL 63
#define IPDNSC_MAXPKT 512
/* Internet address lengths */
#define IPDNSC_INADDRSZ 4
#define IPDNSC_IN6ADDRSZ 16
/* Maximum length of a FQDN */
#define IPDNSC_MAXNAME 255
/* The delimiter between the address and the interface for scoped addresses
* ex FE80::200:30FF:FE40:5060%eth0
*/
#define IPDNSC_IF_DELIMITER '%'
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
*===========================================================================
* Ipdnsc_hdr
*===========================================================================
* DNS protocol header structure.
*/
#include "ipcom_align16.h"
typedef IP_PACKED1 struct Ipdnsc_hdr_struct
{
Ip_u16 id; /* identification */
Ip_u16 flags; /* flags */
Ip_u16 no_ques; /* number of questions */
Ip_u16 no_answ; /* number of answers */
Ip_u16 no_auth; /* number of authority records */
Ip_u16 no_addi; /* number of resource records */
}
IP_PACKED2 Ipdnsc_hdr;
#include "ipcom_align16.h"
/*
*===========================================================================
* Ipdnsc_ns_addr
*===========================================================================
*/
typedef struct Ipdnsc_ns_addr_struct
{
Ip_s32 family;
union
{
struct Ip_in_addr inaddr; /* Network endian */
#ifdef IPCOM_USE_INET6
struct Ip_in6_addr inaddr6; /* Network endian */
#endif
} addr;
#ifdef IPCOM_USE_INET6
Ip_u32 ifindex; /* Interface index for link local v6 addresses */
#endif
Ip_u16 port; /* Network endian */
Ip_u16 pad;
}
Ipdnsc_ns_addr;
/* store name server address parsed from response records */
typedef struct Ipdnsc_record_addr_struct {
struct Ipdnsc_record_addr_struct *next;
Ipdnsc_ns_addr ns_addr;
}
Ipdnsc_record_addr;
/* store ns addresses which belong to a same zone */
typedef struct Ipdnsc_record_ns_struct {
struct Ipdnsc_record_ns_struct *next;
char *name;
Ip_u32 flags;
Ipdnsc_record_addr *rr_addrs;
}
Ipdnsc_record_ns;
/* all zones have been separated */
typedef struct Ipdnsc_record_nsset_struct {
Ipdnsc_record_ns *head;
}
Ipdnsc_record_nsset;
/*
*===========================================================================
* Ipdnsc_ns_spec
*===========================================================================
*/
typedef struct Ipdnsc_ns_spec_struct
{
Ip_s32 timeout;
Ip_s32 retry;
Ipdnsc_ns_addr primary_dns;
Ipdnsc_ns_addr secondary_dns;
Ipdnsc_ns_addr tertiary_dns;
Ipdnsc_ns_addr quaternary_dns;
}
Ipdnsc_ns_spec;
/*
*===========================================================================
* Ipdnsc_cache
*===========================================================================
*/
typedef struct Ipdnsc_cache_struct
{
struct Ipdnsc_cache_struct *next;
Ip_u32 type;
Ip_u32 expire;
struct Ip_hostent *he;
}
Ipdnsc_cache;
/*
*===========================================================================
* Ipdnsc_data
*===========================================================================
*/
typedef struct Ipdnsc_data_struct
{
Ip_bool init;
Ipcom_mutex mutex;
Ipdnsc_cache *cache_head;
Ip_u32 cache_entries;
}
Ipdnsc_data;
/*
****************************************************************************
* 6 LOCAL FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* 10 PUBLIC FUNCTIONS
****************************************************************************
*/
IP_EXTERN Ipdnsc_data ipdnsc;
IP_GLOBAL Ip_u32 ipdnsc_get_time(void);
IP_GLOBAL Ip_s32 ipdnsc_create_query(Ip_u8 *dname, Ip_u16 type, Ip_u16 class, Ip_u8 *buf);
IP_GLOBAL Ip_s32 ipdnsc_send_wait(Ip_u8 *s_buf, Ip_s32 s_size, Ip_u8 *r_buf, Ip_s32 *r_size, Ipdnsc_ns_addr *dns_addr, Ip_s32 timeout);
IP_GLOBAL struct Ip_hostent* ipdnsc_resolve_addr(Ip_u8 *addr, Ip_s32 family, Ip_s32 use_cache,
Ipdnsc_ns_spec *dns, Ip_s32 *error_num);
IP_GLOBAL struct Ip_hostent* ipdnsc_resolve_name(Ip_u8 *name, Ip_s32 family, Ip_s32 use_cache,
Ipdnsc_ns_spec *dns, Ip_s32 *error_num);
IP_GLOBAL void ipdnsc_hostent_free(struct Ip_hostent *he);
IP_GLOBAL struct Ip_hostent* ipdnsc_hostent_create(Ip_s32 type);
IP_GLOBAL Ip_s32 ipdnsc_hostent_insert_addr(struct Ip_hostent *he, char *addr);
IP_GLOBAL Ip_s32 ipdnsc_hostent_insert_alias(struct Ip_hostent *he, char *name);
IP_GLOBAL Ip_s32 ipdnsc_hostent_insert_name(struct Ip_hostent *he, char *name);
IP_GLOBAL struct Ip_hostent* ipdnsc_hostent_copy(struct Ip_hostent *he);
IP_GLOBAL Ip_s32 ipdnsc_default(Ipdnsc_ns_spec *dns);
IP_GLOBAL void ipdnsc_dns_tick(void);
IP_GLOBAL int ipdnsc_dns_init(void);
IP_GLOBAL int ipdnsc_cmd_nslookup(int argc, char **argv);
IP_GLOBAL Ip_err ipdnsc_start(void);
#ifdef IPCOM_USE_INET6
IP_GLOBAL Ip_s32 ipdnsc_get_ipv6_addrconfig(struct Ip_in6_addr *addr6, Ip_u32 *ifindex, char *addrstr);
#endif
IP_GLOBAL Ip_s32 ipdnsc_samename(const char *a, const char *b);
IP_GLOBAL void ipdnsc_free_nsrrset(Ipdnsc_record_nsset *nsrrsp);
IP_GLOBAL Ip_s32 ipdnsc_mkupdate(Ipdnsc_updrec *rrecp_in, Ip_u8 *buf, Ip_s32 buflen, Ip_s32 *error);
IP_GLOBAL Ip_s32 ipdnsc_get_soa(const char *dname, Ip_u16 qclass, char *zname,
Ip_size_t zsize, char *mname, Ip_size_t msize, Ipdnsc_record_nsset *nsrrsp, Ip_s32 *error);
IP_GLOBAL Ip_s32 ipdnsc_get_ns(char *zname, Ip_u16 qclass, Ipdnsc_record_nsset *nsrrsp, Ip_s32 *error);
IP_GLOBAL Ip_s32 ipdnsc_get_glue(Ip_u16 qclass, Ipdnsc_record_nsset *nsrrsp, Ip_s32 *error);
IP_GLOBAL Ip_s32 ipdnsc_satisfy(const char *mname, Ipdnsc_record_nsset *nsrrsp, Ipdnsc_ns_addr *addrs);
IP_GLOBAL Ip_s32 ipdnsc_send_update(Ip_u8 *s_buf, Ip_s32 s_buflen, Ip_u8 *r_buf, Ip_s32 *r_buflen,
Ipdnsc_ns_addr *dns_addr, Ip_u32 dns_count, Ip_s32 *error);
IP_GLOBAL Ip_s32 ipdnsc_send_update_signed(const Ip_u8 *s_buf, Ip_s32 s_buflen, Ip_u8 *r_buf, Ip_s32 *r_buflen,
Ipdnsc_ns_addr *dns_addr, Ip_u32 dns_count, Ipdnsc_tsig_key *key, Ip_s32 *error);
#ifdef IPDNSC_USE_TEST
IP_GLOBAL void ipdnsc_test_update(void);
#endif
#endif
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,285 @@
/*
******************************************************************************
* HEADER FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipftps_h.h,v $ $Revision: 1.11.12.4 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/ipftps_h.h,v $
* $Author: hxu0 $ $Date: 2013-04-18 08:48:02 $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Ulf Olofsson <ulf@interpeak.se>
******************************************************************************
*/
#ifndef IPFTPS_H_H
#define IPFTPS_H_H
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
#include "ipftps_config.h"
#include "ipcom_config.h"
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#include "ipftps.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/* Syslog facility code */
#ifdef IPCOM_SYSLOG_FACILITY
#undef IPCOM_SYSLOG_FACILITY
#endif
#define IPCOM_SYSLOG_FACILITY IPCOM_LOG_IPFTP
/* Select level of verbosity for syslog messages if it is not defined */
#ifdef IPCOM_SYSLOG_PRIORITY
#undef IPCOM_SYSLOG_PRIORITY
#endif
#if defined(_WRS_CONFIG_IPFTPS_SYSLOG_PRIORITY)
#define IPCOM_SYSLOG_PRIORITY _WRS_CONFIG_IPFTPS_SYSLOG_PRIORITY
#else
#define IPCOM_SYSLOG_PRIORITY IPCOM_LOG_WARNING
#endif
/* Telnet */
#define IPFTPS_SE 240
#define IPFTPS_NOP 241
#define IPFTPS_DM 242
#define IPFTPS_BRK 243
#define IPFTPS_IP 244
#define IPFTPS_AO 245
#define IPFTPS_AYT 246
#define IPFTPS_EC 247
#define IPFTPS_EL 248
#define IPFTPS_GA 249
#define IPFTPS_SB 250
#define IPFTPS_WILL 251
#define IPFTPS_WONT 252
#define IPFTPS_DO 253
#define IPFTPS_DONT 254
#define IPFTPS_IAC 255
/* Line terminator */
#define CL "\r\n"
/* Source code line string */
#define __LINESTR(line) #line
#define __LINESTR_(line) __LINESTR(line)
#define __LINESTR__ __LINESTR_(__LINE__)
/* Create log messages */
#undef IPCOM_SYSLOG_BODY
#ifdef IP_DEBUG
#define IPCOM_SYSLOG_BODY(x) "%s at " __FILE__ "(" __LINESTR__ "): " x, \
(session == IP_NULL ? "ipftps" : session->name)
#else
#define IPCOM_SYSLOG_BODY(x) "%s: " x, \
(session == IP_NULL ? "ipftps" : session->name)
#endif
/* Time values */
#define IPFTPS_MONTHS (30L * IPFTPS_DAYS)
#define IPFTPS_DAYS (24L * IPFTPS_HOURS)
#define IPFTPS_HOURS (60L * IPFTPS_MINUTES)
#define IPFTPS_MINUTES (60L)
/* Max length of IP address string */
#define IPFTPS_IPV4_MAXLEN \
sizeof("255.255.255.255")
#define IPFTPS_IPV6_MAXLEN \
sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
/* IPv4/IPv6 macros */
#ifdef IPCOM_USE_INET6
#define IPFTPS_AFMAX 2
#define IPFTPS_AFSTR "(1,2)"
#define IPFTPS_IP_MAXLEN IPFTPS_IPV6_MAXLEN
#define IPFTPS_PORT(s) *((s)->sa.sa_family == IP_AF_INET ? \
&(s)->sin.sin_port : \
&(s)->sin6.sin6_port)
#define IPFTPS_SOCKADDRLEN(s) ((s)->sa.sa_family == IP_AF_INET ? \
sizeof((s)->sin) : \
sizeof((s)->sin6))
#define IPFTPS_ADDRCMP(a1,a2) ((a1)->sa.sa_family != (a2)->sa.sa_family || \
((a1)->sa.sa_family == IP_AF_INET ? \
ipcom_memcmp(&(a1)->sin.sin_addr, &(a2)->sin.sin_addr, \
sizeof((a1)->sin.sin_addr)) : \
ipcom_memcmp(&(a1)->sin6.sin6_addr, &(a2)->sin6.sin6_addr, \
sizeof((a1)->sin6.sin6_addr))) != 0)
#else
#define IPFTPS_AFMAX 1
#define IPFTPS_AFSTR "(1)"
#define IPFTPS_IP_MAXLEN IPFTPS_IPV4_MAXLEN
#define IPFTPS_PORT(s) ((s)->sin.sin_port)
#define IPFTPS_SOCKADDRLEN(s) (sizeof((s)->sin))
#define IPFTPS_ADDRCMP(a1,a2) ((a1)->sa.sa_family != (a2)->sa.sa_family || \
ipcom_memcmp(&(a1)->sin.sin_addr, &(a2)->sin.sin_addr, \
sizeof((a1)->sin.sin_addr)) != 0)
#ifndef IP_AF_INET6
#define IP_AF_INET6 -1 /* Dummy to avoid #ifdefs */
#endif
#endif
/* Liberal free */
#define ipftps_free(x) \
if ((x) != IP_NULL) do {ipcom_free(x); (x) = IP_NULL; (void)(x);} while (0)
/* A string and its length */
#define ipftps_strandlen(x) x, ipcom_strlen(x)
/*!! to work with ipcom410. */
#ifndef IP_ERRNO_ENOTDIR
#define IP_ERRNO_ENOTDIR 20
#endif
/* IPLITE/IPNET does not support write select */
#if !defined(IPCOM_SOCK_IPCOM)
#define IPFTPS_USE_WRITESELECT
#endif
#ifdef IPFTP_TLS
#define IPFTPS_CTRL_FLAG 0
#define IPFTPS_DATA_FLAG 1
#endif
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
****************************************************************************
* 6 LOCAL FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* 10 PUBLIC FUNCTIONS
****************************************************************************
*/
#ifdef IPFTPS_C
#ifdef IPFTP_TLS
IP_GLOBAL void
ipftps_free_tls(void);
IP_GLOBAL void
ipftps_free_tls_session(Ipftps_session *session);
IP_GLOBAL int
ipftps_init_tls(Ipftps_session *session);
IP_GLOBAL int
ipftps_init_tls_session(Ipftps_session *session);
IP_GLOBAL int
ipftps_create_new_tls_csession(Ipftps_session *session);
IP_GLOBAL int
ipftps_create_new_tls_dsession(Ipftps_session *session, int *eno);
IP_GLOBAL void
ipftps_tls_close_session(Ipftps_session *session, int option);
IP_GLOBAL int
ipftps_tls_send(Ipftps_session *session, Ip_fd fd,char *buf, int buflen);
IP_GLOBAL int
ipftps_tls_read(Ipftps_session *session, Ip_fd fd,char *buf, int buflen, int *len);
IP_GLOBAL void
ipftps_tls_set_flag(Ipftps_session *session, int option,Ip_bool flag);
IP_GLOBAL void
ipftps_tls_shutdown(Ipftps_session *session,int option);
IP_GLOBAL void
ipftps_tls_set_ccc(Ipftps_session *session, Ip_bool flag);
IP_GLOBAL Ip_bool
ipftps_tls_get_ccc(Ipftps_session *session);
#endif
#endif
/*
*===========================================================================
* ipftps_session
*===========================================================================
* Description:
* FTP session handler subprocess.
*
* Parameters:
*
* Returns:
*/
IPCOM_PROCESS(ipftps_session);
/*
*===========================================================================
* ipftps_main
*===========================================================================
* Description:
* FTP main process.
*
* Parameters:
*
* Returns:
*/
IPCOM_PROCESS(ipftps_main);
/*
*===========================================================================
* ipftps_create
*===========================================================================
* Description:
* Start FTP daemon.
*
* Parameters:
*
* Returns:
*/
IP_PUBLIC Ip_err
ipftps_create(void);
#ifdef __cplusplus
}
#endif
#endif
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,185 @@
/*
******************************************************************************
* HEADER FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: iptftp_h.h,v $ $Revision: 1.9.12.2 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/iptftp_h.h,v $
* $Author: jren $ $Date: 2019-04-02 08:29:40 $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
#ifndef IPTFTP_H_H
#define IPTFTP_H_H
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#include <ipcom_clib.h>
#include <ipcom_err.h>
#include <ipcom_os.h>
#include <ipcom_syslog.h>
#include <ipcom_sock.h>
#ifdef IPCOM_USE_INET6
#include <ipcom_sock6.h>
#endif
#include <ipcom_sysvar.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/* Syslog facility */
#ifdef IPCOM_SYSLOG_FACILITY
#undef IPCOM_SYSLOG_FACILITY
#endif
#define IPCOM_SYSLOG_FACILITY IPCOM_LOG_IPTFTP
/* Syslog priority */
#ifdef IPCOM_SYSLOG_PRIORITY
#undef IPCOM_SYSLOG_PRIORITY
#endif
#if defined(_WRS_CONFIG_IPTFTP_SYSLOG_PRIORITY)
#define IPCOM_SYSLOG_PRIORITY _WRS_CONFIG_IPTFTP_SYSLOG_PRIORITY
#else
#define IPCOM_SYSLOG_PRIORITY IPCOM_LOG_WARNING
#endif
/* Directory separator character */
#define IPTFTP_DIR_SEP '/'
#define IPTFTP_DIR_SEP_STRING "/"
/* tftp port */
#define IPTFTP_PORT 69
/* tftp maximum packet size */
#define IPTFTP_MAX_PACKET 516
/* TFTP opcodes */
#define IPTFTP_OPCODE_RRQ 1
#define IPTFTP_OPCODE_WRQ 2
#define IPTFTP_OPCODE_DATA 3
#define IPTFTP_OPCODE_ACK 4
#define IPTFTP_OPCODE_ERR 5
/* TFTP Error codes */
#define IPTFTP_ERRCODE_NOTDEFINED 0
#define IPTFTP_ERRCODE_FILENOTFOUND 1
#define IPTFTP_ERRCODE_ACCESSVIOLATION 2
#define IPTFTP_ERRCODE_DISKFULL 3
#define IPTFTP_ERRCODE_ILLEGALTFTPOP 4
#define IPTFTP_ERRCODE_UNKNKOWNTRANSFERID 5
#define IPTFTP_ERRCODE_FILEALREADYEXISTS 6
#define IPTFTP_ERRCODE_NOSUCHUSER 7
/* TFTP Error messages */
#define IPTFTP_ERRMSG_INCOMPLETEMSG "Incomplete message."
#define IPTFTP_ERRMSG_INVALIDOPCODE "Invalid opcode."
#define IPTFTP_ERRMSG_INVALIDMODE "Invalid mode."
#define IPTFTP_ERRMSG_COULDNOTOPENFILE "Could not open file."
#define IPTFTP_ERRMSG_COULDNOTCREATEFILE "Could not create file."
#define IPTFTP_ERRMSG_FILEREADERROR "File read error."
#define IPTFTP_ERRMSG_INCORRECTSRCPORT "Incorrect source port."
#define IPTFTP_ERRMSG_FAILEDTOCHANGEDIR "Failed to change directory."
#define IPTFTP_ERRMSG_INVALIDLINEDELIMITER "Invalid line delimiter received."
#define IPTFTP_ERRMSG_FILEWRITEERROR "File write error."
#define IPTFTP_ERRMSG_FILEABOVEWORKINGDIR "Could not access file aboved working directory."
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
typedef union
{
struct Ip_in_addr addr;
#ifdef IPCOM_USE_INET6
struct Ip_in6_addr addr6;
#endif
}
Iptftp_addr;
typedef struct Iptftp_struct
{
Ip_s32 family; /* Internet address family */
#ifdef IPCOM_USE_INET6
Ip_u32 ifindex; /* Interface index for link local v6 addresses */
#endif
Iptftp_addr peer_addr; /* Peer address (network endian) */
Ip_u16 peer_port; /* Peer port */
Ip_u16 opcode; /* Read or Write request */
Ip_bool binary; /* True for octet mode */
Ip_bool verbose; /* True for statistics and error message stdio printing */
Ip_u32 nbytes; /* The number of bytes transfer in the connection */
Ip_s32 dirlen; /* Length of the server working directory */
char file[256]; /* Filename with absolute path */
/* ancillary data buffer */
#ifdef IPCOM_USE_INET6
char controlbuf[IP_CMSG_SPACE(sizeof(struct Ip_in6_pktinfo))];
#else
char controlbuf[IP_CMSG_SPACE(sizeof(struct Ip_in_pktinfo))];
#endif
Ip_socklen_t controllen; /* ancillary data len */
}
Iptftp;
/*
****************************************************************************
* 6 FUNCTIONS
****************************************************************************
*/
IP_GLOBAL Ip_err iptftp_sendto(Ip_fd sock, Iptftp *tftp, char *buf, Ip_s32 len);
IP_GLOBAL Ip_err iptftp_bind_zero(Ip_fd sock, Ip_s32 family);
IP_GLOBAL Ip_s32 iptftp_recvfrom(Ip_fd sock, Iptftp *tftp, char *buf, Ip_s32 bufsize);
IP_GLOBAL void iptftp_send_err(Ip_fd sock, Iptftp *tftp, Ip_u16 err_code, char *err_msg);
IP_GLOBAL Ip_err iptftp_receive_loop(Ip_fd sock, IP_FILE *fp, char **err_msg, Ip_u16 *err_code, Iptftp *tftp);
IP_GLOBAL Ip_err iptftp_send_loop(Ip_fd sock, IP_FILE *fp, char **err_msg, Ip_u16 *err_code, Iptftp *tftp);
IP_GLOBAL void iptftp_strip_path(char *path);
IP_GLOBAL int iptftp_cmd_tftp(int argc, char **argv);
#ifdef IPTESTENGINE
IP_GLOBAL void iptftp_create_file(char *fullPath);
#endif
#ifdef __cplusplus
}
#endif
#endif
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
+550
View File
@@ -0,0 +1,550 @@
/*
******************************************************************************
* SOURCE FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: iptftpc.c,v $ $Revision: 1.27.10.2 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/iptftpc.c,v $
* $Author: jren $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
* TFTP client implementation.
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#define IPCOM_USE_CLIB_PROTO
#include <ipcom_clib.h>
#include <ipcom_time.h>
#include <ipcom_getopt.h>
#include <ipcom_syslog.h>
#include <ipcom_sock2.h>
#include "iptftp_h.h"
#include "iptftp.h"
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
****************************************************************************
* 6 EXTERN PROTOTYPES
****************************************************************************
*/
/*
****************************************************************************
* 7 LOCAL PROTOTYPES
****************************************************************************
*/
IP_PUBLIC Ip_err iptftpc_create(void);
IP_PUBLIC Ip_err iptftpc_start(void);
IP_PUBLIC const char *iptftpc_version(void);
IP_STATIC Ip_err iptftp_client_send_file(const char *src, Iptftp *tftp);
IP_STATIC Ip_err iptftp_client_receive_file(const char *dst, Iptftp *tftp);
/*
****************************************************************************
* 8 DATA
****************************************************************************
*/
/*
****************************************************************************
* 9 STATIC FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* iptftp_client_send_file
*===========================================================================
* Description: Internal send function for TFTP client
* Parameters: src - name of the file to send
* tftp - TFTP structure
* Returns: IPTFTP_SUCCESS for success and a TFTP error code for failures.
*/
IP_STATIC Ip_err
iptftp_client_send_file(const char *src, Iptftp *tftp)
{
Ip_fd sock = IP_INVALID_SOCKET;
IP_FILE *fp;
char *err_msg = IP_NULL;
Ip_u16 err_code = 0;
Ip_err retval;
if(tftp->binary)
fp = ipcom_fopen(src, "rb");
else
fp = ipcom_fopen(src, "r");
if(!fp)
{
retval = IPTFTP_ERR_FILE_OPEN;
IPCOM_LOG1(ERR, "iptftp_client_send_file :: Could not open file: \"%s\"", src);
goto err;
}
/* Get a udp socket, and bind to port 0 */
sock = ipcom_socket(tftp->family, IP_SOCK_DGRAM, 0);
if (sock == IP_INVALID_SOCKET)
{
retval = IPTFTP_ERR_SOCKET_FAIL;
IPCOM_LOG0(ERR, "iptftp_client_send_file :: ipcom_socket() failed");
goto err;
}
if (iptftp_bind_zero(sock, tftp->family) != IPCOM_SUCCESS)
{
retval = IPTFTP_ERR_SOCKET_FAIL;
goto err;
}
/* Send the file */
retval = iptftp_send_loop(sock, fp, &err_msg, &err_code, tftp);
err:
/* Send error */
if(err_msg)
iptftp_send_err(sock, tftp, err_code, err_msg);
if(fp != IP_NULL)
(void)ipcom_fclose(fp);
if(sock != IP_INVALID_SOCKET)
ipcom_socketclose(sock);
return retval;
}
/*
*===========================================================================
* iptftp_client_receive_file
*===========================================================================
* Description: Internal receive function for TFTP client
* Parameters: dst - name of the file to receive
* tftp - TFTP structure
* Returns: IPTFTP_SUCCESS for success and a TFTP error code for failures.
*/
IP_STATIC Ip_err
iptftp_client_receive_file(const char *dst, Iptftp *tftp)
{
Ip_fd sock = IP_INVALID_SOCKET;
IP_FILE *fp;
char *err_msg = IP_NULL;
Ip_u16 err_code = 0;
Ip_err retval;
if(tftp->binary)
fp = ipcom_fopen(dst, "wb");
else
fp = ipcom_fopen(dst, "w");
if(!fp)
{
retval = IPTFTP_ERR_FILE_OPEN;
IPCOM_LOG1(ERR, "iptftp_client_receive_file :: Could not create file: \"%s\"", dst);
goto err;
}
/* Get a udp socket, and bind to port 0 */
sock = ipcom_socket(tftp->family, IP_SOCK_DGRAM, 0);
if (sock == IP_INVALID_SOCKET)
{
retval = IPTFTP_ERR_SOCKET_FAIL;
IPCOM_LOG0(ERR, "iptftp_client_receive_file :: ipcom_socket() failed");
goto err;
}
if(iptftp_bind_zero(sock, tftp->family) != IPCOM_SUCCESS)
{
retval = IPTFTP_ERR_SOCKET_FAIL;
goto err;
}
/* Receive the file */
retval = iptftp_receive_loop(sock, fp, &err_msg, &err_code, tftp);
err:
/* Receive error */
if(err_msg)
iptftp_send_err(sock, tftp, err_code, err_msg);
if(fp != IP_NULL)
(void)ipcom_fclose(fp);
if(sock != IP_INVALID_SOCKET)
ipcom_socketclose(sock);
return retval;
}
/*
****************************************************************************
* 10 GLOBAL FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* 11 PUBLIC FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* iptftp_client_get
*===========================================================================
* Description: See iptftp.h for description.
* Parameters:
* Returns:
*/
IP_PUBLIC Ip_err
iptftp_client_get(const char *server, const char *srcfile, const char *dstfile,
Ip_bool binary, Ip_bool verbose, int port)
{
Iptftp *tftp;
Ip_err ret;
char dst[256];
#ifdef IPCOM_USE_TIME
Ip_u32 msec, sec;
#endif
struct Ip_addrinfo *res = IP_NULL;
ip_assert(srcfile != IP_NULL);
tftp = ipcom_malloc(sizeof(*tftp));
if(tftp == IP_NULL)
return IPTFTP_ERR_MEMORY_FAIL;
ipcom_memset(tftp, 0, sizeof(*tftp));
tftp->opcode = IPTFTP_OPCODE_RRQ;
tftp->binary = binary;
tftp->verbose = verbose;
if(port == 0) /* No passed port number check the sysvar */
tftp->peer_port = (Ip_u16)ipcom_sysvar_get_as_int("iptftp.clntSrvPort", IPTFTP_PORT);
else
tftp->peer_port = (Ip_u16) port;
if (ipcom_getaddrinfo(server, IP_NULL, IP_NULL, &res) == 0)
{
tftp->family = res->ai_addr->sa_family;
#ifdef IPCOM_USE_INET
if(tftp->family == IP_AF_INET)
{
ipcom_memcpy(&tftp->peer_addr.addr,
&((struct Ip_sockaddr_in *)res->ai_addr)->sin_addr,
sizeof(tftp->peer_addr.addr));
goto server_ok;
}
#endif
#ifdef IPCOM_USE_INET6
if(tftp->family == IP_AF_INET6)
{
ipcom_memcpy(&tftp->peer_addr.addr6,
&((struct Ip_sockaddr_in6 *)res->ai_addr)->sin6_addr,
sizeof(tftp->peer_addr.addr6));
tftp->ifindex = ((struct Ip_sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
goto server_ok;
}
#endif
}
ret = IPTFTP_ERR_SERVER_ADDR;
goto exit;
server_ok:
if(ipcom_strlen(srcfile) >= sizeof(tftp->file))
{
ret = IPTFTP_ERR_FILE_OPEN;
goto exit;
}
else
{
ipcom_strcpy(tftp->file, srcfile);
}
if(!dstfile)
{
ipcom_strcpy(dst, tftp->file);
iptftp_strip_path(dst);
}
else
ipcom_strcpy(dst, dstfile);
#ifdef IPCOM_USE_TIME
msec = (Ip_u32)ipcom_times(IP_NULL);
#endif
ret = iptftp_client_receive_file(dst, tftp);
if(ret == IPTFTP_SUCCESS)
{
#ifdef IPCOM_USE_TIME
msec = (Ip_u32)ipcom_times(IP_NULL) - msec;
sec = msec / 1000;
msec = (msec % 1000) / 100; /* Tenths of second */
if(verbose)
ipcom_printf("Transfer completed: %ld bytes in %ld.%ld seconds."IP_LF, tftp->nbytes, sec, msec);
#else
if(verbose)
ipcom_printf("Transfer completed: %ld bytes."IP_LF, tftp->nbytes);
#endif
}
exit:
if(res != IP_NULL)
ipcom_freeaddrinfo(res);
ipcom_free(tftp);
return ret;
}
/*
*===========================================================================
* iptftp_client_put
*===========================================================================
* Description: See iptftp.h for description.
* Parameters:
* Returns:
*/
IP_PUBLIC Ip_err
iptftp_client_put(const char *server, const char *srcfile, const char *dstfile,
Ip_bool binary, Ip_bool verbose, int port)
{
Iptftp *tftp;
Ip_err ret;
char dst[256];
#ifdef IPCOM_USE_TIME
Ip_u32 msec, sec;
#endif
struct Ip_addrinfo *res = IP_NULL;
ip_assert(srcfile != IP_NULL);
tftp = ipcom_malloc(sizeof(*tftp));
if(tftp == IP_NULL)
return IPTFTP_ERR_MEMORY_FAIL;
ipcom_memset(tftp, 0, sizeof(*tftp));
tftp->opcode = IPTFTP_OPCODE_WRQ;
tftp->binary = binary;
tftp->verbose = verbose;
if(port == 0) /* No passed port number check the sysvar */
tftp->peer_port = (Ip_u16)ipcom_sysvar_get_as_int("iptftp.clntSrvPort", IPTFTP_PORT);
else
tftp->peer_port = (Ip_u16) port;
if (ipcom_getaddrinfo(server, IP_NULL, IP_NULL, &res) == 0
&& res != IP_NULL && res->ai_addr != IP_NULL)
{
tftp->family = res->ai_addr->sa_family;
#ifdef IPCOM_USE_INET
if(tftp->family == IP_AF_INET)
{
ipcom_memcpy(&tftp->peer_addr.addr,
&((struct Ip_sockaddr_in *)res->ai_addr)->sin_addr,
sizeof(tftp->peer_addr.addr));
goto server_ok;
}
#endif
#ifdef IPCOM_USE_INET6
if(tftp->family == IP_AF_INET6)
{
ipcom_memcpy(&tftp->peer_addr.addr6,
&((struct Ip_sockaddr_in6 *)res->ai_addr)->sin6_addr,
sizeof(tftp->peer_addr.addr6));
tftp->ifindex = ((struct Ip_sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
goto server_ok;
}
#endif
}
ret = IPTFTP_ERR_SERVER_ADDR;
goto exit;
server_ok:
if(!dstfile)
{
ipcom_strcpy(dst, srcfile);
iptftp_strip_path(dst);
}
else
ipcom_strcpy(dst, dstfile);
if(ipcom_strlen(dst) >= sizeof(tftp->file))
{
ret = IPTFTP_ERR_FILE_OPEN;
goto exit;
}
else
{
ipcom_strcpy(tftp->file, dst);
}
#ifdef IPCOM_USE_TIME
msec = (Ip_u32)ipcom_times(IP_NULL);
#endif
ret = iptftp_client_send_file(srcfile, tftp);
if(ret == IPTFTP_SUCCESS)
{
#ifdef IPCOM_USE_TIME
msec = (Ip_u32)ipcom_times(IP_NULL) - msec;
sec = msec / 1000;
msec = (msec % 1000) / 100; /* Tenths of second */
if(verbose)
ipcom_printf("Transfer completed: %ld bytes in %ld.%ld seconds."IP_LF, tftp->nbytes, sec, msec);
#else
if(verbose)
ipcom_printf("Transfer completed: %ld bytes."IP_LF, tftp->nbytes);
#endif
}
exit:
if(res != IP_NULL)
ipcom_freeaddrinfo(res);
ipcom_free(tftp);
return ret;
}
/*
*===========================================================================
* iptftp_create_file
*===========================================================================
*
* Description:
* create file in local target.
*
* WARNING--This function is designed to be used for test
* purposes. It is of utmost importance that this function
* is disabled before deploying the TFTP client. This is
* done by not defining IPTESTENGINE.
*
* Parameters:
* fullPath - full directory
*
* Returns:
*/
#ifdef IPTESTENGINE
IP_GLOBAL void
iptftp_create_file(char *fullPath)
{
int mode = IP_O_RDWR | IP_O_CREAT
#ifdef IP_O_BINARY
| IP_O_BINARY
#endif
;
Ip_fd fd;
Ip_fd sock = -1;
Iptftp tftp;
if ((fd = ipcom_fileopen(fullPath, mode,
IP_S_IRUSR | IP_S_IWUSR |
IP_S_IRGRP | IP_S_IROTH)) < 0)
return;
else
{
static const char data[] = "\r\n\n\n\r\r\n";
int datalen = strlen(data);
int len = 50;
int i;
for (i = 0; i < len; i += datalen)
{
int writelen = IP_MIN(datalen, len - i);
if (ipcom_filewrite(fd, data, writelen) != writelen)
{
(void)ipcom_fileclose(fd);
return;
}
}
(void)ipcom_fileclose(fd);
}
ipcom_memset(&tftp,0,sizeof(tftp));
tftp.family = IP_AF_UNSPEC;
iptftp_send_err(sock, &tftp, IPTFTP_ERRCODE_UNKNKOWNTRANSFERID, IPTFTP_ERRMSG_INCORRECTSRCPORT);
return;
}
#endif
/*
*===========================================================================
* iptftpc_create
*===========================================================================
* Description: TFTP Client create function.
* Parameters: none.
* Returns: IPCOM_SUCCESS or an IPCOM_ERR_XXX error code.
*/
IP_PUBLIC Ip_err
iptftpc_create(void)
{
return IPCOM_SUCCESS;
}
/*
*===========================================================================
* iptftpc_start
*===========================================================================
* Description: TFTP Client start function.
* Parameters: none.
* Returns: IPCOM_SUCCESS or an IPCOM_ERR_XXX error code.
*/
IP_PUBLIC Ip_err
iptftpc_start(void)
{
return IPCOM_SUCCESS;
}
/*
*===========================================================================
* iptftpc_version
*===========================================================================
* Description: Get TFTP client version string.
* Parameters: none.
* Returns: TFTP client version.
*/
IP_PUBLIC const char *
iptftpc_version(void)
{
return "@(#) IPTFTPC $Name: VXWORKS_ITER40_2019102207 $ - INTERPEAK_COPYRIGHT_STRING";
}
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
@@ -0,0 +1,239 @@
/*
******************************************************************************
* SOURCE FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: iptftpc_cmd_tftp.c,v $ $Revision: 1.6.10.1 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/iptftpc_cmd_tftp.c,v $
* $Author: jren $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#define IPCOM_USE_CLIB_PROTO
#include <ipcom_clib.h>
#include <ipcom_getopt.h>
#include "iptftp.h"
#include "iptftp_h.h"
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
****************************************************************************
* 6 EXTERN PROTOTYPES
****************************************************************************
*/
/*
****************************************************************************
* 7 LOCAL PROTOTYPES
****************************************************************************
*/
IP_PUBLIC int iptftp_cmd_tftp(int argc, char **argv);
/*
****************************************************************************
* 8 DATA
****************************************************************************
*/
/*
****************************************************************************
* 9 STATIC FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* 10 GLOBAL FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* 11 PUBLIC FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* iptftp_cmd_tftp
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_PUBLIC int
iptftp_cmd_tftp(int argc, char **argv)
{
Ip_bool binary = IP_TRUE;
char *src = IP_NULL, *dst = IP_NULL;
Ip_s32 i;
Ip_u16 operation = IPTFTP_OPCODE_RRQ;
Ip_err ret;
Ipcom_getopt opt;
int port = 0;
int command_index;
int src_index;
int dst_index;
int port_index;
ipcom_getopt_clear_r(&opt);
while ((i = ipcom_getopt_r(argc, argv, "ac:", &opt)) != -1)
{
switch (i)
{
case 'a':
binary = IP_FALSE;
break;
#ifdef IPTESTENGINE
case 'c':
iptftp_create_file(argv[opt.optind - 1]);
return 0;
#endif
default:
ipcom_printf("Unknown switch"IP_LF);
return -1;
}
}
if (argc - opt.optind < 3)
{
/* Interactive mode not supported */
goto usage;
}
/* Get port index */
port_index = opt.optind+1;
/* Get the operation */
if (port_index < argc)
{
port = ipcom_atoi(argv[port_index]);
/* no port specified */
if((port <= 0) || (port > 0xFFFF))
{
command_index = opt.optind + 1;
src_index = opt.optind + 2;
dst_index = opt.optind + 3;
port = 0;
}
else
{
command_index = opt.optind + 2;
src_index = opt.optind + 3;
dst_index = opt.optind + 4;
}
}
else
{
goto usage;
}
if (command_index < argc)
{
if (!ipcom_strcmp(argv[command_index], "GET") || !ipcom_strcmp(argv[command_index], "get"))
operation = IPTFTP_OPCODE_RRQ;
else if (!ipcom_strcmp(argv[command_index], "PUT") || !ipcom_strcmp(argv[command_index], "put"))
operation = IPTFTP_OPCODE_WRQ;
else
goto usage;
}
/* Get the source file and optionally the destination file */
if (src_index < argc)
src = argv[src_index];
if (dst_index < argc)
dst = argv[dst_index];
/* Call the tftp api */
if (operation == IPTFTP_OPCODE_RRQ)
ret = iptftp_client_get(argv[opt.optind], src, dst, binary, IP_TRUE, port);
else
ret = iptftp_client_put(argv[opt.optind], src, dst, binary, IP_TRUE, port);
switch(ret)
{
case IPTFTP_SUCCESS:
break;
case IPTFTP_ERR_SERVER_ADDR:
ipcom_printf("Unknown host: %s"IP_LF, argv[opt.optind]);
break;
case IPTFTP_ERR_MEMORY_FAIL:
ipcom_printf("Out of memory."IP_LF);
break;
case IPTFTP_ERR_FILE_OPEN:
if (operation == IPTFTP_OPCODE_RRQ)
ipcom_printf("Could not create file: %s"IP_LF, dst == IP_NULL ? src : dst);
else
ipcom_printf("Could not open file: %s"IP_LF, src);
break;
case IPTFTP_ERR_FILE_READ:
ipcom_printf("Could not read from file: %s"IP_LF, src);
break;
case IPTFTP_ERR_FILE_WRITE:
ipcom_printf("Could not write to file: %s"IP_LF, dst == IP_NULL ? src : dst);
break;
case IPTFTP_ERR_TRANSFER_TIMEOUT:
ipcom_printf("Timeout in TFTP transfer."IP_LF);
break;
case IPTFTP_ERR_SOCKET_FAIL:
ipcom_printf("Socket operation failed."IP_LF);
break;
case IPTFTP_ERR_ILLEGAL_MESSAGE:
ipcom_printf("Illegal TFTP message received."IP_LF);
break;
case IPTFTP_ERR_ERROR_MESSAGE:
ipcom_printf("Received error message during transfer."IP_LF);
break;
default:
break;
}
return 0;
usage:
ipcom_printf("usage: tftp [-a] <host [port]> <get|put> <source> [destination]"IP_LF);
return 0;
}
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
+924
View File
@@ -0,0 +1,924 @@
/*
******************************************************************************
* SOURCE FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: iptftps.c,v $ $Revision: 1.25.10.6 $
* $Source: /home/interpeak/CVSRoot/ipappl/src/iptftps.c,v $
* $Author: jren $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
* TFTP server implementation.
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
#include "ipcom_config.h"
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#define IPCOM_USE_CLIB_PROTO
#include <ipcom_ipd.h>
#include "iptftp_h.h"
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/* Specifies if TFTP write requests are permitted */
#define IPTFTP_SERVER_ALLOW_UPLOAD
/* Specifies if TFTP read requests are permitted */
#define IPTFTP_SERVER_ALLOW_DOWNLOAD
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
****************************************************************************
* 6 EXTERN PROTOTYPES
****************************************************************************
*/
/*
****************************************************************************
* 7 LOCAL PROTOTYPES
****************************************************************************
*/
IP_PUBLIC Ip_err iptftps_create(void);
IP_PUBLIC Ip_err iptftps_start(void);
IP_PUBLIC const char *iptftps_version(void);
IP_STATIC IPCOM_PROCESS(iptftps);
IP_STATIC Ip_err iptftp_server_spawn_handler(Iptftp *tftp);
IP_STATIC Ip_err iptftp_server_parse_request(Ip_fd sock, Ip_s32 family, Iptftp *tftp);
IP_STATIC void iptftp_server_send_file(Iptftp *tftp);
IP_STATIC void iptftp_server_receive_file(Iptftp *tftp);
IP_STATIC IPCOM_PROCESS(iptftp_server_handler);
/*
****************************************************************************
* 8 DATA
****************************************************************************
*/
/*
****************************************************************************
* 9 LOCAL FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* iptftp_server_handler
*===========================================================================
* Description: Spawned TFTP handler.
* Parameters: none.
* Returns: nothing.
*/
IP_STATIC
IPCOM_PROCESS(iptftp_server_handler)
{
Ip_bool ipc_opened = IP_FALSE;
Ipcom_ipc ipc;
Iptftp *ipc_tftp = IP_NULL;
ipcom_proc_init();
if (ipcom_ipc_open(&ipc, "iptftps", -1) != IPCOM_SUCCESS)
{
IPCOM_LOG0(ERR, "iptftp_server_handler :: ipcom_ipc_open(\"ipwebsd\") failed");
goto exit;
}
ipc_opened = IP_TRUE;
ipc_tftp = ipcom_ipc_malloc(sizeof(*ipc_tftp));
if (ipc_tftp == IP_NULL)
{
IPCOM_LOG0(ERR, "iptftp_server_handler :: ipcom_ipc_malloc() failed");
goto exit;
}
/* Get info from tftp server. */
if (ipcom_ipc_send(&ipc, ipc_tftp) != IPCOM_SUCCESS)
{
IPCOM_LOG0(ERR, "iptftp_server_handler :: ipcom_ipc_send() failed");
goto exit;
}
ipc_tftp = IP_NULL;
/* Wait for reply. */
if (ipcom_ipc_receive(IP_NULL, &ipc_tftp, -1) != IPCOM_SUCCESS)
{
IPCOM_LOG0(ERR, "iptftp_server_handler :: ipcom_ipc_receive() failed");
goto exit;
}
if (ipc_tftp->opcode == IPTFTP_OPCODE_RRQ)
{
iptftp_server_send_file(ipc_tftp);
}
else if (ipc_tftp->opcode == IPTFTP_OPCODE_WRQ)
{
iptftp_server_receive_file(ipc_tftp);
}
exit:
if (ipc_opened)
(void)ipcom_ipc_close(&ipc);
if (ipc_tftp != IP_NULL)
ipcom_ipc_free(ipc_tftp);
ipcom_proc_exit();
}
/*
*===========================================================================
* iptftp_server_spawn_handler
*===========================================================================
* Description: Called by the tftp server to spawn a new handler for each
* connection
* Parameters: sock - The socket the connection arrived on.
* family - The Internet address family.
* srv_handle - handle for ipcom call messages
* Returns: IPCOM_SUCCESS or IPCOM_ERR_FAILED
*/
IP_STATIC Ip_err
iptftp_server_spawn_handler(Iptftp *tftp)
{
/* Spawn a tftp handler */
if (ipcom_proc_create("iptftp_server_handler", (Ipcom_proc_func)iptftp_server_handler, IPCOM_PROC_STACK_DEFAULT, IP_NULL))
{
IPCOM_LOG0(ERR, "iptftp_server_spawn_handler :: ipcom_proc_create() failed");
goto err;
}
else
{
/* Wait for a message from the handler */
Iptftp *ipc_tftp;
if (ipcom_ipc_receive(IP_NULL, &ipc_tftp, -1) != IPCOM_SUCCESS)
{
IPCOM_LOG0(ERR, "iptftp_server_spawn_handler :: ipcom_ipc_receive() failed");
goto err;
}
/* Respond to handler */
ipcom_memcpy(ipc_tftp, tftp, sizeof(*tftp));
if (ipcom_ipc_send(IPCOM_IPC_SENDER, ipc_tftp) != IPCOM_SUCCESS)
{
ipcom_ipc_free(ipc_tftp);
IPCOM_LOG0(ERR, "iptftp_server_spawn_handler :: ipcom_ipc_send() failed");
goto err;
}
}
return IPCOM_SUCCESS;
err:
return IPCOM_ERR_FAILED;
}
/*
*===========================================================================
* iptftp_server_dirindex
*===========================================================================
*
* Description:
* Calculate level of directory path.
*
* Parameters:
* path Directory path.
*
* Returns:
* minimum subdirectory level.
*/
IP_STATIC int
iptftp_server_dirindex(char *path)
{
int level = 0;
int min = 0;
char *start;
char *stop;
char *cp;
cp = path;
while (*cp != '\0')
{
char c;
start = cp;
cp += ipcom_strcspn(cp, IPTFTP_DIR_SEP_STRING);
stop = cp;
cp += ipcom_strspn(cp, IPTFTP_DIR_SEP_STRING);
c = *stop;
*stop = '\0';
if (ipcom_strcmp(start, "..") == 0)
{
level--;
}
else if (*start != '\0' && ipcom_strcmp(start, ".") != 0)
{
level++;
}
*stop = c;
if (level < min)
{
min = level;
}
}
return min;
}
/*
*===========================================================================
* iptftp_server_parse_request
*===========================================================================
* Description: Parses a tftp request and calls the request handler
* Parameters: sock - socket descriptor the request arrived on.
* family - Internet address family for the request.
* tftp - TFTP structure.
* Returns: IPCOM_SUCCESS if ok and an IPCOM_ERR_XXX error code for
* failures.
*/
IP_STATIC Ip_err
iptftp_server_parse_request(Ip_fd sock, Ip_s32 family, Iptftp *tftp)
{
#ifdef IPCOM_USE_INET
struct Ip_sockaddr_in sa;
#endif
#ifdef IPCOM_USE_INET6
struct Ip_sockaddr_in6 sa6;
#endif
char buf[IPTFTP_MAX_PACKET];
Ip_s32 i, len;
char *err_msg = IP_NULL;
struct Ip_msghdr msghdr;
struct Ip_iovec iov;
Ip_err ret = IPCOM_SUCCESS;
/* Get the message from the socket */
tftp->controllen = sizeof(tftp->controlbuf);
msghdr.msg_controllen = tftp->controllen;
msghdr.msg_control = tftp->controlbuf;
msghdr.msg_flags = 0;
msghdr.msg_iov = &iov;
msghdr.msg_iovlen = 1;
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
/* Get this connection's first packet */
switch (family)
{
#ifdef IPCOM_USE_INET
case IP_AF_INET:
msghdr.msg_name = &sa;
msghdr.msg_namelen = sizeof(sa);
len = (Ip_s32)ipcom_recvmsg(sock, &msghdr, 0);
if (len < 0)
{
ret = IPCOM_ERR_FAILED;
goto exit;
}
tftp->peer_port = ip_ntohs(sa.sin_port);
ipcom_memcpy(&tftp->peer_addr.addr, &sa.sin_addr, sizeof(tftp->peer_addr.addr));
tftp->family = IP_AF_INET;
break;
#endif /* IPCOM_USE_INET */
#ifdef IPCOM_USE_INET6
case IP_AF_INET6:
msghdr.msg_name = &sa6;
msghdr.msg_namelen = sizeof(sa6);
len = (Ip_s32)ipcom_recvmsg(sock, &msghdr, 0);
if (len < 0)
{
ret = IPCOM_ERR_FAILED;
goto exit;
}
tftp->peer_port = ip_ntohs(sa6.sin6_port);
ipcom_memcpy(&tftp->peer_addr.addr6, &sa6.sin6_addr, sizeof(tftp->peer_addr.addr6));
tftp->ifindex = sa6.sin6_scope_id;
tftp->family = IP_AF_INET6;
break;
#endif /* IPCOM_USE_INET6 */
default:
ret = IPCOM_ERR_FAILED;
goto exit;
}
/* Sanity checking */
if (len < 6)
{
/* Not enough characters */
err_msg = IPTFTP_ERRMSG_INCOMPLETEMSG;
goto exit;
}
if (buf[len-1] != '\0')
{
/* No null char at end */
err_msg = IPTFTP_ERRMSG_INCOMPLETEMSG;
goto exit;
}
for (i = 2; i < len; i++)
if (buf[i] == '\0')
break;
if (i < 3)
{
/* First char is null char */
err_msg = IPTFTP_ERRMSG_INCOMPLETEMSG;
goto exit;
}
if (i > (len-3))
{
/* Only one null char or two trailing null chars */
err_msg = IPTFTP_ERRMSG_INCOMPLETEMSG;
goto exit;
}
/* Check opcode */
tftp->opcode = (Ip_u16)(buf[0]*256 + buf[1]);
if (tftp->opcode != IPTFTP_OPCODE_RRQ && tftp->opcode != IPTFTP_OPCODE_WRQ)
{
err_msg = IPTFTP_ERRMSG_INVALIDOPCODE;
goto exit;
}
/* Check mode */
if (!ipcom_strcasecmp(&buf[i+1], "netascii"))
tftp->binary = IP_FALSE;
else if (!ipcom_strcasecmp(&buf[i+1], "octet"))
tftp->binary = IP_TRUE;
else
{
err_msg = IPTFTP_ERRMSG_INVALIDMODE;
goto exit;
}
if (tftp->dirlen + 1 + ipcom_strlen(&buf[2]) >= sizeof(tftp->file))
{
ret = IPCOM_ERR_REACHED_MAX;
goto exit;
}
/* Check for trespassing */
if (iptftp_server_dirindex(&buf[2]) < 0)
{
/* Peeked above root directory */
iptftp_send_err(sock, tftp, IPTFTP_ERRCODE_ACCESSVIOLATION,
IPTFTP_ERRMSG_FILEABOVEWORKINGDIR);
return IPCOM_ERR_PERMISSION_DENIED;
}
/* Append filename to server directory */
if (buf[2] == IPTFTP_DIR_SEP)
{
ipcom_strcpy(&tftp->file[tftp->dirlen], &buf[2]);
}
else
{
tftp->file[tftp->dirlen] = IPTFTP_DIR_SEP;
ipcom_strcpy(&tftp->file[tftp->dirlen+1], &buf[2]);
}
exit:
if (err_msg != IP_NULL)
iptftp_send_err(sock, tftp, IPTFTP_ERRCODE_ILLEGALTFTPOP, err_msg);
return ret;
}
/*
*===========================================================================
* iptftp_server_send_file
*===========================================================================
* Description: TFTP server internal send function.
* Parameters: tftp - TFTP structure.
* Returns: nothing.
*/
IP_STATIC void
iptftp_server_send_file(Iptftp *tftp)
{
Ip_fd sock;
IP_FILE *fp = IP_NULL;
char *err_msg = IP_NULL;
Ip_u16 err_code = 0;
/* Get a udp socket, and bind to port 0 */
sock = ipcom_socket(tftp->family, IP_SOCK_DGRAM, 0);
if (sock == IP_INVALID_SOCKET)
{
IPCOM_LOG0(ERR, "iptftp_server_send_file :: ipcom_socket() failed");
goto err;
}
if (iptftp_bind_zero(sock, tftp->family) != IPCOM_SUCCESS)
goto err;
#ifdef IPTFTP_SERVER_ALLOW_DOWNLOAD
/* Open the file */
if (tftp->binary)
fp = ipcom_fopen(tftp->file, "rb");
else
fp = ipcom_fopen(tftp->file, "r");
#endif
if (!fp)
{
IPCOM_LOG1(ERR, "iptftp_server_send_file :: Could not open file: \"%s\"", tftp->file);
err_code = IPTFTP_ERRCODE_FILENOTFOUND;
err_msg = IPTFTP_ERRMSG_COULDNOTOPENFILE;
goto err;
}
/* Start the send loop */
(void)iptftp_send_loop(sock, fp, &err_msg, &err_code, tftp);
err:
/* Send error */
if (err_msg)
iptftp_send_err(sock, tftp, err_code, err_msg);
/* Terminate connection */
if (fp != IP_NULL)
(void)ipcom_fclose(fp);
if (sock != IP_INVALID_SOCKET)
ipcom_socketclose(sock);
}
/*
*===========================================================================
* iptftp_server_receive_file
*===========================================================================
* Description: TFTP server internal receive function.
* Parameters: tftp - TFTP structure.
* Returns: nothing.
*/
IP_STATIC void
iptftp_server_receive_file(Iptftp *tftp)
{
Ip_fd sock;
IP_FILE *fp = IP_NULL;
Ip_u16 err_code = 0;
char *err_msg = IP_NULL;
char *buf = IP_NULL;
/* Get a udp socket, and bind to port 0 */
sock = ipcom_socket(tftp->family, IP_SOCK_DGRAM, 0);
if (sock == IP_INVALID_SOCKET)
{
IPCOM_LOG0(ERR, "iptftp_server_receive_file :: ipcom_socket() failed");
goto err;
}
if (iptftp_bind_zero(sock, tftp->family) != IPCOM_SUCCESS)
goto err;
#ifdef IPTFTP_SERVER_ALLOW_UPLOAD
/* Create the file */
buf = ipcom_sysvar_get ("iptftps.allow.upload", IP_NULL, IP_NULL);
if (buf)
{
if(ipcom_strcasecmp(buf,"enable")==0)
{
if (tftp->binary)
fp = ipcom_fopen(tftp->file, "wb");
else
fp = ipcom_fopen(tftp->file, "w");
}
ipcom_free(buf);
buf=IP_NULL;
}
#endif
if (!fp)
{
IPCOM_LOG1(ERR, "iptftp_server_receive_file :: Could not create file: \"%s\"", tftp->file);
err_code = IPTFTP_ERRCODE_ACCESSVIOLATION;
err_msg = IPTFTP_ERRMSG_COULDNOTCREATEFILE;
goto err;
}
/* Start the receive loop */
(void)iptftp_receive_loop(sock, fp, &err_msg, &err_code, tftp);
err:
/* Send error */
if (err_msg)
iptftp_send_err(sock, tftp, err_code, err_msg);
/* Terminate connection */
if (fp != IP_NULL)
(void)ipcom_fclose(fp);
if (sock != IP_INVALID_SOCKET)
ipcom_socketclose(sock);
}
/*
*===========================================================================
* iptftp_server_init
*===========================================================================
* Description: Initialize TFTP server.
* Parameters: tftp - TFTP structure.
* Returns: IPCOM_SUCCESS or an IPCOM_ERR_XXX error code for failures.
*/
IP_STATIC Ip_err
iptftp_server_init(Iptftp *tftp)
{
Ip_err ret = IPCOM_SUCCESS;
char *dir;
struct Ip_stat stat_buf;
/* Create the default directory */
dir = ipcom_sysvar_get("iptftp.dir", IP_NULL, IP_NULL);
if (dir == IP_NULL)
{
ipcom_strcpy(tftp->file, IPCOM_FILE_ROOT "tftpdir");
}
else if (ipcom_strlen(dir) >= sizeof(tftp->file))
{
IPCOM_LOG1(ERR, "TFTP server directory \"%s\" is too long!", dir);
ret = IPCOM_ERR_FAILED;
goto exit;
}
else
{
ipcom_strcpy(tftp->file, dir);
}
tftp->dirlen = (Ip_s32)ipcom_strlen(tftp->file);
/* Create the default TFTP directory if it does not exist. */
if (ipcom_stat(tftp->file, &stat_buf) != 0)
{
if (ipcom_mkdir(tftp->file, 0777) == 0)
{
IPCOM_LOG1(INFO, "created TFTP server directory \"%s\"",
tftp->file);
}
else
{
IPCOM_LOG2(ERR, "iptftp_server_init :: ipcom_mkdir(\"%s\") - %s",
tftp->file, ipcom_strerror(ipcom_errno));
ret = IPCOM_ERR_FAILED;
}
}
exit:
if (dir != IP_NULL)
ipcom_free(dir);
return ret;
}
/*
*===========================================================================
* iptftps
*===========================================================================
* Description: TFTP server damon. Listens for connections and spawns
* child processes for each connection.
* Parameters: none.
* Returns: nothing.
*/
IP_STATIC
IPCOM_PROCESS(iptftps)
{
#ifdef IPCOM_USE_INET
struct Ip_sockaddr_in sa;
Ip_fd sock = IP_INVALID_SOCKET;
#endif
#ifdef IPCOM_USE_INET6
struct Ip_sockaddr_in6 sa6;
Ip_fd sock6 = IP_INVALID_SOCKET;
#endif
Ip_fd max_sock, ipd_sock = IP_INVALID_SOCKET;
Ip_fd_set *read_set = IP_NULL;
Iptftp *tftp;
Ip_err err;
int on = 1;
ipcom_proc_init();
tftp = ipcom_malloc(sizeof(*tftp));
if (tftp == IP_NULL)
{
err = IPCOM_ERR_NO_MEMORY;
IPCOM_LOG0(ERR, "iptftps :: ipcom_malloc(tftp) failed");
goto fail;
}
ipcom_memset(tftp, 0, sizeof(*tftp));
/* Install IPC. */
err = ipcom_ipc_install();
if (err != IPCOM_SUCCESS)
{
IPCOM_LOG0(ERR, "iptftps :: ipcom_ipc_install() failed");
goto fail;
}
/* Initialize tftp server */
err = iptftp_server_init(tftp);
if (err != IPCOM_SUCCESS)
{
IPCOM_LOG0(ERR, "iptftps :: iptftp_server_init() failed");
goto fail;
}
/* Get read set. */
read_set = ipcom_malloc(sizeof(Ip_fd_set));
if (read_set == IP_NULL)
{
err = IPCOM_ERR_NO_MEMORY;
IPCOM_LOG0(ERR, "iptftps :: ipcom_malloc(read_set) failed");
goto fail;
}
err = IPCOM_ERR_SOCKCALLFAILED;
#ifdef IPCOM_USE_INET
/* Get a udp socket, bind to the tftp port */
sock = ipcom_socket(IP_AF_INET, IP_SOCK_DGRAM, 0);
if (sock == IP_INVALID_SOCKET)
{
IPCOM_LOG0(ERR, "iptftps :: ipcom_socket() failed");
#ifndef IPCOM_USE_INET6
goto fail;
#endif
}
else
{
/* Enable ancillary data */
if (ipcom_setsockopt(sock, IP_IPPROTO_IP, IP_IP_PKTINFO, &on, sizeof(on)) < 0)
{
#ifndef IPLITE
IPCOM_LOG2(INFO, "iptftps :: ipcom_setsockopt(IP_IP_PKTINFO) failed, errno = %d (%s)",
ipcom_errno, ipcom_strerror(ipcom_errno));
#endif
}
ipcom_memset(&sa, 0, sizeof(sa));
IPCOM_SA_LEN_SET(&sa, sizeof(struct Ip_sockaddr_in));
sa.sin_family = IP_AF_INET;
sa.sin_port = ip_htons(IPTFTP_PORT);
sa.sin_addr.s_addr = IP_INADDR_ANY;
if (ipcom_bind(sock, (struct Ip_sockaddr *)&sa, sizeof(sa)) == IP_SOCKERR)
{
IPCOM_LOG1(ERR, "iptftps :: ipcom_bind() failed, errno = %d", ipcom_errno);
#ifdef IPCOM_USE_INET6
ipcom_socketclose(sock);
sock = IP_INVALID_SOCKET;
#else
goto fail;
#endif
}
}
#endif /* IPCOM_USE_INET */
#ifdef IPCOM_USE_INET6
/* Get a udp v6 socket, bind to the tftp port */
sock6 = ipcom_socket(IP_AF_INET6, IP_SOCK_DGRAM, 0);
if (sock6 == IP_INVALID_SOCKET)
{
IPCOM_LOG0(ERR, "iptftps :: ipcom_socket(INET6) failed");
#ifndef IPCOM_USE_INET
goto fail;
#endif
}
else
{
/* Enable ancillary data */
if (ipcom_setsockopt(sock6, IP_IPPROTO_IPV6, IP_IPV6_RECVPKTINFO, &on, sizeof(on)) < 0)
{
#ifndef IPLITE
IPCOM_LOG2(INFO, "iptftps :: ipcom_setsockopt(IP_IPV6_RECVPKTINFO) failed, errno = %d (%s)",
ipcom_errno, ipcom_strerror(ipcom_errno));
#endif
}
ipcom_memset(&sa6, 0, sizeof(sa6));
IPCOM_SA_LEN_SET(&sa6, sizeof(sa6));
sa6.sin6_family = IP_AF_INET6;
sa6.sin6_port = ip_htons(IPTFTP_PORT);
if (ipcom_bind(sock6, (struct Ip_sockaddr *)&sa6, sizeof(sa6)) == IP_SOCKERR)
{
IPCOM_LOG0(ERR, "iptftps :: ipcom_bind(INET6) failed");
#ifdef IPCOM_USE_INET
ipcom_socketclose(sock6);
sock6 = IP_INVALID_SOCKET;
#else
goto fail;
#endif
}
}
#endif /* IPCOM_USE_INET6 */
/* IPD init. */
if (ipcom_ipd_init("iptftps", IPCOM_SUCCESS, &ipd_sock) != IPCOM_SUCCESS)
{
IPCOM_LOG0 (ERR, "iptftps :: ipcom_ipd_init() failed");
goto fail;
}
max_sock = ipd_sock;
#ifdef IPCOM_USE_INET
max_sock = IP_MAX(max_sock, sock);
#endif
#ifdef IPCOM_USE_INET6
max_sock = IP_MAX(max_sock, sock6);
#endif
max_sock++;
/* Start the iptftps select loop */
IPCOM_LOG0(DEBUG, "iptftps :: listening for connections");
for (;;)
{
Ip_s32 num;
IP_FD_ZERO(read_set);
IP_FD_SET(ipd_sock, read_set);
#ifdef IPCOM_USE_INET
if (sock != IP_INVALID_SOCKET)
IP_FD_SET(sock, read_set);
#endif
#ifdef IPCOM_USE_INET6
if (sock6 != IP_INVALID_SOCKET)
IP_FD_SET(sock6, read_set);
#endif
/* Wait for a connection */
num = ipcom_socketselect(max_sock, read_set, IP_NULL, IP_NULL, IP_NULL);
if (num == IP_SOCKERR)
{
IPCOM_LOG1(ERR, "iptftps :: ipcom_socketselect() failed, errno = %d", ipcom_errno);
goto fail;
}
#ifdef IPCOM_USE_INET
/* Check for connection on ipv4 socket*/
if (num > 0 && sock != IP_INVALID_SOCKET && IP_FD_ISSET(sock, read_set))
{
num--;
if (iptftp_server_parse_request(sock, IP_AF_INET, tftp) == IPCOM_SUCCESS)
{
if (iptftp_server_spawn_handler(tftp) != IPCOM_SUCCESS)
IPCOM_LOG0(ERR, "iptftps :: iptftp_server_spawn_handler failed");
}
else
IPCOM_LOG0(ERR, "iptftps :: iptftp_server_parse_request(INET4) failed");
}
#endif /* IPCOM_USE_INET */
#ifdef IPCOM_USE_INET6
/* Check for connection on ipv6 socket*/
if (num > 0 && sock6 != IP_INVALID_SOCKET && IP_FD_ISSET(sock6, read_set))
{
num--;
if (iptftp_server_parse_request(sock6, IP_AF_INET6, tftp) == IPCOM_SUCCESS)
{
if (iptftp_server_spawn_handler(tftp) != IPCOM_SUCCESS)
IPCOM_LOG0(ERR, "iptftps :: iptftp_server_spawn_handler() failed");
}
else
IPCOM_LOG0(ERR, "iptftps :: iptftp_server_parse_request(INET6) failed");
}
#endif /* IPCOM_USE_INET6 */
/* Check for connection on ipd socket*/
if (num > 0 && IP_FD_ISSET(ipd_sock, read_set))
{
int event;
event = ipcom_ipd_input(ipd_sock);
switch(event)
{
case IPCOM_IPD_EVENT_RECONFIGURE:
IPCOM_LOG0(INFO, "received IPD reconfigure message");
/* Initialize tftp server */
if (iptftp_server_init(tftp) != IPCOM_SUCCESS)
{
IPCOM_LOG0(ERR, "iptftps :: iptftp_server_init() failed");
goto fail;
}
break;
case IPCOM_IPD_EVENT_KILL:
IPCOM_LOG0(INFO, "received IPD kill message");
goto fail;
default:
break;
}
}
}
fail:
if (tftp != IP_NULL)
ipcom_free(tftp);
if (read_set != IP_NULL)
ipcom_free(read_set);
#ifdef IPCOM_USE_INET
if (sock != IP_INVALID_SOCKET)
ipcom_socketclose(sock);
#endif
#ifdef IPCOM_USE_INET6
if (sock6 != IP_INVALID_SOCKET)
ipcom_socketclose(sock6);
#endif
if (ipd_sock == IP_INVALID_SOCKET)
{
ip_assert(err != IPCOM_SUCCESS);
(void)ipcom_ipd_init("iptftps", err, &ipd_sock);
}
if (ipd_sock != IP_INVALID_SOCKET)
(void)ipcom_ipd_exit("iptftps", ipd_sock);
(void)ipcom_ipc_uninstall();
ipcom_proc_exit();
}
/*
****************************************************************************
* 10 PUBLIC FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* iptftps_create
*===========================================================================
* Description: TFTP server create function.
* Parameters: none.
* Returns: IPCOM_SUCCESS or an IPCOM_ERR_XXX error code.
*/
IP_PUBLIC Ip_err
iptftps_create(void)
{
return IPCOM_SUCCESS;
}
/*
*===========================================================================
* iptftps_start
*===========================================================================
* Description: TFTP server start function.
* Parameters: none.
* Returns: IPCOM_SUCCESS or an IPCOM_ERR_XXX error code.
*/
IP_PUBLIC Ip_err
iptftps_start(void)
{
return ipcom_proc_create("iptftps", (Ipcom_proc_func)iptftps, IPCOM_PROC_STACK_DEFAULT, IP_NULL);
}
/*
*===========================================================================
* iptftps_version
*===========================================================================
* Description: Get TFTP server version string.
* Parameters: none.
* Returns: TFTP server version.
*/
IP_PUBLIC const char *
iptftps_version(void)
{
return "@(#) IPTFTPS $Name: VXWORKS_ITER40_2019102207 $ - INTERPEAK_COPYRIGHT_STRING";
}
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/