From: Simone Piccardi Date: Sat, 9 Sep 2006 14:13:09 +0000 (+0000) Subject: Aggiunto esempio di uso di {{{TCP_INFO}}} X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=commitdiff_plain;h=655820f73a10748f15e3b578df98e41abdae3515 Aggiunto esempio di uso di {{{TCP_INFO}}} --- diff --git a/listati/is_closing.c b/listati/is_closing.c new file mode 100644 index 0000000..d0eafbd --- /dev/null +++ b/listati/is_closing.c @@ -0,0 +1,17 @@ +int is_closing(int sock) +{ + int err; + struct tcp_info info; + socklen_t len = sizeof(info); + if (getsockopt(sock, SOL_TCP, TCP_INFO, &info, &len) != -1) { + if (info.tcpi_state == TCP_CLOSE || + info.tcpi_state == TCP_CLOSE_WAIT || + info.tcpi_state == TCP_CLOSING) { + return 1; + } else { + return 0; + } + } else { + return errno; + } +} diff --git a/sockctrl.tex b/sockctrl.tex index ea07241..538ef96 100644 --- a/sockctrl.tex +++ b/sockctrl.tex @@ -3184,10 +3184,11 @@ seguente elenco: una dimensione minima pari a \texttt{SOCK\_MIN\_RCVBUF/2}. Questa opzione non deve essere utilizzata in codice che vuole essere portabile. -\item[\const{TCP\_INFO}] permette di ricevere una serie di informazioni - relative al socket che il kernel restituisce in una speciale struttura +\item[\const{TCP\_INFO}] opzione, specifica di Linux, ma introdotta anche in + altri kernel (ad esempio FreeBSD) di controllare lo stato di un socket TCP + in user space. L'opzione restituisce in una speciale struttura \struct{tcp\_info}, la cui definizione è riportata in - fig.~\ref{fig:tcp_info_struct}. + fig.~\ref{fig:tcp_info_struct}, tutta una serie di dati relativi al socket. \begin{figure}[!htb] \footnotesize \centering @@ -3199,6 +3200,22 @@ seguente elenco: \label{fig:tcp_info_struct} \end{figure} +Con questa opzione diventa possibile ricevere una serie di informazioni +relative al socket così da poter effettuare dei controlli, senza passare +attraverso delle operazioni di lettura. Ad esempio si può verificare se un +socket è stato chiuso con un codice come quello illustrato in +fig.~\ref{fig:is_closing}. + +\begin{figure}[!htb] + \footnotesize \centering + \begin{minipage}[c]{15cm} + \includestruct{listati/tcp_info.h} + \end{minipage} + \caption{Codice della funzione \texttt{is_closing.c}, che controlla lo stato + di un socket TCP per verificare se si sta chiudendo..} + \label{fig:is_closing} +\end{figure} + \item[\const{TCP\_QUICKACK}] abilita la modalità speciale \textit{quickack}. \item[\const{TCP\_CONGESTION}] Introdotta con il kernel 2.6.13. diff --git a/sources/full_fread.c b/sources/full_fread.c index 0567994..329d7e4 100644 --- a/sources/full_fread.c +++ b/sources/full_fread.c @@ -1,6 +1,6 @@ /* full_fread.c * - * Copyright (C) 2005 Simone Piccardi + * Copyright (C) 2005-2006 Simone Piccardi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ * Routine to read an exact number of bytes from a file * * Author: Simone Piccardi - * Mat. 2005 + * Mar. 2005 * * $Id$ * diff --git a/sources/is_closing.c b/sources/is_closing.c new file mode 100644 index 0000000..32e66aa --- /dev/null +++ b/sources/is_closing.c @@ -0,0 +1,54 @@ +/* is_closing.c + * + * Copyright (C) 2006 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Routine is_closing + * Routine to check if a TCP socket is closing + * + * Return 1 if the socket is closing, 0 if not and a negative value in + * case of errors + * + * Author: Simone Piccardi + * Sep. 2006 + * + * $Id$ + * + ****************************************************************/ +#include +#include +#include +#include /* error code */ +# +int is_closing(int sock) +{ + int err; + struct tcp_info info; + socklen_t len = sizeof(info); + if (getsockopt(sock, SOL_TCP, TCP_INFO, &info, &len) != -1) { + if (info.tcpi_state == TCP_CLOSE || + info.tcpi_state == TCP_CLOSE_WAIT || + info.tcpi_state == TCP_CLOSING) { + return 1; + } else { + return 0; + } + } else { + return errno; + } +}