\hypertarget{ntputil_8c}{}\doxysection{mqtt/ntputil.c File Reference}
\label{ntputil_8c}\index{mqtt/ntputil.c@{mqtt/ntputil.c}}


N\+TP utility functions.  


{\ttfamily \#include $<$errno.\+h$>$}\newline
{\ttfamily \#include $<$netdb.\+h$>$}\newline
{\ttfamily \#include $<$stdint.\+h$>$}\newline
{\ttfamily \#include $<$stdio.\+h$>$}\newline
{\ttfamily \#include $<$string.\+h$>$}\newline
{\ttfamily \#include $<$time.\+h$>$}\newline
{\ttfamily \#include $<$unistd.\+h$>$}\newline
{\ttfamily \#include \char`\"{}ntputil.\+h\char`\"{}}\newline
Include dependency graph for ntputil.\+c\+:
\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=350pt]{ntputil_8c__incl}
\end{center}
\end{figure}
\doxysubsection*{Classes}
\begin{DoxyCompactItemize}
\item 
struct \mbox{\hyperlink{struct__ntp__timestamp__t}{\+\_\+ntp\+\_\+timestamp\+\_\+t}}
\begin{DoxyCompactList}\small\item\em A custom data type to represent N\+TP timestamp format. \end{DoxyCompactList}\item 
struct \mbox{\hyperlink{struct__ntp__packet__t}{\+\_\+ntp\+\_\+packet\+\_\+t}}
\begin{DoxyCompactList}\small\item\em A custom data type to represent N\+TP packet header format. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Typedefs}
\begin{DoxyCompactItemize}
\item 
typedef struct \mbox{\hyperlink{struct__ntp__timestamp__t}{\+\_\+ntp\+\_\+timestamp\+\_\+t}} \mbox{\hyperlink{ntputil_8c_a2c0a80096a0661d352a513a192ab45db}{ntp\+\_\+timestamp\+\_\+t}}
\begin{DoxyCompactList}\small\item\em A custom data type to represent N\+TP timestamp format. \end{DoxyCompactList}\item 
typedef struct \mbox{\hyperlink{struct__ntp__packet__t}{\+\_\+ntp\+\_\+packet\+\_\+t}} \mbox{\hyperlink{ntputil_8c_ae4cb2d0676900dc2d9544df30b86ab8d}{ntp\+\_\+packet\+\_\+t}}
\begin{DoxyCompactList}\small\item\em A custom data type to represent N\+TP packet header format. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
uint32\+\_\+t \mbox{\hyperlink{ntputil_8c_a22348663e96af2a1bfb051c63ddc6a87}{\+\_\+convert\+\_\+to\+\_\+host\+\_\+byte\+\_\+order}} (uint32\+\_\+t in)
\begin{DoxyCompactList}\small\item\em Wrapper function of ntohl. \end{DoxyCompactList}\item 
int64\+\_\+t \mbox{\hyperlink{ntputil_8c_a841bdcb296a30cbb219bf12e819c0423}{ntputil\+\_\+get\+\_\+epoch}} (uint32\+\_\+t hnums, char $\ast$$\ast$hnames, uint16\+\_\+t $\ast$ports)
\begin{DoxyCompactList}\small\item\em Get N\+TP timestamps from the given or public N\+TP servers. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Variables}
\begin{DoxyCompactItemize}
\item 
const uint64\+\_\+t \mbox{\hyperlink{ntputil_8c_a6a9700006650fbbd5cc8e1558ad618df}{N\+T\+P\+U\+T\+I\+L\+\_\+\+T\+I\+M\+E\+S\+T\+A\+M\+P\+\_\+\+D\+E\+L\+TA}} = 2208988800\+U\+LL
\item 
const double \mbox{\hyperlink{ntputil_8c_a1f1c673f79c02e99a61b8b71c92a4071}{N\+T\+P\+U\+T\+I\+L\+\_\+\+M\+A\+X\+\_\+\+F\+R\+A\+C\+\_\+\+D\+O\+U\+B\+LE}} = 4294967295.\+0L
\item 
const int64\+\_\+t \mbox{\hyperlink{ntputil_8c_a33d7c9d8dc4da262031b1ab1d3957e30}{N\+T\+P\+U\+T\+I\+L\+\_\+\+S\+E\+C\+\_\+\+T\+O\+\_\+\+U\+S\+E\+C\+\_\+\+M\+U\+L\+T\+I\+P\+L\+I\+ER}} = 1000000
\item 
const char \mbox{\hyperlink{ntputil_8c_a92cf955d422eb9bdb2ca8bc7328328b9}{N\+T\+P\+U\+T\+I\+L\+\_\+\+D\+E\+F\+A\+U\+L\+T\+\_\+\+H\+N\+A\+ME}} \mbox{[}$\,$\mbox{]} = \char`\"{}pool.\+ntp.\+org\char`\"{}
\item 
const uint16\+\_\+t \mbox{\hyperlink{ntputil_8c_a06e704e3bdd29774e4e3beb6f9a7863a}{N\+T\+P\+U\+T\+I\+L\+\_\+\+D\+E\+F\+A\+U\+L\+T\+\_\+\+P\+O\+RT}} = 123
\end{DoxyCompactItemize}


\doxysubsection{Detailed Description}
N\+TP utility functions. 

Copyright (C) 2021 Wook Song \href{mailto:wook16.song@samsung.com}{\texttt{ wook16.\+song@samsung.\+com}}

\begin{DoxyDate}{Date}
16 Jul 2021 
\end{DoxyDate}
\begin{DoxySeeAlso}{See also}
\href{https://github.com/nnstreamer/nnstreamer}{\texttt{ https\+://github.\+com/nnstreamer/nnstreamer}} 
\end{DoxySeeAlso}
\begin{DoxyAuthor}{Author}
Wook Song \href{mailto:wook16.song@samsung.com}{\texttt{ wook16.\+song@samsung.\+com}} 
\end{DoxyAuthor}
\begin{DoxyRefDesc}{Bug}
\item[\mbox{\hyperlink{bug__bug000022}{Bug}}]No known bugs except for N\+YI items \end{DoxyRefDesc}
\begin{DoxyRefDesc}{Todo}
\item[\mbox{\hyperlink{todo__todo000008}{Todo}}]Need to support caching and polling timer mechanism \end{DoxyRefDesc}


\doxysubsection{Typedef Documentation}
\mbox{\Hypertarget{ntputil_8c_ae4cb2d0676900dc2d9544df30b86ab8d}\label{ntputil_8c_ae4cb2d0676900dc2d9544df30b86ab8d}} 
\index{ntputil.c@{ntputil.c}!ntp\_packet\_t@{ntp\_packet\_t}}
\index{ntp\_packet\_t@{ntp\_packet\_t}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{ntp\_packet\_t}{ntp\_packet\_t}}
{\footnotesize\ttfamily typedef struct \mbox{\hyperlink{struct__ntp__packet__t}{\+\_\+ntp\+\_\+packet\+\_\+t}} \mbox{\hyperlink{ntputil_8c_ae4cb2d0676900dc2d9544df30b86ab8d}{ntp\+\_\+packet\+\_\+t}}}



A custom data type to represent N\+TP packet header format. 

N\+TP Packet Header Format (\href{https://www.ietf.org/rfc/rfc5905.txt}{\texttt{ https\+://www.\+ietf.\+org/rfc/rfc5905.\+txt}} p.\+18) 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$\+LI $\vert$ VN $\vert$\+Mode $\vert$ Stratum $\vert$ Poll $\vert$ Precision $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ Root Delay $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ Root Dispersion $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ Reference ID $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ $\vert$
\begin{DoxyItemize}
\item Reference Timestamp (64) + $\vert$ $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ $\vert$
\item Origin Timestamp (64) + $\vert$ $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ $\vert$
\item Receive Timestamp (64) + $\vert$ $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ $\vert$
\item Transmit Timestamp (64) + $\vert$ $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ $\vert$ . . . Extension Field 1 (variable) . . . $\vert$ $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ $\vert$ . . . Extension Field 2 (variable) . . . $\vert$ $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ Key Identifier $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ $\vert$ $\vert$ dgst (128) $\vert$ $\vert$ $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ 
\end{DoxyItemize}\mbox{\Hypertarget{ntputil_8c_a2c0a80096a0661d352a513a192ab45db}\label{ntputil_8c_a2c0a80096a0661d352a513a192ab45db}} 
\index{ntputil.c@{ntputil.c}!ntp\_timestamp\_t@{ntp\_timestamp\_t}}
\index{ntp\_timestamp\_t@{ntp\_timestamp\_t}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{ntp\_timestamp\_t}{ntp\_timestamp\_t}}
{\footnotesize\ttfamily typedef struct \mbox{\hyperlink{struct__ntp__timestamp__t}{\+\_\+ntp\+\_\+timestamp\+\_\+t}} \mbox{\hyperlink{ntputil_8c_a2c0a80096a0661d352a513a192ab45db}{ntp\+\_\+timestamp\+\_\+t}}}



A custom data type to represent N\+TP timestamp format. 

N\+TP Timestamp Format (\href{https://www.ietf.org/rfc/rfc5905.txt}{\texttt{ https\+://www.\+ietf.\+org/rfc/rfc5905.\+txt}} p.\+12) 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ Seconds $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ $\vert$ Fraction $\vert$ +-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+-\/+ 

\doxysubsection{Function Documentation}
\mbox{\Hypertarget{ntputil_8c_a22348663e96af2a1bfb051c63ddc6a87}\label{ntputil_8c_a22348663e96af2a1bfb051c63ddc6a87}} 
\index{ntputil.c@{ntputil.c}!\_convert\_to\_host\_byte\_order@{\_convert\_to\_host\_byte\_order}}
\index{\_convert\_to\_host\_byte\_order@{\_convert\_to\_host\_byte\_order}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{\_convert\_to\_host\_byte\_order()}{\_convert\_to\_host\_byte\_order()}}
{\footnotesize\ttfamily uint32\+\_\+t \+\_\+convert\+\_\+to\+\_\+host\+\_\+byte\+\_\+order (\begin{DoxyParamCaption}\item[{uint32\+\_\+t}]{in }\end{DoxyParamCaption})}



Wrapper function of ntohl. 

Converting network byte order to host byte order. 

Definition at line 125 of file ntputil.\+c.

Here is the caller graph for this function\+:
\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=350pt]{ntputil_8c_a22348663e96af2a1bfb051c63ddc6a87_icgraph}
\end{center}
\end{figure}
\mbox{\Hypertarget{ntputil_8c_a841bdcb296a30cbb219bf12e819c0423}\label{ntputil_8c_a841bdcb296a30cbb219bf12e819c0423}} 
\index{ntputil.c@{ntputil.c}!ntputil\_get\_epoch@{ntputil\_get\_epoch}}
\index{ntputil\_get\_epoch@{ntputil\_get\_epoch}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{ntputil\_get\_epoch()}{ntputil\_get\_epoch()}}
{\footnotesize\ttfamily int64\+\_\+t ntputil\+\_\+get\+\_\+epoch (\begin{DoxyParamCaption}\item[{uint32\+\_\+t}]{hnums,  }\item[{char $\ast$$\ast$}]{hnames,  }\item[{uint16\+\_\+t $\ast$}]{ports }\end{DoxyParamCaption})}



Get N\+TP timestamps from the given or public N\+TP servers. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em hnums} & A number of hostname and port pairs. If 0 is given, the N\+TP server pool will be used. \\
\hline
\mbox{\texttt{ in}}  & {\em hnames} & A list of hostname \\
\hline
\mbox{\texttt{ in}}  & {\em ports} & A list of port \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
an Unix epoch time as microseconds on success, negative values on error 
\end{DoxyReturn}
\begin{DoxyNote}{Note}
ntp\+\_\+timestamp\+\_\+t recv\+\_\+ts in ntp\+\_\+packet\+\_\+t means the timestamp as the packet left the N\+TP server. \textquotesingle{}sec\textquotesingle{} corresponds to the seconds passed since 1900 and \textquotesingle{}frac\textquotesingle{} is needed to convert seconds to smaller units of a second such as microsceonds. Note that the bit/byte order of those data should be converted to the host\textquotesingle{}s endianness.

N\+TP uses an epoch of January 1, 1900 while the Unix epoch is the number of seconds that have elapsed since January 1, 1970. For this reason, we subtract 70 years worth of seconds from the seconds since 1900
\end{DoxyNote}


Definition at line 140 of file ntputil.\+c.

Here is the call graph for this function\+:
\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=347pt]{ntputil_8c_a841bdcb296a30cbb219bf12e819c0423_cgraph}
\end{center}
\end{figure}
Here is the caller graph for this function\+:
\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=350pt]{ntputil_8c_a841bdcb296a30cbb219bf12e819c0423_icgraph}
\end{center}
\end{figure}


\doxysubsection{Variable Documentation}
\mbox{\Hypertarget{ntputil_8c_a92cf955d422eb9bdb2ca8bc7328328b9}\label{ntputil_8c_a92cf955d422eb9bdb2ca8bc7328328b9}} 
\index{ntputil.c@{ntputil.c}!NTPUTIL\_DEFAULT\_HNAME@{NTPUTIL\_DEFAULT\_HNAME}}
\index{NTPUTIL\_DEFAULT\_HNAME@{NTPUTIL\_DEFAULT\_HNAME}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{NTPUTIL\_DEFAULT\_HNAME}{NTPUTIL\_DEFAULT\_HNAME}}
{\footnotesize\ttfamily const char N\+T\+P\+U\+T\+I\+L\+\_\+\+D\+E\+F\+A\+U\+L\+T\+\_\+\+H\+N\+A\+ME\mbox{[}$\,$\mbox{]} = \char`\"{}pool.\+ntp.\+org\char`\"{}}



Definition at line 118 of file ntputil.\+c.

\mbox{\Hypertarget{ntputil_8c_a06e704e3bdd29774e4e3beb6f9a7863a}\label{ntputil_8c_a06e704e3bdd29774e4e3beb6f9a7863a}} 
\index{ntputil.c@{ntputil.c}!NTPUTIL\_DEFAULT\_PORT@{NTPUTIL\_DEFAULT\_PORT}}
\index{NTPUTIL\_DEFAULT\_PORT@{NTPUTIL\_DEFAULT\_PORT}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{NTPUTIL\_DEFAULT\_PORT}{NTPUTIL\_DEFAULT\_PORT}}
{\footnotesize\ttfamily const uint16\+\_\+t N\+T\+P\+U\+T\+I\+L\+\_\+\+D\+E\+F\+A\+U\+L\+T\+\_\+\+P\+O\+RT = 123}



Definition at line 119 of file ntputil.\+c.

\mbox{\Hypertarget{ntputil_8c_a1f1c673f79c02e99a61b8b71c92a4071}\label{ntputil_8c_a1f1c673f79c02e99a61b8b71c92a4071}} 
\index{ntputil.c@{ntputil.c}!NTPUTIL\_MAX\_FRAC\_DOUBLE@{NTPUTIL\_MAX\_FRAC\_DOUBLE}}
\index{NTPUTIL\_MAX\_FRAC\_DOUBLE@{NTPUTIL\_MAX\_FRAC\_DOUBLE}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{NTPUTIL\_MAX\_FRAC\_DOUBLE}{NTPUTIL\_MAX\_FRAC\_DOUBLE}}
{\footnotesize\ttfamily const double N\+T\+P\+U\+T\+I\+L\+\_\+\+M\+A\+X\+\_\+\+F\+R\+A\+C\+\_\+\+D\+O\+U\+B\+LE = 4294967295.\+0L}



Definition at line 116 of file ntputil.\+c.

\mbox{\Hypertarget{ntputil_8c_a33d7c9d8dc4da262031b1ab1d3957e30}\label{ntputil_8c_a33d7c9d8dc4da262031b1ab1d3957e30}} 
\index{ntputil.c@{ntputil.c}!NTPUTIL\_SEC\_TO\_USEC\_MULTIPLIER@{NTPUTIL\_SEC\_TO\_USEC\_MULTIPLIER}}
\index{NTPUTIL\_SEC\_TO\_USEC\_MULTIPLIER@{NTPUTIL\_SEC\_TO\_USEC\_MULTIPLIER}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{NTPUTIL\_SEC\_TO\_USEC\_MULTIPLIER}{NTPUTIL\_SEC\_TO\_USEC\_MULTIPLIER}}
{\footnotesize\ttfamily const int64\+\_\+t N\+T\+P\+U\+T\+I\+L\+\_\+\+S\+E\+C\+\_\+\+T\+O\+\_\+\+U\+S\+E\+C\+\_\+\+M\+U\+L\+T\+I\+P\+L\+I\+ER = 1000000}



Definition at line 117 of file ntputil.\+c.

\mbox{\Hypertarget{ntputil_8c_a6a9700006650fbbd5cc8e1558ad618df}\label{ntputil_8c_a6a9700006650fbbd5cc8e1558ad618df}} 
\index{ntputil.c@{ntputil.c}!NTPUTIL\_TIMESTAMP\_DELTA@{NTPUTIL\_TIMESTAMP\_DELTA}}
\index{NTPUTIL\_TIMESTAMP\_DELTA@{NTPUTIL\_TIMESTAMP\_DELTA}!ntputil.c@{ntputil.c}}
\doxysubsubsection{\texorpdfstring{NTPUTIL\_TIMESTAMP\_DELTA}{NTPUTIL\_TIMESTAMP\_DELTA}}
{\footnotesize\ttfamily const uint64\+\_\+t N\+T\+P\+U\+T\+I\+L\+\_\+\+T\+I\+M\+E\+S\+T\+A\+M\+P\+\_\+\+D\+E\+L\+TA = 2208988800\+U\+LL}



Definition at line 115 of file ntputil.\+c.

