-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathbsd.c
174 lines (167 loc) · 4.95 KB
/
bsd.c
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
/************************************************************************
* IRC - Internet Relay Chat, src/bsd.c
* Copyright (C) 1990 Jarkko Oikarinen and
* University of Oulu, Computing Center
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "h.h"
#include "fds.h"
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
extern int errno; /* ...seems that errno.h doesn't define this everywhere */
#ifndef SYS_ERRLIST_DECLARED
extern char *sys_errlist[];
#endif
#if defined(DEBUGMODE) || defined (DNS_DEBUG)
int writecalls = 0, writeb[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int readcalls = 0;
#endif
void dummy()
{
struct sigaction act;
act.sa_handler = dummy;
act.sa_flags = 0;
(void) sigemptyset(&act.sa_mask);
(void) sigaddset(&act.sa_mask, SIGALRM);
(void) sigaddset(&act.sa_mask, SIGPIPE);
#ifdef SIGWINCH
(void) sigaddset(&act.sa_mask, SIGWINCH);
#endif
(void) sigaction(SIGALRM, &act, (struct sigaction *) NULL);
(void) sigaction(SIGPIPE, &act, (struct sigaction *) NULL);
#ifdef SIGWINCH
(void) sigaction(SIGWINCH, &act, (struct sigaction *) NULL);
#endif
}
/*
* deliver_it
*
* Attempt to send a sequence of bytes to the connection.
* Returns
* < 0 Some fatal error occurred, (but not EWOULDBLOCK).
* his return is a request to close the socket and clean up the link.
* >= 0 No real error occurred, returns the number of bytes actually
* transferred. EWOULDBLOCK and other similar conditions should be mapped
* to zero return.
* Upper level routine will have to decide what to do with
* those unwritten bytes...
* *NOTE* alarm calls have been preserved, so this should work equally
* well whether blocking or non-blocking mode is used...
*/
#ifdef WRITEV_IOV
int deliver_it(aClient *cptr, struct iovec *iov, int len)
#else
int deliver_it(aClient *cptr, char *str, int len)
#endif
{
int retval;
aListener *lptr = cptr->lstn;
#ifdef DEBUGMODE
writecalls++;
#endif
#ifdef WRITEV_IOV
#ifdef USE_SSL
if(IsSSL(cptr) && cptr->ssl)
retval = safe_ssl_write(cptr, iov->iov_base, iov->iov_len);
else
#endif
retval = writev(cptr->fd, iov, len);
#else
#ifdef USE_SSL
if(IsSSL(cptr) && cptr->ssl)
retval = safe_ssl_write(cptr, str, len);
else
#endif
retval = send(cptr->fd, str, len, 0);
#endif
/*
* Convert WOULDBLOCK to a return of "0 bytes moved". This
* should occur only if socket was non-blocking. Note, that all is
* Ok, if the 'write' just returns '0' instead of an error and
* errno=EWOULDBLOCK.
*/
if (retval < 0 && (errno == EWOULDBLOCK || errno == EAGAIN ||
errno == ENOBUFS))
{
retval = 0;
cptr->flags |= FLAGS_BLOCKED;
set_fd_flags(cptr->fd, FDF_WANTWRITE);
return (retval); /* Just get out now... */
}
else if (retval > 0)
{
if(cptr->flags & FLAGS_BLOCKED)
{
cptr->flags &= ~FLAGS_BLOCKED;
unset_fd_flags(cptr->fd, FDF_WANTWRITE);
}
}
#ifdef DEBUGMODE
if (retval < 0)
{
writeb[0]++;
Debug((DEBUG_ERROR, "write error (%s) to %s",
sys_errlist[errno], cptr->name));
}
else if (retval == 0)
writeb[1]++;
else if (retval < 16)
writeb[2]++;
else if (retval < 32)
writeb[3]++;
else if (retval < 64)
writeb[4]++;
else if (retval < 128)
writeb[5]++;
else if (retval < 256)
writeb[6]++;
else if (retval < 512)
writeb[7]++;
else if (retval < 1024)
writeb[8]++;
else
writeb[9]++;
#endif
if (retval > 0)
{
cptr->sendB += retval;
if (cptr->sendB & 0x0400)
{
cptr->sendK += (cptr->sendB >> 10);
cptr->sendB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
}
me.sendB += retval;
if (me.sendB & 0x0400)
{
me.sendK += (me.sendB >> 10);
me.sendB &= 0x03ff;
}
if (lptr)
{
lptr->sendB += retval;
if (lptr->sendB & 0x0400)
{
lptr->sendK += (lptr->sendB >> 10);
lptr->sendB &= 0x03ff;
}
}
}
return (retval);
}