From: Michael Gernoth Date: Wed, 5 Jun 2013 12:09:52 +0000 (+0200) Subject: gracefully handle errors on socket-write X-Git-Tag: v0.100~86 X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/hmcfgusb/commitdiff_plain/4371275b6337ea3ae513f1c19463f8b4745d89d8?hp=f77690c303b7bd05a04603e6383613020cfe1ce6 gracefully handle errors on socket-write --- diff --git a/hmcfgusb.c b/hmcfgusb.c index 9f02480..7ec0714 100644 --- a/hmcfgusb.c +++ b/hmcfgusb.c @@ -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, "> "); } diff --git a/hmcfgusb.h b/hmcfgusb.h index e33f6e6..8a0751a 100644 --- a/hmcfgusb.h +++ b/hmcfgusb.h @@ -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; diff --git a/hmland.c b/hmland.c index 608c887..04ee740 100644 --- a/hmland.c +++ b/hmland.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/hmsniff.c b/hmsniff.c index 67a2df6..e842453 100644 --- 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)