%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Rychla reference k sitovym socketum
%
%  Martin Bruchanov -- bruxy at regnet dot cz
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\pdfoutput=1
\documentclass[a4paper, 10pt, pointednumbers]{scrartcl}
\usepackage{czech}
\usepackage{a4wide}
\usepackage{graphics}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{multicol}

% zm�na okraj� str�nky, kter� jsou standardn� 1 palec = 2.54 cm
\evensidemargin 0mm
\oddsidemargin 0mm
\headsep 0mm
\hoffset -18.4mm
\voffset -17.4mm % vrsek
\topmargin 0in 
\headheight 0cm
\textwidth 197mm
\textheight 28.7cm
\footskip 0mm

% Zmena radkovani
\renewcommand{\baselinestretch}{1.0}

% Zmena vzdalenosti
%\newcommand{\itemizespace}{-0.5em}
\newcommand{\itemizespace}{0em}


\pagestyle{empty}

\begin{document} 
\section*{S��ov� sockety}
\addtocounter{section}{1}

\begin{multicols}{2}

\begin{description}

\item[TCP] -- p�ed samotnou komunikac� se nav�e spojen�, V�echna odeslan� data
se potvrzuj� a na konec je nutn� spojen� ukon�it (uzav��t). TCP paket obsahuje
svou hlavi�ku a samotn� data, kter� p�en��. TCP paket bude vlo�en do IP
datagramu (jako data IP datagramu) a odesl�n.

\item[UDP] -- jedn� se  tzv. nespojovanou slu�bu. To znamen�, �e nedoch�z� k
nav�z�n� spojen�. Prost� ode�leme data na stanovenou IP adresu a dan� UDP port
a nev�me, zda data dorazila a zda se nepo�kodila nebo nedorazila v jin�m
spojen�.

\end{description}


\subsection{Funkce pro pr�ci se sockety}

%\begin{verbatim}
%int descriptor, domain, type, protocol;
%decriptor = socket(domain, type, protocol);
%\end{verbatim}

\begin{itemize}\addtolength\itemsep{\itemizespace}
\item Definice form�t� adres (\verb|<sys/socket.h>|):
\begin{itemize}\addtolength\itemsep{\itemizespace}
\item \verb|AF_UNIX| -- intern� protokol UNIXu, 
\item \verb|AF_INET| -- Internet. adresy,
\item \verb|AF_ISO| -- protokoly ISO.
\end{itemize}

\item Typ socketu:
\begin{itemize}\addtolength\itemsep{\itemizespace}
\item \verb|SOCK_STREAM| -- (TCP) sekven�n�, spolehliv�, dvoustrann� proudov�
komunikace, mechanizmus p�enosu \emph{out-of-band}, 
\item \verb|SOCK_DGRAM| -- (UDP) podpora datagram� (nespojovan�, nespolehliv� zpr�vy
mal� d�lky).
\end{itemize}

\item Specifikace jm�na socketu:

\begin{verbatim}
struct sockaddr {
    /* Rodina adres AF_xxx */
    unsigned short sa_family;  
    /* a� 14bytov� p��m� adresa */
    char sa_data[14]; };
\end{verbatim}

\item Volba adresy a portu:
\begin{verbatim}
struct sockaddr_in {
	/* Rodina adres AF_xxx */
    short int          sin_family;  
    /* ��slo portu */ 
     unsigned short int sin_port;    
    /* Internetov� adresa */
    struct in_addr     sin_addr;   
    /* nepou�ito */
    unsigned char      sin_zero[8]; };
\end{verbatim}

\item Nastaven� adresy:

\begin{verbatim}
struct in_addr {
/* 32 bitov� adresa */
    unsigned long s_addr; };
\end{verbatim}

\item Pro p�evod adresy z textov�ho tvaru (192.168.0.1) do bin�rn�ho tvaru:

\begin{verbatim}
int inet_aton (const char *NAME, 
               struct in_addr *ADDR)

struct in_addr {
   unsigned long int s_addr;
};
\end{verbatim}

\item p�evod po�ad� bytu mezi s��� (big endian, MSB prvn�) a hostem,
nap�. na x86 je LSB byte prvn� v po�ad� (little endian).

\begin{itemize}\addtolength\itemsep{\itemizespace}
\item Host $\rightarrow$ Network:\\
 \verb|uint32_t htonl(uint32_t hostlong)|, \\
 \verb|uint16_t htons(uint16_t hostshort)|.
\item Network $\rightarrow$ Host:\\
 \verb|uint32_t ntohl((uint32_t netlong)|, \\
 \verb|uint16_t ntohs((uint16_t hostshort)|.
\end{itemize}

\item \textbf{socket()} -- tvo�� socket dan�ho typu a vr�t�
file-descriptor, protokol 0x0, domain \verb|AF_xxx|

\begin{verbatim}
int socket(int domain, int type, int protocol);
\end{verbatim}

\item \textbf{connect()} -- slou�� k navazov�n� spojen�

\begin{verbatim}
int connect(int sockfd, struct sockaddr
             *serv_addr, int addrlen);
\end{verbatim}

\item \textbf{bind()} --  sv�e socket se jm�nem, v kombinaci s
\texttt{listen()} ur�� ��sla portu, kde server poslouch�

\begin{verbatim}
int bind(int sockfd, struct sockaddr 
         *my_addr, int addrlen);
\end{verbatim}

\item \textbf{listen()} --  zavolat po \texttt{bind()}, slou�� ke
konfiguraci pro p��jem dat., \texttt{backlog} ud�v� max. po�et nepokryt�ch
spojen� ve front�.

\begin{verbatim}
int listen(int sockfd, int backlog);
\end{verbatim}

\item \textbf{accept()} -- �ek� na p��choz� spojen�, vr�t� parametry
prvn�ho spojen� ve front� a nov� socket, kter� slou�� ke komunikaci s
druhou stranou. P�vodn� socket \texttt{sockfd} se tak m��e op�t pou��t
pro �ek�n� na dal�� spojen�.

\begin{verbatim}
int accept(int sockfd, void *addr, int *addrlen);
\end{verbatim}

\item \textbf{close(), shutdown()} --  zav�r� socket.

\item {\large\bfseries  Pos�l�n� zpr�v do socketu}

\begin{itemize}\addtolength\itemsep{\itemizespace}
\item \textbf{send()} -- pouze jeli socket spojen�, neobsahuje indikaci spr�vn�ho doru�en�,
s flagem 0 je ekv. \texttt{write()}.
\begin{verbatim}
int send(int s, const void *msg, 
         int len, unsigned int flags);
\end{verbatim}

\item \textbf{sendto()} -- pouze pro \verb|SOCK_STREAM| 
\begin{verbatim}
int  sendto(int  s, const void *msg, int len, 
            unsigned int flags, 
            const struct sockaddr *to, 
			int tolen);
\end{verbatim}

Definice struktury \texttt{msghdr}:

\begin{verbatim}
struct msghdr {
 /* optional address */
    void * msg_name;
/* size of address */
    socklen_t  msg_namelen;  
/* scatter/gather array */
    struct iovec * msg_iov;  
/* # elements in msg_iov */
    size_t msg_iovlen;     
/* ancillary data, see below */
    void * msg_control;     
/* ancillary data buffer len */
    socklen_t  msg_controllen; 
/* flags on received message */
    int  msg_flags;  
};
\end{verbatim}

\item \textbf{sendmsg()} --

\begin{verbatim}
int sendmsg(int s, const struct msghdr *msg,  
            unsigned int flags);
\end{verbatim}

\item \textbf{write()} -- syst�mov� funkce \verb|<unistd.h>|

\begin{verbatim}
ssize_t write(int fd, const void *buf,
               size_t count);
\end{verbatim}

\item Hodnoty parametry \texttt{flags}:
	\begin{itemize}\addtolength\itemsep{\itemizespace}
		\item \verb|MSG_OOB| -- zpracuj out-of-band data
		\item \verb|MSG_DONTROUTE| -- obejdi routing, pou�ij p��m� rozhran�
	\end{itemize}
\end{itemize}

\item {\large\bfseries P��jem zpr�vy ze socketu}
	\begin{itemize}\addtolength\itemsep{\itemizespace}
		\item \textbf{recv()} -- �ten� dat bez odstran�n� z fronty
\begin{verbatim}
ssize_t recv(int s, void *buf, 
             size_t len, int flags);
\end{verbatim}
		\item \textbf{recvfrom()} --
\begin{verbatim}
ssize_t  recvfrom(int  s,  void *buf, 
                  size_t len, int flags, struct
                  sockaddr *from,  
                  socklen_t *fromlen);
\end{verbatim}
		\item \textbf{recvmsg()} --
\begin{verbatim}
ssize_t recvmsg(int s, struct msghdr *msg, 
                int flags);
\end{verbatim}
		
		\item \textbf{read()} -- syst�mov� funkce \verb|<unistd.h>|,
		�te data bez odstran�n� z fronty

\begin{verbatim}
ssize_t read(int fd, void *buf, size_t count);
\end{verbatim}		 
	\end{itemize}

\item Chybov� n�vraty

\begin{itemize}\addtolength\itemsep{\itemizespace}
	\item EBADF -- �patn� descriptor,
	\item ECONNREFUSED -- odm�tnut� spojen�,
	\item ENOTCONN -- nep�ipojen� socket,
	\item ENOTSOCK -- argument nen� socket,
	\item EAGAIN -- �as vypr�el p�ed doru�en�m,
	\item EINTR -- p��jem p�eru�en,
	\item EFAULT -- RX ukazatel mimo adresov� prostor,
	\item EINVAL -- vlo�en �patn� argument,
	\item ENOMEM -- nelze alokovat pam� pro \texttt{recvmsg}.
	
\end{itemize}

\end{itemize}

\subsection{P�eklad dom�nov�ch jmen}

\begin{itemize}\addtolength\itemsep{\itemizespace}
\item Definice funkc� \verb|<netdb.h>|

\item Struktura \texttt{hostent} pro char. vzd�len�ho
po��ta�e

\begin{verbatim}
struct hostent {
/* �et�zec s n�zvem po��ta�e */
   char   *h_name;      
/* Ukazatel na pole aliasu, ukon�en� NULL */ 
   char  **h_aliases;   
/* Typ adresy, nap�. AF_INET */
      int     h_addrtype;   
/* D�lka adresy v bytech */
      int     h_length;     
/* Ukazatel na pole ukazatel� ukon�en� NULL,
   ukazatel obsahuje adresu pole typu char
   d�lky h_length, obsahuj�c� adresu v s��ov�m
   po�ad� */
     char  **h_addr_list  
};
\end{verbatim}

\item \textbf{gethostbyname()} -- vrat� informace o zadan�m
dom�nov�m jm�n�.

\begin{verbatim}
struct hostent *gethostbyname(const char *name);
\end{verbatim}

\item \textbf{gethostbyaddr()} -- vr�ti info o zazan� adrese

\begin{verbatim}
struct hostent *gethostbyaddr(const char *addr, 
                              int len, int type);
\end{verbatim}

%\item \textbf{sethostent()} -- 
%\begin{verbatim}
%void sethostent(int stayopen);
%\end{verbatim}

%\item \textbf{endhostent()} -- 

%\begin{verbatim}
%void endhostent(void);
%\end{verbatim}

%\item \textbf{herror()} -- 
%\begin{verbatim}
%void herror(const char *s);
%\end{verbatim}

\item Struktura \texttt{protent}

\begin{verbatim}
struct protoent {
    char *p_name; /* official protocol name */
    char **p_aliases; /* alias list */
    int p_proto; /* protocol number */
};
\end{verbatim}

\item \textbf{getprotobyname(), getprotobynumber()} -- vr�t� \texttt{protent} pro
dan� ��dek z \texttt{/etc/protocols} pro dan� protokol nebo jeho ��slo.

\begin{verbatim}
struct protoent *getprotobyname(const char *name);
struct protoent *getprotobynumber(int proto);
\end{verbatim}

%struct protoent *getprotoent(void);
%struct servent *getservent(void);

\item Struktura \texttt{servent}

\begin{verbatim}
struct servent {
  char *s_name;     /* official service name */
  char **s_aliases; /* alias list */
  int  s_port;      /* port number */
  char *s_proto;    /* protocol to use */ };
\end{verbatim}


\item \textbf{getservbyname(), getservbyport()} -- vrac� strukturu
\texttt{servent} pro konkr�tn� slu�bu nebo ��slo z \texttt{/etc/services}

\begin{verbatim}
struct servent *getservbyname(const char *name, 
                              const char *proto);
struct servent *getservbyport(int port, 
                              const char *proto);
\end{verbatim}
\end{itemize}

\subsection{Kompilace}

\begin{itemize}\addtolength\itemsep{\itemizespace}

\item Doporu�en� hlavi�kov� soubory: \texttt{<stdio.h> <stdlib.h>, <unistd.h>,
<netinet/in.h>, <netdb.h>,\\ <sys/socket.h>, <string.h>}

\item Kompilace na GNU/Linux:

\begin{verbatim}
gcc -Wall -o program zdrojak.c
\end{verbatim}

\item Kompilace na Solaris vy�aduje dopln�n� 
knihoven:
\begin{verbatim}
gcc -Wall -lnsl -lsocket -o program zdrojak.c
\end{verbatim}

\item P��klad \texttt{Makefile}
\begin{verbatim}
all: program

program.o: zdrojak.c
   gcc -c -o program.o zdrojak.c

program: program.o
   gcc -Wall -o program program.o
\end{verbatim}
\end{itemize}

\end{multicols}
\end{document}

