From: Guido Vranken Date: Tue, 27 Jun 2017 08:54:01 +0000 (+0200) Subject: Implement platform_read, platform_write and replace all read()/write() calls X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df5659cd978cb43dcb37330f9c7338d8d1d9f7b1;p=thirdparty%2Fopenvpn.git Implement platform_read, platform_write and replace all read()/write() calls --- diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 72fcc8c6a..301b2e4aa 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -328,7 +328,7 @@ void buf_write_string_file(const struct buffer *buf, const char *filename, int fd) { const int len = strlen((char *) BPTR(buf)); - const int size = write(fd, BPTR(buf), len); + const int size = platform_write(fd, BPTR(buf), len); if (size != len) { msg(M_ERR, "Write error on file '%s'", filename); diff --git a/src/openvpn/console_systemd.c b/src/openvpn/console_systemd.c index 8cee8c8ed..1d18b1a7d 100644 --- a/src/openvpn/console_systemd.c +++ b/src/openvpn/console_systemd.c @@ -33,6 +33,7 @@ #include "syshead.h" #include "console.h" #include "misc.h" +#include "platform.h" #include @@ -77,7 +78,7 @@ get_console_input_systemd(const char *prompt, const bool echo, char *input, cons return false; } memset(input, 0, capacity); - if (read(std_out, input, capacity-1) != 0) + if (platform_read(std_out, input, capacity-1) != 0) { chomp(input); ret = true; diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index 191fee8e0..f0c22bf43 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -1263,7 +1263,7 @@ read_key_file(struct key2 *key2, const char *file, const unsigned int flags) { msg(M_ERR, "Cannot open file key file '%s'", file); } - size = read(fd, in.data, in.capacity); + size = platform_read(fd, in.data, in.capacity); if (size < 0) { msg(M_FATAL, "Read error on key file ('%s')", file); diff --git a/src/openvpn/mstats.c b/src/openvpn/mstats.c index 9b0918864..4d02654f2 100644 --- a/src/openvpn/mstats.c +++ b/src/openvpn/mstats.c @@ -40,6 +40,7 @@ #include "error.h" #include "misc.h" #include "mstats.h" +#include "platform.h" #include "memdbg.h" @@ -77,7 +78,7 @@ mstats_open(const char *fn) * struct mmap_stats, and zero it */ CLEAR(ms); ms.state = MSTATS_ACTIVE; - stat = write(fd, &ms, sizeof(ms)); + stat = platform_write(fd, &ms, sizeof(ms)); if (stat != sizeof(ms)) { msg(M_ERR, "mstats_open: write error: %s", fn); diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 8d3d67fd5..0c2c0fedc 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -44,6 +44,7 @@ #include "mstats.h" #include "ssl_verify.h" #include +#include "platform.h" #include "memdbg.h" @@ -2119,7 +2120,7 @@ multi_process_file_closed(struct multi_context *m, const unsigned int mpp_flags) { char buffer[INOTIFY_EVENT_BUFFER_SIZE]; size_t buffer_i = 0; - int r = read(m->top.c2.inotify_fd, buffer, INOTIFY_EVENT_BUFFER_SIZE); + int r = platform_read(m->top.c2.inotify_fd, buffer, INOTIFY_EVENT_BUFFER_SIZE); while (buffer_i < r) { diff --git a/src/openvpn/packet_id.c b/src/openvpn/packet_id.c index fab9c62dc..e544dfa64 100644 --- a/src/openvpn/packet_id.c +++ b/src/openvpn/packet_id.c @@ -43,6 +43,7 @@ #include "packet_id.h" #include "misc.h" #include "integer.h" +#include "platform.h" #include "memdbg.h" @@ -460,7 +461,7 @@ packet_id_persist_load(struct packet_id_persist *p, const char *filename) #endif p->filename = filename; - n = read(p->fd, &image, sizeof(image)); + n = platform_read(p->fd, &image, sizeof(image)); if (n == sizeof(image)) { p->time = p->time_last_written = image.time; @@ -496,7 +497,7 @@ packet_id_persist_save(struct packet_id_persist *p) seek_ret = lseek(p->fd, (off_t)0, SEEK_SET); if (seek_ret == (off_t)0) { - n = write(p->fd, &image, sizeof(image)); + n = platform_write(p->fd, &image, sizeof(image)); if (n == sizeof(image)) { p->time_last_written = p->time; diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c index cbd64411d..0efa6e270 100644 --- a/src/openvpn/platform.c +++ b/src/openvpn/platform.c @@ -396,6 +396,14 @@ ssize_t platform_send(int sockfd, const void *buf, size_t len, int flags) return send(sockfd, buf, len, flags); } +ssize_t platform_read(int fd, void* buf, size_t len) +{ + return platform_recv(fd, buf, len, 0); +} +ssize_t platform_write(int fd, const void* buf, size_t len) +{ + return platform_send(fd, buf, len, 0); +} ssize_t platform_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { diff --git a/src/openvpn/platform.h b/src/openvpn/platform.h index 93ff2b2a3..c633959d1 100644 --- a/src/openvpn/platform.h +++ b/src/openvpn/platform.h @@ -149,6 +149,8 @@ int platform_stat(const char *path, platform_stat_t *buf); ssize_t platform_recv(int sockfd, void* buf, size_t len, int flags); ssize_t platform_send(int sockfd, const void* buf, size_t len, int flags); +ssize_t platform_read(int fd, void* buf, size_t len); +ssize_t platform_write(int fd, const void* buf, size_t len); ssize_t platform_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); char* platform_fgets(char *s, int size, FILE *stream); int platform_fgetc(FILE *stream); diff --git a/src/openvpn/ps.c b/src/openvpn/ps.c index 1e062d873..b2bc4da9a 100644 --- a/src/openvpn/ps.c +++ b/src/openvpn/ps.c @@ -36,6 +36,7 @@ #include "fdmisc.h" #include "crypto.h" #include "ps.h" +#include "platform.h" #include "memdbg.h" @@ -145,7 +146,7 @@ static int recv_control(const socket_descriptor_t fd) { unsigned char c; - const ssize_t size = read(fd, &c, sizeof(c)); + const ssize_t size = platform_read(fd, &c, sizeof(c)); if (size == sizeof(c)) { return c; @@ -160,7 +161,7 @@ static int send_control(const socket_descriptor_t fd, int code) { unsigned char c = (unsigned char) code; - const ssize_t size = write(fd, &c, sizeof(c)); + const ssize_t size = platform_write(fd, &c, sizeof(c)); if (size == sizeof(c)) { return (int) size; @@ -361,7 +362,7 @@ journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c fd = platform_open(jfn, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP); if (fd != -1) { - if (write(fd, f, strlen(f)) != strlen(f)) + if (platform_write(fd, f, strlen(f)) != strlen(f)) { msg(M_WARN, "PORT SHARE: writing to journal file (%s) failed", jfn); } diff --git a/src/openvpn/route.c b/src/openvpn/route.c index fdd37cc0d..96388e8b1 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -41,6 +41,7 @@ #include "manage.h" #include "win32.h" #include "options.h" +#include "platform.h" #include "memdbg.h" @@ -3625,14 +3626,14 @@ get_default_gateway(struct route_gateway_info *rgi) msg(M_WARN, "GDG: socket #1 failed"); goto done; } - if (write(sockfd, (char *)&m_rtmsg, l) < 0) + if (platform_write(sockfd, (char *)&m_rtmsg, l) < 0) { msg(M_WARN, "GDG: problem writing to routing socket"); goto done; } do { - l = read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg)); + l = platform_read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg)); } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid)); close(sockfd); sockfd = -1; @@ -3852,7 +3853,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, msg(M_WARN, "GDG6: socket #1 failed"); goto done; } - if (write(sockfd, (char *)&m_rtmsg, l) < 0) + if (platform_write(sockfd, (char *)&m_rtmsg, l) < 0) { msg(M_WARN, "GDG6: problem writing to routing socket"); goto done; @@ -3860,7 +3861,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, do { - l = read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg)); + l = platform_read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg)); } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid)); diff --git a/src/openvpn/status.c b/src/openvpn/status.c index a16340837..0b6ac8795 100644 --- a/src/openvpn/status.c +++ b/src/openvpn/status.c @@ -33,6 +33,7 @@ #include "perf.h" #include "misc.h" #include "fdmisc.h" +#include "platform.h" #include "memdbg.h" @@ -266,7 +267,7 @@ status_printf(struct status_output *so, const char *format, ...) len = strlen(buf); if (len > 0) { - if (write(so->fd, buf, len) != len) + if (platform_write(so->fd, buf, len) != len) { so->errors = true; } @@ -300,7 +301,7 @@ status_read(struct status_output *so, struct buffer *buf) int len; ASSERT(buf_init(&so->read_buf, 0)); - len = read(so->fd, BPTR(&so->read_buf), BCAP(&so->read_buf)); + len = platform_read(so->fd, BPTR(&so->read_buf), BCAP(&so->read_buf)); if (len <= 0) { break; diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 710f8f288..7ec3025c9 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -45,6 +45,7 @@ #include "manage.h" #include "route.h" #include "win32.h" +#include "platform.h" #include "memdbg.h" @@ -1675,7 +1676,7 @@ write_tun_header(struct tuntap *tt, uint8_t *buf, int len) } else { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } } @@ -1696,7 +1697,7 @@ read_tun_header(struct tuntap *tt, uint8_t *buf, int len) } else { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } } #endif /* if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H) */ @@ -1908,13 +1909,13 @@ close_tun(struct tuntap *tt) int write_tun(struct tuntap *tt, uint8_t *buf, int len) { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } int read_tun(struct tuntap *tt, uint8_t *buf, int len) { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } #elif defined(TARGET_LINUX) @@ -2177,13 +2178,13 @@ close_tun(struct tuntap *tt) int write_tun(struct tuntap *tt, uint8_t *buf, int len) { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } int read_tun(struct tuntap *tt, uint8_t *buf, int len) { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } #elif defined(TARGET_SOLARIS) @@ -2745,7 +2746,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len) } else { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } } @@ -2766,7 +2767,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len) } else { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } } @@ -2869,7 +2870,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len) } else { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } } @@ -2890,7 +2891,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len) } else { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } } @@ -2964,7 +2965,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len) } else { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } } @@ -2985,7 +2986,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len) } else { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } } @@ -3221,7 +3222,7 @@ write_tun(struct tuntap *tt, uint8_t *buf, int len) } else #endif - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } int @@ -3234,7 +3235,7 @@ read_tun(struct tuntap *tt, uint8_t *buf, int len) } else #endif - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } #elif defined(TARGET_AIX) @@ -3370,13 +3371,13 @@ close_tun(struct tuntap *tt) int write_tun(struct tuntap *tt, uint8_t *buf, int len) { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } int read_tun(struct tuntap *tt, uint8_t *buf, int len) { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } #elif defined(_WIN32) @@ -6359,13 +6360,13 @@ close_tun(struct tuntap *tt) int write_tun(struct tuntap *tt, uint8_t *buf, int len) { - return write(tt->fd, buf, len); + return platform_write(tt->fd, buf, len); } int read_tun(struct tuntap *tt, uint8_t *buf, int len) { - return read(tt->fd, buf, len); + return platform_read(tt->fd, buf, len); } #endif /* if defined (TARGET_ANDROID) */