1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/*
** Copyright 1998 - 2006 Double Precision, Inc.
** See COPYING for distribution information.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <time.h>
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include "imapwrite.h"
static char outbuf[BUFSIZ]; /* Ye olde output buffer */
static size_t outbuf_cnt=0; /* How much stuff's in ye olde output buffer */
extern FILE *debugfile;
extern unsigned long bytes_sent_count; /* counter for sent bytes (imapwrite.c) */
extern void disconnected();
void writeflush()
{
const char *p=outbuf;
unsigned s=outbuf_cnt;
time_t t, tend;
fd_set fds;
struct timeval tv;
int n;
if (s == 0) return;
time(&t);
tend=t+SOCKET_TIMEOUT;
if (debugfile)
{
fprintf(debugfile, "WRITE: ");
if (fwrite(p, 1, s, debugfile) == 1)
fprintf(debugfile, "\n");
fflush(debugfile);
}
while (t < tend)
{
FD_ZERO(&fds);
FD_SET(1, &fds);
tv.tv_sec=tend-t;
tv.tv_usec=0;
/* BUG: if client closes connection BEFORE we flush it, select "stucks"
* until timeout. To workaround this, we should "write" first, and then
* if we get EPIPE connection is already closed. Othervise, try select
*/
if ((n=write(1, p, s)) <= 0)
{
if (errno == EPIPE ||
select(2, 0, &fds, 0, &tv) <= 0 ||
!FD_ISSET(1, &fds) ||
(n=write(1, p, s)) <= 0)
{
disconnected();
return;
}
}
bytes_sent_count += n;
p += n;
s -= n;
if (s == 0) break;
time(&t);
}
if (s) disconnected();
outbuf_cnt=0;
}
void writemem(const char *s, size_t l)
{
size_t n;
while (l)
{
n=sizeof(outbuf) - outbuf_cnt;
if (n >= l)
{
memcpy(outbuf+outbuf_cnt, s, l);
outbuf_cnt += l;
break;
}
if (n == 0)
{
writeflush();
continue;
}
if (n > l) n=l;
memcpy(outbuf+outbuf_cnt, s, n);
outbuf_cnt += n;
l -= n;
s += n;
}
}
void writes(const char *s)
{
writemem(s, strlen(s));
}
void writen(unsigned long n)
{
char buf[40];
sprintf(buf, "%lu", n);
writemem(buf, strlen(buf));
}
void writeqs(const char *s)
{
size_t i=strlen(s), j;
while (i)
{
for (j=0; j<i; j++)
{
if ( s[j] == '"' || s[j] == '\\')
{
writemem(s, j);
writemem("\\", 1);
writemem(s+j, 1);
++j;
s += j;
i -= j;
j=0;
break;
}
#if 0
if (s[j] == '&')
{
writemem(s, j);
writemem("&-", 2);
++j;
s += j;
i -= j;
j=0;
break;
}
if (s[j] < ' ' || s[j] >= 0x7F)
{
char *q;
writemem(s, j);
++j;
s += j;
i -= j;
for (j=0; j<i; j++)
if (s[j] >= ' ' && s[j] < 0x7F)
break;
q=imap_utf7_encode(s, j);
if (!q) write_error_exit(0);
writemem("&", 1);
writes(q);
writemem("-", 1);
s += j;
i -= j;
j=0;
break;
}
#endif
}
writemem(s, j);
s += j;
i -= j;
}
}
void write_error_exit(const char *funcname)
{
outbuf_cnt=0;
writes("* BYE [ALERT] Fatal error: ");
if (funcname && *funcname)
{
writes(funcname);
writes(": ");
}
if (errno)
{
#if HAVE_STRERROR
const char *p;
p=strerror(errno);
#else
char p[40];
sprintf(p, "Error %d", (int)errno);
#endif
writes(p);
}
writes("\r\n");
writeflush();
if (funcname && *funcname)
{
fprintf(stderr, "ERR: %s: %s\n", getenv("AUTHENTICATED"),
funcname);
fflush(stderr);
}
_exit(1);
}
|