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,360 @@
/*
******************************************************************************
* SOURCE FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_RCPL8_2024030303 $ $RCSfile: ipsntp_cmd_sntp.c,v $ $Revision: 1.21.12.7 $
* $Source: /home/interpeak/CVSRoot/ipsntp/src/ipsntp_cmd_sntp.c,v $
* $Author: jren $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
* SNTP shell command.
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
#include "ipsntp_config.h"
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#define IPCOM_USE_CLIB_PROTO
#include <ipcom_type.h>
#include <ipcom_clib.h>
#include <ipcom_getopt.h>
#include <ipcom_err.h>
#include "ipsntp.h"
#include "ipsntp_h.h"
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
****************************************************************************
* 6 EXTERN PROTOTYPES
****************************************************************************
*/
IP_PUBLIC const char *ipsntp_version(void);
#ifdef IP_PORT_INTEGRITY
IP_EXTERN int ipcom_adjtime(IP_CONST struct Ip_timeval *delta,
struct Ip_timeval *olddelta);
IP_EXTERN int ipsntp_timeval_subtime(struct Ip_timeval *tid1,
struct Ip_timeval *tid2,
struct Ip_timeval *out);
#endif
/*
****************************************************************************
* 7 LOCAL PROTOTYPES
****************************************************************************
*/
/*
****************************************************************************
* 8 DATA
****************************************************************************
*/
/*
****************************************************************************
* 9 LOCAL FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* ipsntp_sntp_print_usage
*===========================================================================
* Description: Prints usage information for the SNTP shell command.
* Parameters: None.
* Returns: Nothing.
*/
IP_STATIC void
ipsntp_sntp_print_usage(void)
{
ipcom_printf("\n");
ipcom_printf("NAME\n");
ipcom_printf(" sntp - set the local date and time via (S)NTP\n");
ipcom_printf("\n");
ipcom_printf("SYNOPSIS\n");
ipcom_printf(" sntp [-v] [-b] [-i <ifname>] [-d] [-t timeout] [-p port] [server|mcastaddr]\n");
ipcom_printf("\n");
ipcom_printf("DESCRIPTION\n");
ipcom_printf(" sntp polls an (S)NTP server for the current date and time until\n");
ipcom_printf(" a response has been received or the timeout period expires. If a\n");
ipcom_printf(" valid response was received the local date and time is updated.\n");
ipcom_printf(" sntp can also be used to wait for a (S)NTP server multicast or broadcast.\n");
ipcom_printf("\n");
ipcom_printf(" -v Show IPSNTP product version.\n");
ipcom_printf(" -b Wait for (S)NTP server IPv4 broadcast.\n");
ipcom_printf(" -i ifname\n");
ipcom_printf(" Wait for (S)NTP server multicast on specified interface.\n");
ipcom_printf(" -d Debug only. Do not update the local time, just print status.\n");
ipcom_printf(" -t timeout\n");
ipcom_printf(" Specifies the maximum time (in seconds) to wait for an answer from the (S)NTP\n");
ipcom_printf(" server. A timeout value of -1 means that the program will wait forever.\n");
ipcom_printf(" Default is 3 seconds.\n");
ipcom_printf(" -p port\n");
ipcom_printf(" Specifies the (S)NTP server's listening port. Default is 123.\n");
ipcom_printf("\n");
ipcom_printf("RETURN VALUES\n");
ipcom_printf(" The sntp command will exit with 0 on success, and non-zero on errors.\n");
}
/*
****************************************************************************
* 10 GLOBAL FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* ipsntp_cmd_sntp
*===========================================================================
* Description: SNTP shell command.
* Parameters: argc - number of arguments.
* argv - list of arguments.
* Returns: 0 = ok.
* -1 = error.
*/
IP_GLOBAL int
ipsntp_cmd_sntp(int argc, char **argv)
{
int i, timeout = 3, port = 123;
Ip_bool multicast = IP_FALSE, broadcast = IP_FALSE, debug = IP_FALSE;
struct Ip_timespec tv;
#ifndef IP_PORT_VXWORKS
struct Ip_timeval tv_old;
#endif
#ifdef IP_PORT_INTEGRITY
struct Ip_timeval now, diff;
int sign;
#endif
Ip_err ret;
char ifname[IP_IFNAMSIZ];
char *ifnp = IP_NULL, *maddr = IP_NULL;
if(argc <= 1)
{
goto usage;
}
ipcom_getopt_clear();
while ((i = ipcom_getopt(argc, argv, "vbi:dt:p:rs")) != -1)
{
switch (i)
{
case 'v':
ipcom_printf("%s\n", ipsntp_version());
return 0;
case 'b':
broadcast = IP_TRUE;
break;
case 'i':
if (ipcom_strlen(ip_optarg) > IP_IFNAMSIZ - 1)
{
ipcom_printf("Too long interface name: %s!\n", ip_optarg);
return -1;
}
ipcom_strcpy(ifname, ip_optarg);
multicast = IP_TRUE;
break;
case 'd':
debug = IP_TRUE;
break;
case 't':
timeout = ipcom_atoi(ip_optarg);
if(timeout < -1)
{
ipcom_printf("Invalid timeout!\n");
return -1;
}
break;
case 'p':
port = ipcom_atoi(ip_optarg);
if(port < 1 || port > 65535)
{
ipcom_printf("Invalid port!\n");
return -1;
}
break;
#ifdef IPSNTP_USE_TEST
case 'r':
{
struct Ip_timeval tv_test;
/* Test command used to set the server reference time to the current time
and the reference id to 1.2.3.4 */
(void)ipcom_gettimeofday(&tv_test, IP_NULL);
tv.tv_sec = (Ip_time_t)tv_test.tv_sec;
tv.tv_nsec = tv_test.tv_usec * 1000;
ipsntp_set_reference(&tv, ip_htonl(0x01020304));
ipcom_printf("Server reference set.\n");
return 0;
}
case 's':
/* Test command used to set the server precision */
ipsntp_set_precision(-6);
ipcom_printf("Server precision set.\n");
return 0;
#endif
default:
return -1;
}
}
if (broadcast && multicast)
{
ipcom_printf("Do not specify both broadcast and multicast!\n");
return -1;
}
/* Get the hostname or host address or multicast address */
if (broadcast)
{
ifnp = IP_NULL;
maddr = IP_NULL;
}
else if (multicast)
{
if(argv[ip_optind] == IP_NULL)
{
ipcom_printf("No multicast address specified!\n");
return -1;
}
ifnp = ifname;
maddr = argv[ip_optind];
}
else
{
if(argv[ip_optind] == IP_NULL)
{
ipcom_printf("No sntp server specified!\n");
return -1;
}
}
if (broadcast || multicast)
{
ret = ipsntp_wait_time(maddr, ifnp, (Ip_u16)port, timeout, &tv, SNTP_VERSION_1);
}
else
{
ret = ipsntp_query_time(argv[ip_optind], (Ip_u16)port, timeout, &tv, SNTP_VERSION_1);
}
switch(ret)
{
case IPCOM_SUCCESS:
#ifdef _WRS_CONFIG_TIME_T_LONG_LONG
(void)ipcom_printf("(S)NTP time: %llx.%lx, %s",
tv.tv_sec, tv.tv_nsec, ipcom_ctime(&tv.tv_sec));
#else
(void)ipcom_printf("(S)NTP time: %lx.%lx, %s",
tv.tv_sec, tv.tv_nsec, ipcom_ctime(&tv.tv_sec));
#endif
if(debug == IP_FALSE)
{
#ifdef IP_PORT_INTEGRITY
if(ipcom_gettimeofday(&now, IP_NULL) != 0)
return -1;
tv_old.tv_sec = (long)tv.tv_sec;
tv_old.tv_usec = tv.tv_nsec / 1000;
sign = ipsntp_timeval_subtime(&now, &tv_old, &diff);
if(sign == 1)
{
/* now > tv -> Clock is ahead, slow down. */
diff.tv_sec = -diff.tv_sec;
diff.tv_usec = -diff.tv_usec;
}
if(ipcom_adjtime(&diff, IP_NULL) != 0)
#elif IP_PORT_VXWORKS
Ipsntp_rtc_time newtime;
newtime.seconds = tv.tv_sec;
newtime.nanos = (Ip_u32)tv.tv_nsec;
if(ipsntp_clock_settime(&newtime) != IPCOM_SUCCESS)
#else
tv_old.tv_sec = (long)tv.tv_sec;
tv_old.tv_usec = tv.tv_nsec / 1000;
if(ipcom_settimeofday(&tv_old, IP_NULL))
#endif
{
ipcom_printf("Failed to update system time!\n");
ret = 1;
}
else
{
ipcom_printf("System time updated!\n");
}
}
break;
case IPCOM_ERR_FAILED:
ipcom_printf("Invalid (S)NTP message received!\n");
break;
case IPCOM_ERR_INVALID_ARG:
if (broadcast)
ipcom_printf("Invalid arguments!\n");
else if (multicast)
ipcom_printf("Invalid multicast address %s on interface %s!\n", argv[ip_optind], ifname);
else
ipcom_printf("Can't find %s!\n", argv[ip_optind]);
break;
case IPCOM_ERR_TIMEOUT:
ipcom_printf("Timeout. No responses received!\n");
break;
default:
ipcom_printf("Command failed!\n");
break;
}
return ret;
usage:
ipsntp_sntp_print_usage();
return 0;
}
/*
****************************************************************************
* 11 PUBLIC FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
@@ -0,0 +1,296 @@
/*
******************************************************************************
* INTERPEAK INTERNAL API HEADER FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipsntp_h.h,v $ $Revision: 1.21.12.4 $
* $Source: /home/interpeak/CVSRoot/ipsntp/src/ipsntp_h.h,v $
* $Author: jren $ $Date: 2015-08-03 08:44:18 $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
#ifndef IPSNTP_H_H
#define IPSNTP_H_H
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
* Internal header file.
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#include <ipcom_sock.h>
#include <ipcom_tmo.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/* 1970 - 1900 in seconds */
#define IPSNTP_JAN_1970 2208988800UL
/* 2001 - 1900 in seconds */
#define IPSNTP_JAN_2001 3187296000UL
/* NTP fragments to microseconds: 1/sec * x * 10^-6 sec * 2^32 */
#define IPSNTP_MICROFRAC 4295L
/* Start date for system clock */
#define IPSNTP_RTC_START IPSNTP_JAN_1970
/* Seconds per day */
#define IPSNTP_SECONDS_PER_DAY (24*3600)
#define IPSNTP_POSITIVE_LEAP_SECOND 1
#define IPSNTP_NEGATIVE_LEAP_SECOND -1
/* (S)NTP modes. */
#define IPSNTP_RESERVED 0
#define IPSNTP_SYMMETRIC_ACTIVE 1
#define IPSNTP_SYMMETRIC_PASSIVE 2
#define IPSNTP_CLIENT_MODE 3
#define IPSNTP_SERVER_MODE 4
#define IPSNTP_BROADCAST_MODE 5
#define IPSNTP_RESERVED_CONTROL 6
#define IPSNTP_RESERVED_PRIVATE 7
/* Useful macros */
#define IPSNTP_MODE(xxpkt) ((xxpkt)->LVM & 0x07)
#define IPSNTP_VERSION(xxpkt) (((xxpkt)->LVM & 0x38) >> 3)
#define IPSNTP_LEAP(xxpkt) (((xxpkt)->LVM & 0xc0) >> 6)
/* Check version >= 1, no alarm and stratum 1-14 */
#define IPSNTP_CHECK_VERSION_ALARM_STRATUM(xxsntp) \
(IPSNTP_VERSION((xxsntp)->pkt) > 0 && \
IPSNTP_LEAP((xxsntp)->pkt) < 3 && \
(xxsntp)->packet.stratum < 16 && (xxsntp)->packet.stratum > 0)
/* Match response with last request */
#define IPSNTP_MATCH_RESPONSE(xxsntp) \
((xxsntp)->packet.orgTimestamp.sec == (xxsntp)->outputTime.sec && \
(xxsntp)->packet.orgTimestamp.frac == (xxsntp)->outputTime.frac)
/* The maximum number of fallback servers */
#define IPSNTP_NUMSERV 4
/* The delimiter between the address and the interface for scoped addresses
* ex FE80::200:30FF:FE40:5060%eth0
*/
#define IPSNTP_IF_DELIMITER '%'
/* The maximum RTC time difference that can be gracefully adjusted */
#if defined(IP_PORT_OSE) || defined(IP_PORT_OSE5)
#define IPSNTP_MAX_GRACEFUL_ADJUST 2100
#else
#define IPSNTP_MAX_GRACEFUL_ADJUST 0 /* Unsupported if not OSE */
#endif
/* Poll interval for API queries */
#define IPSNTP_QUERY_POLL 2
/* Maximum reasonable roundtrip time before the server time is discarded */
#define IPSNTP_MAX_REASONABLE_ROUND_TIME 5
/*
*===========================================================================
* SYSLOG
*===========================================================================
* Syslog settings
*/
#ifdef IPCOM_SYSLOG_FACILITY
#undef IPCOM_SYSLOG_FACILITY
#endif
#define IPCOM_SYSLOG_FACILITY IPCOM_LOG_IPSNTP
#ifdef IPCOM_SYSLOG_PRIORITY
#undef IPCOM_SYSLOG_PRIORITY
#endif
#if defined(_WRS_CONFIG_IPSNTP_SYSLOG_PRIORITY)
#define IPCOM_SYSLOG_PRIORITY _WRS_CONFIG_IPSNTP_SYSLOG_PRIORITY
#else
#define IPCOM_SYSLOG_PRIORITY IPCOM_LOG_WARNING
#endif
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
*===========================================================================
* Ipsntp_rtc_time
*===========================================================================
*/
typedef struct Ipsntp_rtc_time_struct
{
Ip_time_t seconds; /* Seconds since IPSNTP_RTC_START */
Ip_u32 nanos; /* Additional nanoseconds */
}
Ipsntp_rtc_time;
/*
*===========================================================================
* Ipsntp_ntp_time
*===========================================================================
*/
typedef struct Ipsntp_ntp_time_struct
{
Ip_u32 sec;
Ip_u32 frac;
}
Ipsntp_ntp_time;
/*
*===========================================================================
* Ipsntp_packet
*===========================================================================
*/
typedef struct Ipsntp_packet_struct
{
Ip_u8 LVM;
Ip_u8 stratum;
Ip_u8 poll;
Ip_u8 precision;
Ip_u32 rootDelay;
Ip_u32 rootDispersion;
Ip_u32 refId;
/* Timestamps. */
Ipsntp_ntp_time refTimestamp;
Ipsntp_ntp_time orgTimestamp;
Ipsntp_ntp_time rcvTimestamp;
Ipsntp_ntp_time traTimestamp; /* only used for client. */
/* Optional Authenticator unsupported. 'U32 auth[3],' */
}
Ipsntp_packet;
/* SNTP packet data retrived from SNTP protocol message */
typedef struct Ipsntp_packet_data_struct
{
Ip_u8 leapVerMode;
Ip_u8 stratum;
Ip_u8 poll;
Ip_u8 precision;
} Ipsntp_packet_data;
/*
*===========================================================================
* Ipsntp_data
*===========================================================================
*/
typedef struct Ipsntp_data_struct
{
Ipsntp_packet packet; /* SNTP packet. */
Ipsntp_packet *pkt; /* Alignment trick. */
Ipsntp_rtc_time inputTime; /* Time when packet arrived. */
Ipsntp_ntp_time outputTime; /* Time when packet departed */
Ip_u16 port_n;
Ip_u16 pad;
Ip_bool is_server; /* IP_TRUE if server mode */
#ifdef IPCOM_USE_INET
struct Ip_in_addr cliservip[IPSNTP_NUMSERV]; /* (S)NTP server IP. */
struct Ip_in_addr climultiip; /* Multicast client group */
char climultiifname[IP_IFNAMSIZ]; /* Multicast client interface */
#endif
#ifdef IPCOM_USE_INET6
struct Ip_in6_addr cliservip6[IPSNTP_NUMSERV]; /* (S)NTP server IPv6. */
Ip_u32 cliservip6_ifindex[IPSNTP_NUMSERV]; /* Interface index */
struct Ip_in6_addr climulti6ip; /* Multicast client IPv6 group */
char climulti6ifname[IP_IFNAMSIZ]; /* Multicast client IPv6 interface */
#endif
Ip_u32 clicurip; /* Index ^ of current server used. */
Ip_s32 clitimeout; /* Client timeout (interval). */
Ip_bool syncronized;
Ipcom_tmo adj_tmo;
Ip_bool first; /* Remember boot. */
Ip_s32 climaxretrans; /* Maximum number of cliretrans. */
Ip_s32 cliretrans; /* Current client retrans. */
Ip_s32 clisecretrans; /* Seconds between each retrans. */
#ifdef IPCOM_USE_INET
struct Ip_in_addr servbrdip; /* Server broadcast IP */
Ip_u32 servbrdip_ifindex; /* Interface index */
#endif
#ifdef IPCOM_USE_INET6
struct Ip_in6_addr servbrdip6; /* Server broadcast IPv6 */
Ip_u32 servbrdip6_ifindex; /* Interface index */
#endif
Ip_s32 servtimeout; /* Broadcast/multicast server. */
Ip_s32 stratum; /* Stratum. */
Ip_s32 precision; /* Precision. */
Ip_s32 mcast_ttl; /* TTL. */
Ip_u32 ref_id; /* Reference source */
Ipsntp_rtc_time ref_time; /* Reference time */
int (*servtime)(Ip_u32 *, Ip_u32 *);
Ipcom_tmo tmo;
Ip_time_t tick;
#ifdef IPCOM_USE_INET
Ip_fd fd;
#endif
#ifdef IPCOM_USE_INET6
Ip_fd fd6;
#endif
void *msg_control; /* ancillary data */
Ip_socklen_t msg_controllen; /* ancillary data buffer len */
Ip_u32 leap_second; /* Leap second in NTP time. */
int leap_second_flag; /* Indicator a positive or negative leap second. */
}
Ipsntp_data;
/*
****************************************************************************
* 6 FUNCTIONS
****************************************************************************
*/
IP_GLOBAL int ipsntp_cmd_sntp(int argc, char **argv);
#ifdef IP_PORT_VXWORKS
IP_GLOBAL int ipsntp_clock_gettime(Ipsntp_rtc_time *clock_time);
IP_GLOBAL int ipsntp_clock_settime(Ipsntp_rtc_time *clock_time);
#endif
#ifdef __cplusplus
}
#endif
#endif
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/
@@ -0,0 +1,155 @@
/*
******************************************************************************
* SOURCE FILE
*
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: ipsntp_time.c,v $ $Revision: 1.1.14.1 $
* $Source: /home/interpeak/CVSRoot/ipsntp/src/ipsntp_time.c,v $
* $Author: ulf $
* $State: Exp $ $Locker: $
*
* INTERPEAK_COPYRIGHT_STRING
* Design and implementation by Markus Carlstedt <markus@interpeak.se>
******************************************************************************
*/
/*
****************************************************************************
* 1 DESCRIPTION
****************************************************************************
* SNTP time function.
*/
/*
****************************************************************************
* 2 CONFIGURATION
****************************************************************************
*/
/*
****************************************************************************
* 3 INCLUDE FILES
****************************************************************************
*/
#include <ipcom_type.h>
#include <ipcom_err.h>
#include "ipsntp_h.h"
#ifdef IP_PORT_VXWORKS
#include <vxWorks.h>
#include <time.h>
/*
****************************************************************************
* 4 DEFINES
****************************************************************************
*/
/*
****************************************************************************
* 5 TYPES
****************************************************************************
*/
/*
****************************************************************************
* 6 EXTERN PROTOTYPES
****************************************************************************
*/
/*
****************************************************************************
* 7 LOCAL PROTOTYPES
****************************************************************************
*/
/*
****************************************************************************
* 8 DATA
****************************************************************************
*/
/*
****************************************************************************
* 9 LOCAL FUNCTIONS
****************************************************************************
*/
/*
****************************************************************************
* 10 GLOBAL FUNCTIONS
****************************************************************************
*/
/*
*===========================================================================
* ipcom_clock_gettime
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL int
ipsntp_clock_gettime(Ipsntp_rtc_time *clock_time)
{
struct timespec t_spec;
if(clock_time == IP_NULL)
return IPCOM_ERR_FAILED;
if(clock_gettime(CLOCK_REALTIME, &t_spec) == 0)
{
clock_time->seconds = (Ip_time_t)t_spec.tv_sec;
clock_time->nanos = (Ip_u32)t_spec.tv_nsec;
}
else
{
clock_time->seconds = 0;
clock_time->nanos = 0;
return IPCOM_ERR_FAILED;
}
return IPCOM_SUCCESS;
}
/*
*===========================================================================
* ipcom_clock_settime
*===========================================================================
* Description:
* Parameters:
* Returns:
*
*/
IP_GLOBAL int
ipsntp_clock_settime(Ipsntp_rtc_time *clock_time)
{
struct timespec t_spec;
if(clock_time == IP_NULL)
return IPCOM_ERR_FAILED;
t_spec.tv_sec = (time_t)clock_time->seconds;
t_spec.tv_nsec = (long)clock_time->nanos;
if (clock_settime(CLOCK_REALTIME, &t_spec) == OK)
return IPCOM_SUCCESS;
else
return IPCOM_ERR_FAILED;
}
/*
****************************************************************************
* 11 PUBLIC FUNCTIONS
****************************************************************************
*/
#endif
/*
****************************************************************************
* END OF FILE
****************************************************************************
*/