From 74073bc04f72d2623fee404ed23419fa78f80def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net> Date: Sat, 4 Jan 2025 04:11:05 -0500 Subject: [PATCH] Remove sendfilesocket() and recvfilesocket() Make js_socket_sendfilesocket() suck a lot less. This commit brought to you with limited rants by Synchronet 3.20b "Warning: Your BBS may become habit forming." You could run Synchronet or you could settle for mediocrity. Once in a great while, there comes BBS software that really makes waves. Get out your surfboard. The best BBS software is the most expensive BBS software. NOT! If you had three wishes, you could toss the other two. They couldn't top Synchronet. So we did. Accept the inevitable, switch to Synchronet --- src/sbbs3/js_socket.c | 83 +++++++++++++------------------- src/xpdev/sockwrap.c | 109 ------------------------------------------ src/xpdev/sockwrap.h | 3 -- src/xpdev/xpbeep.c | 2 +- 4 files changed, 33 insertions(+), 164 deletions(-) diff --git a/src/sbbs3/js_socket.c b/src/sbbs3/js_socket.c index 0310ce212e..f2b0569abb 100644 --- a/src/sbbs3/js_socket.c +++ b/src/sbbs3/js_socket.c @@ -68,7 +68,7 @@ static bool js_socket_peek_byte(JSContext *cx, js_socket_private_t *p); static JSBool js_socket_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp); static ptrdiff_t js_socket_recv(JSContext *cx, js_socket_private_t *p, void *buf, size_t len, int flags, int timeout); static JSBool js_socket_resolve(JSContext *cx, JSObject *obj, jsid id); -static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file, off_t *offset, off_t count); +static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file); static ptrdiff_t js_socket_sendsocket(js_socket_private_t *p, const void *msg, size_t len, int flush); static JSBool js_socket_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp); static JSBool js_install_event(JSContext *cx, uintN argc, jsval *arglist, bool once); @@ -393,66 +393,47 @@ static ptrdiff_t js_socket_sendsocket(js_socket_private_t *p, const void *msg, s return total; } -static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file, off_t *offset, off_t count) +static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file) { - char buf[1024*16]; - off_t len; - int rd; - int wr=0; - off_t total=0; - int i; - - if(p->session==-1) - return (int)sendfilesocket(p->sock, file, offset, count); + char buf[1024*16]; + off_t total = 0; - len=filelength(file); - - if(offset!=NULL) - if(lseek(file,*offset,SEEK_SET)<0) - return(-1); - - if(count<1 || count>len) { - count=len; - count-=tell(file); /* don't try to read beyond EOF */ - } - - if(count<0) { - errno=EINVAL; - return(-1); - } - - while(total<count) { - rd=read(file,buf,sizeof(buf)); - if(rd==-1) { - do_CryptFlush(p); - return(-1); + for (;;) { + ssize_t rd = read(file, buf, sizeof(buf)); + if (rd < 0) { + if (p->session != -1) + do_CryptFlush(p); + return (-1); } - if(rd==0) + if (rd == 0) break; - for(i=wr=0;i<rd;i+=wr) { - wr=js_socket_sendsocket(p,buf+i,rd-i,false); - if(wr>0) - continue; - if(wr==SOCKET_ERROR && SOCKET_ERRNO==EWOULDBLOCK) { + size_t sent = 0; + while (sent < rd) { + ptrdiff_t wr = js_socket_sendsocket(p, buf + sent, rd - sent, false); + if (wr > 0) { + sent += wr; + } + else if (wr == SOCKET_ERROR && SOCKET_ERRNO == EWOULDBLOCK) { wr=0; SLEEP(1); - continue; } - do_CryptFlush(p); - return(wr); + else { + if (p->session != -1) + do_CryptFlush(p); + return (wr); + } } - if(i!=rd) { - do_CryptFlush(p); - return(-1); + if (sent != rd) { + if (p->session != -1) + do_CryptFlush(p); + return (-1); } - total+=rd; + total += rd; } - if(offset!=NULL) - (*offset)+=total; - - do_CryptFlush(p); - return(total); + if (p->session != -1) + do_CryptFlush(p); + return (total); } static void dbprintf(bool error, js_socket_private_t* p, char* fmt, ...) @@ -1328,7 +1309,7 @@ js_sendfile(JSContext *cx, uintN argc, jsval *arglist) return(JS_TRUE); } - len = js_socket_sendfilesocket(p, file, NULL, 0); + len = js_socket_sendfilesocket(p, file); close(file); if(len > 0) { dbprintf(false, p, "sent %"PRIdOFF" bytes",len); diff --git a/src/xpdev/sockwrap.c b/src/xpdev/sockwrap.c index f013652f78..43716a5192 100644 --- a/src/xpdev/sockwrap.c +++ b/src/xpdev/sockwrap.c @@ -153,115 +153,6 @@ socket_option_t* getSocketOptionList(void) return(socket_options); } -// TODO: Only called with *offset == NULL and count == 0... -off_t sendfilesocket(int sock, int file, off_t *offset, off_t count) -{ - char buf[1024*16]; - off_t len; - off_t total=0; - - // TODO: Race condition here... length may change. - // But note that js_socket_sendfilesocket() reimplements all of this - // for encrypted sockets. - len = filelength(file); - - if(offset!=NULL) - if(lseek(file,*offset,SEEK_SET)<0) - return(-1); - - if (count < 1 || count > len) { - count = len; - count -= tell(file); /* don't try to read beyond EOF (why not? --- Deuce) */ - } - - if (count < 0) { - errno = EINVAL; - return(-1); - } - - while (total < count) { - ssize_t rd = read(file, buf, sizeof(buf)); - ssize_t sent = 0; - if (rd < 0) // Error - return(-1); - if (rd == 0) // EOF - break; - while (sent < rd) { - ssize_t wr = sendsocket(sock, buf + sent, rd - sent); - if (wr > 0 && wr <= (rd - sent)) { - sent += wr; - } - else if (wr == SOCKET_ERROR && SOCKET_ERRNO == EWOULDBLOCK) { - SLEEP(1); - } - else { - // TODO: This is sketchy to return 0 on write failure - return(wr); - } - } - total += rd; - } - - if (offset != NULL) - (*offset) += total; - - return(total); -} - -off_t recvfilesocket(int sock, int file, off_t *offset, off_t count) -{ - /* Writes a file from a socket - - * - * sock - Socket to read from - * file - File descriptior to write to - * MUST be open and writeable - * offset - pointer to file offset to start writing at - * is set to offset writing STOPPED - * on return - * count - number of bytes to read/write - * - * returns -1 if an error occurse, otherwise - * returns number ob bytes written and sets offset - * to the new offset - */ - - char* buf; - ssize_t rd; - ssize_t wr; - - if(count<1) { - errno=ERANGE; - return(-1); - } - - if((buf=(char*)malloc((size_t)count))==NULL) { - errno=ENOMEM; - return(-1); - } - - if(offset!=NULL) { - if(lseek(file,*offset,SEEK_SET)<0) { - free(buf); - return(-1); - } - } - - rd=read(sock,buf,(size_t)count); - if(rd!=count) { - free(buf); - return(-1); - } - - wr=write(file,buf,rd); - - if(offset!=NULL) - (*offset)+=wr; - - free(buf); - return(wr); -} - - /* Return true if connected, optionally sets *rd_p to true if read data available */ /* * The exact conditions where rd_p is set to true and the return value diff --git a/src/xpdev/sockwrap.h b/src/xpdev/sockwrap.h index 963f5bdfca..a82d27eb85 100644 --- a/src/xpdev/sockwrap.h +++ b/src/xpdev/sockwrap.h @@ -239,9 +239,6 @@ extern "C" { DLLEXPORT socket_option_t* getSocketOptionList(void); DLLEXPORT int getSocketOptionByName(const char* name, int* level); - -DLLEXPORT off_t sendfilesocket(int sock, int file, off_t* offset, off_t count); -DLLEXPORT off_t recvfilesocket(int sock, int file, off_t* offset, off_t count); DLLEXPORT bool socket_check(SOCKET sock, bool* rd_p, bool* wr_p, DWORD timeout); DLLEXPORT int retry_bind(SOCKET s, const struct sockaddr *addr, socklen_t addrlen ,uint retries, uint wait_secs, const char* prot diff --git a/src/xpdev/xpbeep.c b/src/xpdev/xpbeep.c index eb4c864952..27fe510878 100644 --- a/src/xpdev/xpbeep.c +++ b/src/xpdev/xpbeep.c @@ -976,7 +976,7 @@ do_xp_play_sample(unsigned char *sampo, size_t sz, int *freed) size_t wr = 0; while (wr < sz) { ssize_t i = write(dsp, samp + wr, sz - wr); - if (i >= 0 && i <= (sz - wr)) + if (i >= 0) wr += i; else return false; -- GitLab