| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- /**
- * @file DataProto.h
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN 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.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * @section DESCRIPTION
- *
- * Mudule for frame sending used in the VPN client program.
- */
- #ifndef BADVPN_CLIENT_DATAPROTO_H
- #define BADVPN_CLIENT_DATAPROTO_H
- #include <stdint.h>
- #include <misc/debugcounter.h>
- #include <misc/debug.h>
- #include <base/DebugObject.h>
- #include <system/BReactor.h>
- #include <flow/PacketPassFairQueue.h>
- #include <flow/PacketPassNotifier.h>
- #include <flow/PacketRecvBlocker.h>
- #include <flow/SinglePacketBuffer.h>
- #include <flow/PacketPassConnector.h>
- #include <flow/PacketRouter.h>
- #include <flowextra/PacketPassInactivityMonitor.h>
- #include <client/DataProtoKeepaliveSource.h>
- typedef void (*DataProtoSink_handler) (void *user, int up);
- typedef void (*DataProtoSource_handler) (void *user, const uint8_t *frame, int frame_len);
- typedef void (*DataProtoFlow_handler_inactivity) (void *user);
- struct DataProtoFlow_buffer;
- /**
- * Frame destination.
- * Represents a peer as a destination for sending frames to.
- */
- typedef struct {
- BReactor *reactor;
- int frame_mtu;
- PacketPassFairQueue queue;
- PacketPassInactivityMonitor monitor;
- PacketPassNotifier notifier;
- DataProtoKeepaliveSource ka_source;
- PacketRecvBlocker ka_blocker;
- SinglePacketBuffer ka_buffer;
- PacketPassFairQueueFlow ka_qflow;
- BTimer receive_timer;
- int up;
- int up_report;
- DataProtoSink_handler handler;
- void *user;
- BPending up_job;
- struct DataProtoFlow_buffer *detaching_buffer;
- DebugObject d_obj;
- DebugCounter d_ctr;
- } DataProtoSink;
- /**
- * Receives frames from a {@link PacketRecvInterface} input and
- * allows the user to route them to buffers in {@link DataProtoFlow} objects.
- */
- typedef struct {
- DataProtoSource_handler handler;
- void *user;
- BReactor *reactor;
- int frame_mtu;
- PacketRouter router;
- uint8_t *current_buf;
- int current_recv_len;
- DebugObject d_obj;
- DebugCounter d_ctr;
- } DataProtoSource;
- /**
- * Contains a buffer for frames from a specific peer to a specific peer.
- * Receives frames from a {@link DataProtoSource} as routed by the user.
- * Can be attached to a {@link DataProtoSink} to send out frames.
- */
- typedef struct {
- DataProtoSource *source;
- peerid_t source_id;
- peerid_t dest_id;
- DataProtoSink *dp_desired;
- struct DataProtoFlow_buffer *b;
- DebugObject d_obj;
- } DataProtoFlow;
- struct DataProtoFlow_buffer {
- DataProtoFlow *flow;
- int inactivity_time;
- RouteBuffer rbuf;
- PacketPassInactivityMonitor monitor;
- PacketPassConnector connector;
- DataProtoSink *dp;
- PacketPassFairQueueFlow dp_qflow;
- };
- /**
- * Initializes the object.
- *
- * @param o the object
- * @param reactor reactor we live in
- * @param output output interface. Must support cancel functionality. Its MTU must be
- * >=DATAPROTO_MAX_OVERHEAD.
- * @param keepalive_time keepalive time
- * @param tolerance_time after how long of not having received anything from the peer
- * to consider the link down
- * @param handler up state handler
- * @param user value to pass to handler
- * @return 1 on success, 0 on failure
- */
- int DataProtoSink_Init (DataProtoSink *o, BReactor *reactor, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoSink_handler handler, void *user) WARN_UNUSED;
- /**
- * Frees the object.
- * There must be no local sources attached.
- *
- * @param o the object
- */
- void DataProtoSink_Free (DataProtoSink *o);
- /**
- * Notifies the object that a packet was received from the peer.
- * Must not be in freeing state.
- *
- * @param o the object
- * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
- * Must be 0 or 1.
- */
- void DataProtoSink_Received (DataProtoSink *o, int peer_receiving);
- /**
- * Initiazes the object.
- *
- * @param o the object
- * @param input frame input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
- * @param handler handler called when a frame arrives to allow the user to route it to
- * appropriate {@link DataProtoFlow} objects.
- * @param user value passed to handler
- * @param reactor reactor we live in
- * @return 1 on success, 0 on failure
- */
- int DataProtoSource_Init (DataProtoSource *o, PacketRecvInterface *input, DataProtoSource_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
- /**
- * Frees the object.
- * There must be no {@link DataProtoFlow} objects using this source.
- *
- * @param o the object
- */
- void DataProtoSource_Free (DataProtoSource *o);
- /**
- * Initializes the object.
- * The object is initialized in not attached state.
- *
- * @param o the object
- * @param source source to receive frames from
- * @param source_id source peer ID to encode in the headers (i.e. our ID)
- * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
- * object belongs to)
- * @param num_packets number of packets the buffer should hold. Must be >0.
- * @param inactivity_time milliseconds of output inactivity after which to call the
- * inactivity handler; <0 to disable. Note that the object is considered
- * active as long as its buffer is non-empty, even if is not attached to
- * a {@link DataProtoSink}.
- * @param handler_inactivity inactivity handler, if inactivity_time >=0
- * @param user value to pass to handler
- * @return 1 on success, 0 on failure
- */
- int DataProtoFlow_Init (
- DataProtoFlow *o, DataProtoSource *source, peerid_t source_id, peerid_t dest_id, int num_packets,
- int inactivity_time, DataProtoFlow_handler_inactivity handler_inactivity, void *user
- ) WARN_UNUSED;
- /**
- * Frees the object.
- * The object must be in not attached state.
- *
- * @param o the object
- */
- void DataProtoFlow_Free (DataProtoFlow *o);
- /**
- * Routes a frame from the flow's source to this flow.
- * Must be called from within the job context of the {@link DataProtoSource_handler} handler.
- * Must not be called after this has been called with more=0 for the current frame.
- *
- * @param o the object
- * @param more whether the current frame may have to be routed to more
- * objects. If 0, must not be called again until the handler is
- * called for the next frame. Must be 0 or 1.
- */
- void DataProtoFlow_Route (DataProtoFlow *o, int more);
- /**
- * Attaches the object to a destination.
- * The object must be in not attached state.
- *
- * @param o the object
- * @param dp destination to attach to. This object's frame_mtu must be <=
- * (output MTU of dp) - DATAPROTO_MAX_OVERHEAD.
- */
- void DataProtoFlow_Attach (DataProtoFlow *o, DataProtoSink *dp);
- /**
- * Detaches the object from a destination.
- * The object must be in attached state.
- *
- * @param o the object
- */
- void DataProtoFlow_Detach (DataProtoFlow *o);
- #endif
|