\doxysection{include/osmocom/core/socket.h File Reference}
\label{socket_8h}\index{include/osmocom/core/socket.h@{include/osmocom/core/socket.h}}


Osmocom socket convenience functions.  


{\ttfamily \#include $<$stdint.\+h$>$}\newline
{\ttfamily \#include $<$stdbool.\+h$>$}\newline
{\ttfamily \#include $<$stddef.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/defs.\+h$>$}\newline
{\ttfamily \#include $<$osmocom/core/socket\+\_\+compat.\+h$>$}\newline
\doxysubsection*{Data Structures}
\begin{DoxyCompactItemize}
\item 
struct \textbf{ osmo\+\_\+sockaddr}
\item 
struct \textbf{ osmo\+\_\+sock\+\_\+init2\+\_\+multiaddr\+\_\+pars}
\end{DoxyCompactItemize}
\doxysubsection*{Macros}
\begin{DoxyCompactItemize}
\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+MAX\+\_\+\+ADDRS}~32
\begin{DoxyCompactList}\small\item\em maximum number of local or remote addresses supported by an osmo\+\_\+sock instance \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+NAME\+\_\+\+MAXLEN}~(2 + INET6\+\_\+\+ADDRSTRLEN + 1 + 5 + 3 + 2 + INET6\+\_\+\+ADDRSTRLEN + 1 + 5 + 1)
\begin{DoxyCompactList}\small\item\em maximum length of a socket name (\char`\"{}r=1.\+2.\+3.\+4\+:123$<$-\/$>$l=5.\+6.\+7.\+8\+:987\char`\"{}) \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+MULTIADDR\+\_\+\+PEER\+\_\+\+STR\+\_\+\+MAXLEN}~(INET6\+\_\+\+ADDRSTRLEN $\ast$ \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+MAX\+\_\+\+ADDRS} + INET6\+\_\+\+ADDRSTRLEN + 2 + 6 + 1)
\begin{DoxyCompactList}\small\item\em maximum length of a multi-\/address socket peer (endpoint) name\+: (5.\+6.\+7.\+8$\vert$\+::9)\+:987 char \textquotesingle{}(\textquotesingle{} + OSMO\+\_\+\+STREAM\+\_\+\+MAX\+\_\+\+ADDRS -\/ 1 addr separators + chars \char`\"{})\+:\char`\"{} + port buffer + char \textquotesingle{}\textbackslash{}0\textquotesingle{} \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+MULTIADDR\+\_\+\+NAME\+\_\+\+MAXLEN}~(\textbf{ OSMO\+\_\+\+SOCK\+\_\+\+MULTIADDR\+\_\+\+PEER\+\_\+\+STR\+\_\+\+MAXLEN} + 7)
\begin{DoxyCompactList}\small\item\em maximum length of a multia-\/address socket name (\char`\"{}r=(\+::2$\vert$1.\+2.\+3.\+4)\+:123$<$-\/$>$l=(5.\+6.\+7.\+8$\vert$\+::9)\+:987\char`\"{}) \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+CONNECT}~(1 $<$$<$ 0)
\begin{DoxyCompactList}\small\item\em connect the socket to a remote peer \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+BIND}~(1 $<$$<$ 1)
\begin{DoxyCompactList}\small\item\em bind the socket to a local address/port \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+NONBLOCK}~(1 $<$$<$ 2)
\begin{DoxyCompactList}\small\item\em switch socket to non-\/blocking mode \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+NO\+\_\+\+MCAST\+\_\+\+LOOP}~(1 $<$$<$ 3)
\begin{DoxyCompactList}\small\item\em disable multiast loop (IP\+\_\+\+MULTICAST\+\_\+\+LOOP) \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+NO\+\_\+\+MCAST\+\_\+\+ALL}~(1 $<$$<$ 4)
\begin{DoxyCompactList}\small\item\em disable receiving all multiast even for non-\/subscribed groups \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+UDP\+\_\+\+REUSEADDR}~(1 $<$$<$ 5)
\begin{DoxyCompactList}\small\item\em use SO\+\_\+\+REUSEADDR on UDP ports (required for multicast) \end{DoxyCompactList}\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+DSCP}(\textbf{ x})~(((\textbf{ x})\&0x3f) $<$$<$ 24)
\begin{DoxyCompactList}\small\item\em use \doxyref{OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+DSCP(x)}{p.}{group__socket_ga6f3cbc5dea1ebecd6a4312ea65666627} to set IP DSCP \textquotesingle{}x\textquotesingle{} for packets transmitted on the socket \end{DoxyCompactList}\item 
\#define \textbf{ GET\+\_\+\+OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+DSCP}(f)~(((f) $>$$>$ 24) \& 0x3f)
\item 
\#define \textbf{ OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+PRIO}(\textbf{ x})~(((\textbf{ x})\&0xff) $<$$<$ 16)
\begin{DoxyCompactList}\small\item\em use \doxyref{OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+PRIO(x)}{p.}{group__socket_ga3d96505ab56ed6051eca99ab76f16ca3} to set priority \textquotesingle{}x\textquotesingle{} for packets transmitted on the socket \end{DoxyCompactList}\item 
\#define \textbf{ GET\+\_\+\+OSMO\+\_\+\+SOCK\+\_\+\+F\+\_\+\+PRIO}(f)~(((f) $>$$>$ 16) \& 0xff)
\end{DoxyCompactItemize}
\doxysubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+is\+\_\+local} (struct sockaddr $\ast$addr, unsigned int addrlen)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+is\+\_\+any} (const struct \textbf{ osmo\+\_\+sockaddr} $\ast$addr)
\item 
static socklen\+\_\+t \textbf{ osmo\+\_\+sockaddr\+\_\+size} (const struct \textbf{ osmo\+\_\+sockaddr} $\ast$addr)
\begin{DoxyCompactList}\small\item\em Return the size of the variant used in the address NOTE\+: This does not return the size of the in\{,6\}\+\_\+addr, but rather the size of the surrounding sockaddr\+\_\+in\{,6\}. \end{DoxyCompactList}\item 
unsigned int \textbf{ osmo\+\_\+sockaddr\+\_\+to\+\_\+str\+\_\+and\+\_\+uint} (char $\ast$addr, unsigned int addr\+\_\+len, uint16\+\_\+t $\ast$port, const struct sockaddr $\ast$sa)
\item 
size\+\_\+t \textbf{ osmo\+\_\+sockaddr\+\_\+in\+\_\+to\+\_\+str\+\_\+and\+\_\+uint} (char $\ast$addr, unsigned int addr\+\_\+len, uint16\+\_\+t $\ast$port, const struct sockaddr\+\_\+in $\ast$sin)
\item 
const char $\ast$ \textbf{ osmo\+\_\+sockaddr\+\_\+ntop} (const struct sockaddr $\ast$sa, char $\ast$dst)
\item 
uint16\+\_\+t \textbf{ osmo\+\_\+sockaddr\+\_\+port} (const struct sockaddr $\ast$sa)
\item 
void \textbf{ osmo\+\_\+sockaddr\+\_\+set\+\_\+port} (struct sockaddr $\ast$sa, uint16\+\_\+t port)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+local\+\_\+ip} (struct \textbf{ osmo\+\_\+sockaddr} $\ast$local\+\_\+ip, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$remote\+\_\+ip)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+cmp} (const struct \textbf{ osmo\+\_\+sockaddr} $\ast$a, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$b)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+to\+\_\+octets} (uint8\+\_\+t $\ast$dst, size\+\_\+t dst\+\_\+maxlen, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$os)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+from\+\_\+octets} (struct \textbf{ osmo\+\_\+sockaddr} $\ast$os, const void $\ast$src, size\+\_\+t src\+\_\+len)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+from\+\_\+str\+\_\+and\+\_\+uint} (struct \textbf{ osmo\+\_\+sockaddr} $\ast$osa\+\_\+out, const char $\ast$ipstr, uint16\+\_\+t port)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+netmask\+\_\+to\+\_\+prefixlen} (const struct \textbf{ osmo\+\_\+sockaddr} $\ast$addr)
\item 
const char $\ast$ \textbf{ osmo\+\_\+sockaddr\+\_\+to\+\_\+str} (const struct \textbf{ osmo\+\_\+sockaddr} $\ast$sockaddr)
\item 
char $\ast$ \textbf{ osmo\+\_\+sockaddr\+\_\+to\+\_\+str\+\_\+buf} (char $\ast$buf, size\+\_\+t buf\+\_\+len, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$sockaddr)
\item 
int \textbf{ osmo\+\_\+sockaddr\+\_\+to\+\_\+str\+\_\+buf2} (char $\ast$buf, size\+\_\+t buf\+\_\+len, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$sockaddr)
\item 
char $\ast$ \textbf{ osmo\+\_\+sockaddr\+\_\+to\+\_\+str\+\_\+c} (void $\ast$ctx, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$sockaddr)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init} (uint16\+\_\+t family, uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, const char $\ast$\textbf{ host}, uint16\+\_\+t port, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init2} (uint16\+\_\+t family, uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, const char $\ast$local\+\_\+host, uint16\+\_\+t local\+\_\+port, const char $\ast$remote\+\_\+host, uint16\+\_\+t remote\+\_\+port, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init2\+\_\+multiaddr} (uint16\+\_\+t family, uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, const char $\ast$$\ast$local\+\_\+hosts, size\+\_\+t local\+\_\+hosts\+\_\+cnt, uint16\+\_\+t local\+\_\+port, const char $\ast$$\ast$remote\+\_\+hosts, size\+\_\+t remote\+\_\+hosts\+\_\+cnt, uint16\+\_\+t remote\+\_\+port, unsigned int \textbf{ flags}) \textbf{ OSMO\+\_\+\+DEPRECATED\+\_\+\+OUTSIDE}(\char`\"{}Use \textbf{ osmo\+\_\+sock\+\_\+init2\+\_\+multiaddr2}() \textbf{ instead}\char`\"{})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init2\+\_\+multiaddr2} (uint16\+\_\+t family, uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, const char $\ast$$\ast$local\+\_\+hosts, size\+\_\+t local\+\_\+hosts\+\_\+cnt, uint16\+\_\+t local\+\_\+port, const char $\ast$$\ast$remote\+\_\+hosts, size\+\_\+t remote\+\_\+hosts\+\_\+cnt, uint16\+\_\+t remote\+\_\+port, unsigned int \textbf{ flags}, struct \textbf{ osmo\+\_\+sock\+\_\+init2\+\_\+multiaddr\+\_\+pars} $\ast$pars)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init\+\_\+osa} (uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$local, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$remote, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init\+\_\+ofd} (struct \textbf{ osmo\+\_\+fd} $\ast$ofd, int family, int \textbf{ type}, int \textbf{ proto}, const char $\ast$\textbf{ host}, uint16\+\_\+t port, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init2\+\_\+ofd} (struct \textbf{ osmo\+\_\+fd} $\ast$ofd, int family, int \textbf{ type}, int \textbf{ proto}, const char $\ast$local\+\_\+host, uint16\+\_\+t local\+\_\+port, const char $\ast$remote\+\_\+host, uint16\+\_\+t remote\+\_\+port, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init\+\_\+osa\+\_\+ofd} (struct \textbf{ osmo\+\_\+fd} $\ast$ofd, int \textbf{ type}, int \textbf{ proto}, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$local, const struct \textbf{ osmo\+\_\+sockaddr} $\ast$remote, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+init\+\_\+sa} (struct sockaddr $\ast$ss, uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+unix\+\_\+init} (uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, const char $\ast$socket\+\_\+path, unsigned int \textbf{ flags})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+unix\+\_\+init\+\_\+ofd} (struct \textbf{ osmo\+\_\+fd} $\ast$ofd, uint16\+\_\+t \textbf{ type}, uint8\+\_\+t \textbf{ proto}, const char $\ast$socket\+\_\+path, unsigned int \textbf{ flags})
\item 
char $\ast$ \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+name} (const void $\ast$ctx, int fd)
\item 
const char $\ast$ \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+name2} (int fd)
\item 
char $\ast$ \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+name2\+\_\+c} (const void $\ast$ctx, int fd)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+name\+\_\+buf} (char $\ast$str, size\+\_\+t str\+\_\+len, int fd)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+ip\+\_\+and\+\_\+port} (int fd, char $\ast$ip, size\+\_\+t ip\+\_\+len, char $\ast$port, size\+\_\+t port\+\_\+len, bool local)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+local\+\_\+ip} (int fd, char $\ast$\textbf{ host}, size\+\_\+t \textbf{ len})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+local\+\_\+ip\+\_\+port} (int fd, char $\ast$port, size\+\_\+t \textbf{ len})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+remote\+\_\+ip} (int fd, char $\ast$\textbf{ host}, size\+\_\+t \textbf{ len})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+remote\+\_\+ip\+\_\+port} (int fd, char $\ast$port, size\+\_\+t \textbf{ len})
\item 
int \textbf{ osmo\+\_\+sock\+\_\+multiaddr\+\_\+get\+\_\+ip\+\_\+and\+\_\+port} (int fd, int ip\+\_\+proto, char $\ast$ip, size\+\_\+t $\ast$ip\+\_\+cnt, size\+\_\+t ip\+\_\+len, char $\ast$port, size\+\_\+t port\+\_\+len, bool local)
\item 
int \textbf{ osmo\+\_\+multiaddr\+\_\+ip\+\_\+and\+\_\+port\+\_\+snprintf} (char $\ast$str, size\+\_\+t str\+\_\+len, const char $\ast$ip, size\+\_\+t ip\+\_\+cnt, size\+\_\+t ip\+\_\+len, const char $\ast$portbuf)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+multiaddr\+\_\+get\+\_\+name\+\_\+buf} (char $\ast$str, size\+\_\+t str\+\_\+len, int fd, int sk\+\_\+proto)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+multiaddr\+\_\+add\+\_\+local\+\_\+addr} (int sfd, const char $\ast$$\ast$addrs, size\+\_\+t addrs\+\_\+cnt)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+multiaddr\+\_\+del\+\_\+local\+\_\+addr} (int sfd, const char $\ast$$\ast$addrs, size\+\_\+t addrs\+\_\+cnt)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+mcast\+\_\+loop\+\_\+set} (int fd, bool enable)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+mcast\+\_\+ttl\+\_\+set} (int fd, uint8\+\_\+t ttl)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+mcast\+\_\+all\+\_\+set} (int fd, bool enable)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+mcast\+\_\+iface\+\_\+set} (int fd, const char $\ast$ifname)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+mcast\+\_\+subscribe} (int fd, const char $\ast$grp\+\_\+addr)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+local\+\_\+ip} (char $\ast$local\+\_\+ip, const char $\ast$remote\+\_\+ip)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+set\+\_\+dscp} (int fd, uint8\+\_\+t dscp)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+set\+\_\+priority} (int fd, int prio)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+get\+\_\+nonblock} (int fd)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+set\+\_\+nonblock} (int fd, int on)
\item 
int \textbf{ osmo\+\_\+sock\+\_\+sctp\+\_\+get\+\_\+peer\+\_\+addr\+\_\+info} (int fd, struct sctp\+\_\+paddrinfo $\ast$pinfo, size\+\_\+t $\ast$pinfo\+\_\+cnt)
\end{DoxyCompactItemize}


\doxysubsection{Detailed Description}
Osmocom socket convenience functions. 

