Explorar o código

PacketProtoDecoder: report errors without FlowError.
FlowError: remove

ambrop7 %!s(int64=14) %!d(string=hai) anos
pai
achega
7c9776158b

+ 5 - 4
client/StreamPeerIO.c

@@ -48,7 +48,7 @@
 #define LISTEN_STATE_GOTCLIENT 1
 #define LISTEN_STATE_FINISHED 2
 
-static void decoder_handler (StreamPeerIO *pio, int component, int code);
+static void decoder_handler_error (StreamPeerIO *pio);
 static void connector_handler (StreamPeerIO *pio, int is_error);
 static void connection_handler (StreamPeerIO *pio, int event);
 static void connect_sslcon_handler (StreamPeerIO *pio, int event);
@@ -63,7 +63,7 @@ static int compare_certificate (StreamPeerIO *pio, CERTCertificate *cert);
 static void reset_state (StreamPeerIO *pio);
 static void reset_and_report_error (StreamPeerIO *pio);
 
-void decoder_handler (StreamPeerIO *pio, int component, int code)
+void decoder_handler_error (StreamPeerIO *pio)
 {
     DebugObject_Access(&pio->d_obj);
     
@@ -563,8 +563,9 @@ int StreamPeerIO_Init (
     
     // init receiveing objects
     StreamRecvConnector_Init(&pio->input_connector, BReactor_PendingGroup(pio->reactor));
-    FlowErrorDomain_Init(&pio->input_decoder_domain, (FlowErrorDomain_handler)decoder_handler, pio);
-    if (!PacketProtoDecoder_Init(&pio->input_decoder, FlowErrorReporter_Create(&pio->input_decoder_domain, 0), StreamRecvConnector_GetOutput(&pio->input_connector), user_recv_if, BReactor_PendingGroup(pio->reactor))) {
+    if (!PacketProtoDecoder_Init(&pio->input_decoder, StreamRecvConnector_GetOutput(&pio->input_connector), user_recv_if, BReactor_PendingGroup(pio->reactor), pio,
+        (PacketProtoDecoder_handler_error)decoder_handler_error
+    )) {
         BLog(BLOG_ERROR, "FlowErrorDomain_Init failed");
         goto fail1;
     }

+ 0 - 1
client/StreamPeerIO.h

@@ -85,7 +85,6 @@ typedef struct {
     
     // receiving objects
     StreamRecvConnector input_connector;
-    FlowErrorDomain input_decoder_domain;
     PacketProtoDecoder input_decoder;
     
     // connection side

+ 0 - 1
flow/CMakeLists.txt

@@ -19,7 +19,6 @@ add_library(flow
     PacketRecvInterface.c
     StreamPassInterface.c
     StreamRecvInterface.c
-    FlowError.c
     RouteBuffer.c
     PacketRouter.c
     LineBuffer.c

+ 0 - 42
flow/FlowError.c

@@ -1,42 +0,0 @@
-/**
- * @file FlowError.c
- * @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.
- */
-
-#include <flow/FlowError.h>
-
-void FlowErrorDomain_Init (FlowErrorDomain *d, FlowErrorDomain_handler handler, void *user)
-{
-    d->handler = handler;
-    d->user = user;
-}
-
-FlowErrorReporter FlowErrorReporter_Create (FlowErrorDomain *domain, int component)
-{
-    FlowErrorReporter r;
-    r.domain = domain;
-    r.component = component;
-    return r;
-}
-
-void FlowErrorReporter_ReportError (FlowErrorReporter *reporter, int code)
-{
-    reporter->domain->handler(reporter->domain->user, reporter->component, code);
-}

+ 0 - 85
flow/FlowError.h

@@ -1,85 +0,0 @@
-/**
- * @file FlowError.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
- * 
- * Flow error handling.
- */
-
-#ifndef BADVPN_FLOW_FLOWERROR_H
-#define BADVPN_FLOW_FLOWERROR_H
-
-#include <stdint.h>
-
-/**
- * Callback function invoked when {@link FlowErrorReporter_ReportError} is called.
- *
- * @param user value specified to {@link FlowErrorDomain_Init}
- * @param component identifier of the component reporting the error, as in
- *                  {@link FlowErrorReporter_Create}
- * @param code component-specific error data, as in {@link FlowErrorReporter_ReportError}
- */ 
-typedef void (*FlowErrorDomain_handler) (void *user, int component, int code);
-
-/**
- * Object used to report errors from multiple sources to the same error handler.
- */
-typedef struct {
-    FlowErrorDomain_handler handler;
-    void *user;
-} FlowErrorDomain;
-
-/**
- * Initializes the error domain.
- *
- * @param d the object
- * @param handler callback function invoked when {@link FlowErrorReporter_ReportError} is called
- * @param user value passed to callback functions
- */
-void FlowErrorDomain_Init (FlowErrorDomain *d, FlowErrorDomain_handler handler, void *user);
-
-/**
- * Structure that can be passed to flow components to ease error reporting.
- */
-typedef struct {
-    FlowErrorDomain *domain;
-    int component;
-} FlowErrorReporter;
-
-/**
- * Creates a {@link FlowErrorReporter} structure.
- *
- * @param domain error domain
- * @param component component identifier
- * @return a {@link FlowErrorReporter} structure with the specifed error domain and component.
- */
-FlowErrorReporter FlowErrorReporter_Create (FlowErrorDomain *domain, int component);
-
-/**
- * Reports an error.
- *
- * @param reporter a {@link FlowErrorReporter} structure containing the error domain and
- *                 component identifier user to report the error
- * @param code component-specific error data
- */
-void FlowErrorReporter_ReportError (FlowErrorReporter *reporter, int code);
-
-#endif

+ 4 - 10
flow/PacketProtoDecoder.c

@@ -32,17 +32,10 @@
 
 #include <generated/blog_channel_PacketProtoDecoder.h>
 
-static void report_error (PacketProtoDecoder *enc, int error);
 static void process_data (PacketProtoDecoder *enc);
 static void input_handler_done (PacketProtoDecoder *enc, int data_len);
 static void output_handler_done (PacketProtoDecoder *enc);
 
-void report_error (PacketProtoDecoder *enc, int error)
-{
-    FlowErrorReporter_ReportError(&enc->rep, error);
-    return;
-}
-
 void process_data (PacketProtoDecoder *enc)
 {
     int was_error = 0;
@@ -98,7 +91,7 @@ void process_data (PacketProtoDecoder *enc)
     
     // if we had error, report it
     if (was_error) {
-        report_error(enc, PACKETPROTODECODER_ERROR_TOOLONG);
+        enc->handler_error(enc->user);
         return;
     }
 }
@@ -126,12 +119,13 @@ void output_handler_done (PacketProtoDecoder *enc)
     return;
 }
 
-int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg) 
+int PacketProtoDecoder_Init (PacketProtoDecoder *enc, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg, void *user, PacketProtoDecoder_handler_error handler_error)
 {
     // init arguments
-    enc->rep = rep;
     enc->input = input;
     enc->output = output;
+    enc->user = user;
+    enc->handler_error = handler_error;
     
     // init input
     StreamRecvInterface_Receiver_Init(enc->input, (StreamRecvInterface_handler_done)input_handler_done, enc);

+ 11 - 16
flow/PacketProtoDecoder.h

@@ -35,26 +35,20 @@
 #include <base/DebugObject.h>
 #include <flow/StreamRecvInterface.h>
 #include <flow/PacketPassInterface.h>
-#include <flow/FlowError.h>
-
-#define PACKETPROTODECODER_ERROR_TOOLONG 1
 
 /**
- * Object which decodes a stream according to PacketProto.
- *
- * Input is with {@link StreamRecvInterface}.
- * Output is with {@link PacketPassInterface}.
- *
- * Errors are reported through {@link FlowErrorDomain}.
- * On error, the decoder is reset to the initial state.
- * Error code is an int which is one of the following:
- *     - PACKETPROTODECODER_ERROR_TOOLONG: the packet header contains
- *       a packet length value which is too big.
+ * Handler called when a protocol error occurs.
+ * When an error occurs, the decoder is reset to the initial state.
+ * 
+ * @param user as in {@link PacketProtoDecoder_Init}
  */
+typedef void (*PacketProtoDecoder_handler_error) (void *user);
+
 typedef struct {
-    FlowErrorReporter rep;
     StreamRecvInterface *input;
     PacketPassInterface *output;
+    void *user;
+    PacketProtoDecoder_handler_error handler_error;
     int output_mtu;
     int buf_size;
     int buf_start;
@@ -67,14 +61,15 @@ typedef struct {
  * Initializes the object.
  *
  * @param enc the object
- * @param rep error reporting data
  * @param input input interface. The decoder will accept packets with payload size up to its MTU
  *              (but the payload can never be more than PACKETPROTO_MAXPAYLOAD).
  * @param output output interface
  * @param pg pending group
+ * @param user argument to handlers
+ * @param handler_error error handler
  * @return 1 on success, 0 on failure
  */
-int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg) WARN_UNUSED;
+int PacketProtoDecoder_Init (PacketProtoDecoder *enc, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg, void *user, PacketProtoDecoder_handler_error handler_error) WARN_UNUSED;
 
 /**
  * Frees the object.

+ 4 - 5
server/server.c

@@ -194,7 +194,7 @@ static void client_connection_handler (struct client_data *client, int event);
 static void client_sslcon_handler (struct client_data *client, int event);
 
 // decoder handler
-static void client_decoder_handler (struct client_data *client, int component, int code);
+static void client_decoder_handler_error (struct client_data *client);
 
 // provides a buffer for sending a control packet to the client
 static int client_start_control_packet (struct client_data *client, void **data, int len);
@@ -944,9 +944,8 @@ int client_init_io (struct client_data *client)
     PacketPassInterface_Init(&client->input_interface, SC_MAX_ENC, (PacketPassInterface_handler_send)client_input_handler_send, client, BReactor_PendingGroup(&ss));
     
     // init decoder
-    FlowErrorDomain_Init(&client->input_decoder_domain, (FlowErrorDomain_handler)client_decoder_handler, client);
-    if (!PacketProtoDecoder_Init(&client->input_decoder, FlowErrorReporter_Create(&client->input_decoder_domain, 0),
-        recv_if, &client->input_interface, BReactor_PendingGroup(&ss)
+    if (!PacketProtoDecoder_Init(&client->input_decoder,recv_if, &client->input_interface, BReactor_PendingGroup(&ss), client,
+        (PacketProtoDecoder_handler_error)client_decoder_handler_error
     )) {
         client_log(client, BLOG_ERROR, "PacketProtoDecoder_Init failed");
         goto fail1;
@@ -1215,7 +1214,7 @@ fail0:
     client_remove(client);
 }
 
-void client_decoder_handler (struct client_data *client, int component, int code)
+void client_decoder_handler_error (struct client_data *client)
 {
     ASSERT(INITSTATUS_HASLINK(client->initstatus))
     ASSERT(!client->dying)

+ 0 - 1
server/server.h

@@ -142,7 +142,6 @@ struct client_data {
     BPending dying_job;
     
     // input
-    FlowErrorDomain input_decoder_domain;
     PacketProtoDecoder input_decoder;
     PacketPassInterface input_interface;
     

+ 3 - 4
server_connection/ServerConnection.c

@@ -39,7 +39,7 @@ static void pending_handler (ServerConnection *o);
 static SECStatus client_auth_data_callback (ServerConnection *o, PRFileDesc *fd, CERTDistNames *caNames, CERTCertificate **pRetCert, SECKEYPrivateKey **pRetKey);
 static void connection_handler (ServerConnection *o, int event);
 static void sslcon_handler (ServerConnection *o, int event);
-static void decoder_handler (ServerConnection *o, int component, int code);
+static void decoder_handler_error (ServerConnection *o);
 static void input_handler_send (ServerConnection *o, uint8_t *data, int data_len);
 static void packet_hello (ServerConnection *o, uint8_t *data, int data_len);
 static void packet_newclient (ServerConnection *o, uint8_t *data, int data_len);
@@ -120,8 +120,7 @@ void connector_handler (ServerConnection *o, int is_error)
     
     // init input chain
     PacketPassInterface_Init(&o->input_interface, SC_MAX_ENC, (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(o->reactor));
-    FlowErrorDomain_Init(&o->input_decoder_domain, (FlowErrorDomain_handler)decoder_handler, o);
-    if (!PacketProtoDecoder_Init(&o->input_decoder, FlowErrorReporter_Create(&o->input_decoder_domain, 0), recv_iface, &o->input_interface, BReactor_PendingGroup(o->reactor))) {
+    if (!PacketProtoDecoder_Init(&o->input_decoder, recv_iface, &o->input_interface, BReactor_PendingGroup(o->reactor), o, (PacketProtoDecoder_handler_error)decoder_handler_error)) {
         BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
         goto fail2;
     }
@@ -264,7 +263,7 @@ void sslcon_handler (ServerConnection *o, int event)
     return;
 }
 
-void decoder_handler (ServerConnection *o, int component, int code)
+void decoder_handler_error (ServerConnection *o)
 {
     DebugObject_Access(&o->d_obj);
     ASSERT(o->state >= STATE_WAITINIT)

+ 0 - 2
server_connection/ServerConnection.h

@@ -45,7 +45,6 @@
 #include <protocol/msgproto.h>
 #include <base/DebugObject.h>
 #include <system/BConnection.h>
-#include <flow/FlowError.h>
 #include <flow/PacketProtoEncoder.h>
 #include <flow/PacketStreamSender.h>
 #include <flow/PacketProtoDecoder.h>
@@ -158,7 +157,6 @@ typedef struct {
     BSSLConnection sslcon;
     
     // input
-    FlowErrorDomain input_decoder_domain;
     PacketProtoDecoder input_decoder;
     PacketPassInterface input_interface;
     

+ 5 - 6
udpgw/udpgw.c

@@ -63,7 +63,6 @@ struct client {
     BConnection con;
     BAddr addr;
     BTimer disconnect_timer;
-    FlowErrorDomain recv_decoder_domain;
     PacketProtoDecoder recv_decoder;
     PacketPassInterface recv_if;
     PacketPassFairQueue send_queue;
@@ -89,7 +88,6 @@ struct connection {
     union {
         struct {
             BDatagram udp_dgram;
-            FlowErrorDomain udp_domain;
             BufferWriter udp_send_writer;
             PacketBuffer udp_send_buffer;
             SinglePacketBuffer udp_recv_buffer;
@@ -150,7 +148,7 @@ static void client_free (struct client *client);
 static void client_log (struct client *client, int level, const char *fmt, ...);
 static void client_disconnect_timer_handler (struct client *client);
 static void client_connection_handler (struct client *client, int event);
-static void client_decoder_handler (struct client *client, int component, int code);
+static void client_decoder_handler_error (struct client *client);
 static void client_recv_if_handler_send (struct client *client, uint8_t *data, int data_len);
 static void connection_init (struct client *client, uint16_t conid, BAddr addr, const uint8_t *data, int data_len);
 static void connection_free (struct connection *con);
@@ -547,8 +545,9 @@ void listener_handler (BListener *listener)
     PacketPassInterface_Init(&client->recv_if, udpgw_mtu, (PacketPassInterface_handler_send)client_recv_if_handler_send, client, BReactor_PendingGroup(&ss));
     
     // init recv decoder
-    FlowErrorDomain_Init(&client->recv_decoder_domain, (FlowErrorDomain_handler)client_decoder_handler, client);
-    if (!PacketProtoDecoder_Init(&client->recv_decoder, FlowErrorReporter_Create(&client->recv_decoder_domain, 0), BConnection_RecvAsync_GetIf(&client->con), &client->recv_if, BReactor_PendingGroup(&ss))) {
+    if (!PacketProtoDecoder_Init(&client->recv_decoder, BConnection_RecvAsync_GetIf(&client->con), &client->recv_if, BReactor_PendingGroup(&ss), client,
+        (PacketProtoDecoder_handler_error)client_decoder_handler_error
+    )) {
         BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
         goto fail2;
     }
@@ -669,7 +668,7 @@ void client_connection_handler (struct client *client, int event)
     client_free(client);
 }
 
-void client_decoder_handler (struct client *client, int component, int code)
+void client_decoder_handler_error (struct client *client)
 {
     client_log(client, BLOG_ERROR, "decoder error");
     

+ 3 - 6
udpgw_client/UdpGwClient.c

@@ -35,7 +35,7 @@ static int uint16_comparator (void *unused, uint16_t *v1, uint16_t *v2);
 static int compare_addresses (BAddr v1, BAddr v2);
 static int conaddr_comparator (void *unused, struct UdpGwClient_conaddr *v1, struct UdpGwClient_conaddr *v2);
 static void free_server (UdpGwClient *o);
-static void server_error_handler (UdpGwClient *o, int component, int code);
+static void decoder_handler_error (UdpGwClient *o);
 static void recv_interface_handler_send (UdpGwClient *o, uint8_t *data, int data_len);
 static void send_monitor_handler (UdpGwClient *o);
 static void keepalive_if_handler_done (UdpGwClient *o);
@@ -108,7 +108,7 @@ static void free_server (UdpGwClient *o)
     PacketPassInterface_Free(&o->recv_if);
 }
 
-static void server_error_handler (UdpGwClient *o, int component, int code)
+static void decoder_handler_error (UdpGwClient *o)
 {
     DebugObject_Access(&o->d_obj);
     ASSERT(o->have_server)
@@ -520,14 +520,11 @@ int UdpGwClient_ConnectServer (UdpGwClient *o, StreamPassInterface *send_if, Str
     DebugObject_Access(&o->d_obj);
     ASSERT(!o->have_server)
     
-    // init error domain
-    FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)server_error_handler, o);
-    
     // init receive interface
     PacketPassInterface_Init(&o->recv_if, o->udpgw_mtu, (PacketPassInterface_handler_send)recv_interface_handler_send, o, BReactor_PendingGroup(o->reactor));
     
     // init receive decoder
-    if (!PacketProtoDecoder_Init(&o->recv_decoder, FlowErrorReporter_Create(&o->domain, 0), recv_if, &o->recv_if, BReactor_PendingGroup(o->reactor))) {
+    if (!PacketProtoDecoder_Init(&o->recv_decoder, recv_if, &o->recv_if, BReactor_PendingGroup(o->reactor), o, (PacketProtoDecoder_handler_error)decoder_handler_error)) {
         BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
         goto fail1;
     }

+ 0 - 1
udpgw_client/UdpGwClient.h

@@ -69,7 +69,6 @@ typedef struct {
     PacketPassFairQueueFlow keepalive_qflow;
     int keepalive_sending;
     int have_server;
-    FlowErrorDomain domain;
     PacketStreamSender send_sender;
     PacketProtoDecoder recv_decoder;
     PacketPassInterface recv_if;