1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-06-16 06:15:24 +00:00

Another net change, making it more bullet-proof, before could silently miss

data. so now we allocate enough space for the write to succeed fully.

svn path=/trunk/net/; revision=4888
This commit is contained in:
Karl Heyes 2003-06-06 00:05:19 +00:00
parent eb5afc0cfb
commit 08bf02cf67
2 changed files with 46 additions and 11 deletions

View File

@ -57,6 +57,16 @@
#include "sock.h"
#include "resolver.h"
#if 0
#ifndef HAVE_VA_COPY
#ifdef HAVE___VA_COPY
#define va_copy(dest,src) __va_copy(dest, src)
#else
#define va_copy(dest,src) memcpy(&dest, &src, sizeof (va_list))
#endif
#endif
#endif
/* sock_initialize
**
** initializes the socket library. you must call this
@ -312,25 +322,50 @@ int sock_write_string(sock_t sock, const char *buff)
*/
int sock_write(sock_t sock, const char *fmt, ...)
{
char buff[1024];
int rc;
va_list ap;
va_start(ap, fmt);
vsnprintf(buff, 1024, fmt, ap);
va_end(ap);
return sock_write_bytes(sock, buff, strlen(buff));
va_start (ap, fmt);
rc = sock_write_fmt (sock, fmt, ap);
va_end (ap);
return rc;
}
int sock_write_fmt(sock_t sock, char *fmt, va_list ap)
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
{
char buff[1024];
char buffer [1024], *buff = buffer;
int len;
int rc = SOCK_ERROR;
va_list ap_retry;
vsnprintf(buff, 1024, fmt, ap);
va_copy (ap_retry, ap);
return sock_write_bytes(sock, buff, strlen(buff));
len = vsnprintf (buff, sizeof (buffer), fmt, ap);
if (len > 0)
{
if ((size_t)len < sizeof (buffer)) /* common case */
rc = sock_write_bytes(sock, buff, (size_t)len);
else
{
/* truncated */
buff = malloc (++len);
if (buff)
{
len = vsnprintf (buff, len, fmt, ap_retry);
va_end (ap_retry);
if (len > 0)
rc = sock_write_bytes (sock, buff, len);
free (buff);
}
}
}
return rc;
}
int sock_read_bytes(sock_t sock, char *buff, const int len)
{

View File

@ -88,7 +88,7 @@ int sock_connected (int sock, unsigned timeout);
/* Socket write functions */
int sock_write_bytes(sock_t sock, const void *buff, const size_t len);
int sock_write(sock_t sock, const char *fmt, ...);
int sock_write_fmt(sock_t sock, char *fmt, va_list ap);
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap);
int sock_write_string(sock_t sock, const char *buff);
ssize_t sock_writev (int sock, const struct iovec *iov, const size_t count);