2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_stream.c
23 * @brief non-anonymous file-transfer
24 * @author Christian Grothoff
27 * - limit # concurrent clients, timeout for read
30 #include "gnunet_constants.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_stream_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_applications.h"
35 #include "gnunet-service-fs.h"
36 #include "gnunet-service-fs_stream.h"
39 * Information we keep around for each active streaming client.
46 struct StreamClient *next;
51 struct StreamClient *prev;
54 * Socket for communication.
56 struct GNUNET_STREAM_Socket *socket;
59 * Handle for active read operation, or NULL.
61 struct GNUNET_STREAM_IOReadHandle *rh;
64 * Handle for active write operation, or NULL.
66 struct GNUNET_STREAM_IOWriteHandle *wh;
69 * Size of the last write that was initiated.
77 * Listen socket for incoming requests.
79 static struct GNUNET_STREAM_ListenSocket *listen_socket;
82 * Head of DLL of stream clients.
84 static struct StreamClient *sc_head;
87 * Tail of DLL of stream clients.
89 static struct StreamClient *sc_tail;
93 * We're done with a particular client, clean up.
95 * @param sc client to clean up
98 terminate_stream (struct StreamClient *sc)
101 GNUNET_STREAM_io_read_cancel (sc->rh);
103 GNUNET_STREAM_io_write_cancel (sc->wh);
104 GNUNET_STREAM_close (sc->socket);
105 GNUNET_CONTAINER_DLL_remove (sc_head,
113 * Functions of this signature are called whenever data is available from the
116 * @param cls the closure from GNUNET_STREAM_read
117 * @param status the status of the stream at the time this function is called
118 * @param data traffic from the other side
119 * @param size the number of bytes available in data read; will be 0 on timeout
120 * @return number of bytes of processed from 'data' (any data remaining should be
121 * given to the next time the read processor is called).
124 process_request (void *cls,
125 enum GNUNET_STREAM_Status status,
129 struct StreamClient *sc = cls;
134 case GNUNET_STREAM_OK:
135 // fixme: handle request...
137 case GNUNET_STREAM_TIMEOUT:
138 case GNUNET_STREAM_SHUTDOWN:
139 case GNUNET_STREAM_SYSERR:
140 case GNUNET_STREAM_BROKEN:
141 terminate_stream (sc);
147 sc->rh = GNUNET_STREAM_read (sc->socket,
148 GNUNET_TIME_UNIT_FOREVER_REL,
156 * Functions of this type are called upon new stream connection from other peers
157 * or upon binding error which happen when the app_port given in
158 * GNUNET_STREAM_listen() is already taken.
160 * @param cls the closure from GNUNET_STREAM_listen
161 * @param socket the socket representing the stream; NULL on binding error
162 * @param initiator the identity of the peer who wants to establish a stream
163 * with us; NULL on binding error
164 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
165 * stream (the socket will be invalid after the call)
168 accept_cb (void *cls,
169 struct GNUNET_STREAM_Socket *socket,
170 const struct GNUNET_PeerIdentity *initiator)
172 struct StreamClient *sc;
175 return GNUNET_SYSERR;
176 sc = GNUNET_malloc (sizeof (struct StreamClient));
178 sc->rh = GNUNET_STREAM_read (sc->socket,
179 GNUNET_TIME_UNIT_FOREVER_REL,
182 GNUNET_CONTAINER_DLL_insert (sc_head,
190 * Initialize subsystem for non-anonymous file-sharing.
195 listen_socket = GNUNET_STREAM_listen (GSF_cfg,
196 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
198 GNUNET_STREAM_OPTION_END);
203 * Shutdown subsystem for non-anonymous file-sharing.
208 struct StreamClient *sc;
210 while (NULL != (sc = sc_head))
211 terminate_stream (sc);
212 if (NULL != listen_socket)
214 GNUNET_STREAM_listen_close (listen_socket);
215 listen_socket = NULL;
219 /* end of gnunet-service-fs_stream.c */