gracefully handle errors on socket-write
authorMichael Gernoth <michael@gernoth.net>
Wed, 5 Jun 2013 12:09:52 +0000 (14:09 +0200)
committerMichael Gernoth <michael@gernoth.net>
Wed, 5 Jun 2013 12:09:52 +0000 (14:09 +0200)
hmcfgusb.c
hmcfgusb.h
hmland.c
hmsniff.c

index 9f02480..7ec0714 100644 (file)
@@ -225,7 +225,17 @@ static void LIBUSB_CALL hmcfgusb_interrupt(struct libusb_transfer *transfer)
                if (cb_data && cb_data->cb) {
                        if (debug)
                                hexdump(transfer->buffer, transfer->actual_length, "USB > ");
-                       cb_data->cb(transfer->buffer, transfer->actual_length, cb_data->data);
+
+                       if (!cb_data->cb(transfer->buffer, transfer->actual_length, cb_data->data)) {
+                               quit = EIO;
+
+                               if (cb_data && cb_data->dev && cb_data->dev->transfer) {
+                                       libusb_free_transfer(cb_data->dev->transfer);
+                                       cb_data->dev->transfer = NULL;
+                               }
+
+                               return;
+                       }
                } else {
                        hexdump(transfer->buffer, transfer->actual_length, "> ");
                }
index e33f6e6..8a0751a 100644 (file)
@@ -21,7 +21,7 @@
  * IN THE SOFTWARE.
  */
 
-typedef void (*hmcfgusb_cb_fn)(uint8_t *buf, int buf_len, void *data);
+typedef int (*hmcfgusb_cb_fn)(uint8_t *buf, int buf_len, void *data);
 
 struct hmcfgusb_dev {
        libusb_device_handle *usb_devh;
index 608c887..04ee740 100644 (file)
--- a/hmland.c
+++ b/hmland.c
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <strings.h>
 #include <poll.h>
+#include <signal.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -172,15 +173,16 @@ static int parse_part_in(uint8_t **inpos, int inlen, uint8_t **outpos, int outle
        return *outpos - buf_out;
 }
 
-static void hmlan_format_out(uint8_t *buf, int buf_len, void *data)
+static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
 {
        uint8_t out[1024];
        uint8_t *outpos;
        uint8_t *inpos;
        int fd = *((int*)data);
+       int w;
 
        if (buf_len < 1)
-               return;
+               return 1;
 
        memset(out, 0, sizeof(out));
        outpos = out;
@@ -233,9 +235,16 @@ static void hmlan_format_out(uint8_t *buf, int buf_len, void *data)
                        hexdump(buf, buf_len, "Unknown> ");
                        break;
        }
-       write(fd, out, outpos-out);
        if (debug)
                fprintf(stderr, "LAN < %s\n", out);
+
+       w = write(fd, out, outpos-out);
+       if (w <= 0) {
+               perror("write");
+               return 0;
+       }
+
+       return 1;
 }
 
 static int hmlan_parse_in(int fd, void *data)
@@ -372,6 +381,7 @@ static int comm(int fd_in, int fd_out, int master_socket)
 
 static int socket_server(int port, int daemon)
 {
+       struct sigaction sact;
        struct sockaddr_in sin;
        int sock;
        int n;
@@ -388,6 +398,13 @@ static int socket_server(int port, int daemon)
                }
        }
 
+       memset(&sact, 0, sizeof(sact));
+       sact.sa_handler = SIG_IGN;
+
+       if (sigaction(SIGPIPE, &sact, NULL) == -1) {
+               perror("sigaction");
+       }
+
        impersonate_hmlanif = 1;
 
        sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
index 67a2df6..e842453 100644 (file)
--- a/hmsniff.c
+++ b/hmsniff.c
@@ -138,12 +138,12 @@ struct recv_data {
        int wrong_hmid;
 };
 
-static void parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
+static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
 {
        struct recv_data *rdata = data;
 
        if (buf_len < 1)
-               return;
+               return 1;
 
        switch(buf[0]) {
                case 'E':
@@ -164,6 +164,8 @@ static void parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
                        hexdump(buf, buf_len, "Unknown> ");
                        break;
        }
+
+       return 1;
 }
 
 int main(int argc, char **argv)
Impressum, Datenschutz