1 /*
2  *   Unreal Internet Relay Chat Daemon, src/socket.c
3  *   Copyright (C) 1990 Jarkko Oikarinen and
4  *                      University of Oulu, Computing Center
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 1, or (at your option)
9  *   any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include "struct.h"
22 #include "common.h"
23 #include "sys.h"
24 #include "h.h"
25 #include <signal.h>
26 #include "inet.h"
27 #ifndef _WIN32
28 extern int errno;		/* ...seems that errno.h doesn't define this everywhere */
29 #endif
30 #include <sys/types.h>
31 #ifndef _WIN32
32 #include <sys/socket.h>
33 #endif
34 #ifdef DEBUGMODE
35 int  writecalls = 0, writeb[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
36 #endif
37 
38 
39 /*
40 ** deliver_it
41 **	Attempt to send a sequence of bytes to the connection.
42 **	Returns
43 **
44 **	< 0	Some fatal error occurred, (but not EWOULDBLOCK).
45 **		This return is a request to close the socket and
46 **		clean up the link.
47 **
48 **	>= 0	No real error occurred, returns the number of
49 **		bytes actually transferred. EWOULDBLOCK and other
50 **		possibly similar conditions should be mapped to
51 **		zero return. Upper level routine will have to
52 **		decide what to do with those unwritten bytes...
53 **
54 **	*NOTE*	alarm calls have been preserved, so this should
55 **		work equally well whether blocking or non-blocking
56 **		mode is used...
57 **
58 **	*NOTE*	I nuked 'em.  At the load of current ircd servers
59 **		you can't run with stuff that blocks. And we don't.
60 */
deliver_it(aClient * cptr,char * str,int len)61 int  deliver_it(aClient *cptr, char *str, int len)
62 {
63 	int  retval;
64 	aClient *acpt = cptr->listener;
65 
66 #ifdef	DEBUGMODE
67 	writecalls++;
68 #endif
69 #ifdef VMS
70 	retval = netwrite(cptr->fd, str, len);
71 #else
72 	if (IsDead(cptr) || (!IsServer(cptr) && !IsPerson(cptr)
73 	    && !IsHandshake(cptr)
74 #ifdef USE_SSL
75 	    && !IsSSLHandshake(cptr)
76 #endif
77 
78 	    && !IsUnknown(cptr)))
79 	{
80 		str[len] = '\0';
81 		sendto_ops
82 		    ("* * * DEBUG ERROR * * * !!! Calling deliver_it() for %s, status %d %s, with message: %s",
83 		    cptr->name, cptr->status, IsDead(cptr) ? "DEAD" : "", str);
84 		return -1;
85 	}
86 
87 #ifdef USE_SSL
88 	if (cptr->flags & FLAGS_SSL)
89 		 retval = ircd_SSL_write(cptr, str, len);
90 	else
91 #endif
92 		retval = send(cptr->fd, str, len, 0);
93 	/*
94 	   ** Convert WOULDBLOCK to a return of "0 bytes moved". This
95 	   ** should occur only if socket was non-blocking. Note, that
96 	   ** all is Ok, if the 'write' just returns '0' instead of an
97 	   ** error and errno=EWOULDBLOCK.
98 	   **
99 	   ** ...now, would this work on VMS too? --msa
100 	 */
101 # ifndef _WIN32
102 	if (retval < 0 && (errno == EWOULDBLOCK || errno == EAGAIN ||
103 	    errno == ENOBUFS))
104 # else
105 		if (retval < 0 && (WSAGetLastError() == WSAEWOULDBLOCK ||
106 		    WSAGetLastError() == WSAENOBUFS))
107 # endif
108 		{
109 			retval = 0;
110 			SetBlocked(cptr);
111 		}
112 		else if (retval > 0)
113 		{
114 			ClearBlocked(cptr);
115 		}
116 
117 #endif
118 #ifdef DEBUGMODE
119 	if (retval < 0)
120 	{
121 		writeb[0]++;
122 		Debug((DEBUG_ERROR, "write error (%s) to %s", STRERROR(ERRNO), cptr->name));
123 
124 	}
125 	else if (retval == 0)
126 		writeb[1]++;
127 	else if (retval < 16)
128 		writeb[2]++;
129 	else if (retval < 32)
130 		writeb[3]++;
131 	else if (retval < 64)
132 		writeb[4]++;
133 	else if (retval < 128)
134 		writeb[5]++;
135 	else if (retval < 256)
136 		writeb[6]++;
137 	else if (retval < 512)
138 		writeb[7]++;
139 	else if (retval < 1024)
140 		writeb[8]++;
141 	else
142 		writeb[9]++;
143 #endif
144 	if (retval > 0)
145 	{
146 		cptr->sendB += retval;
147 		me.sendB += retval;
148 		if (cptr->sendB > 1023)
149 		{
150 			cptr->sendK += (cptr->sendB >> 10);
151 			cptr->sendB &= 0x03ff;	/* 2^10 = 1024, 3ff = 1023 */
152 		}
153 		if (acpt != &me)
154 		{
155 			acpt->sendB += retval;
156 			if (acpt->sendB > 1023)
157 			{
158 				acpt->sendK += (acpt->sendB >> 10);
159 				acpt->sendB &= 0x03ff;
160 			}
161 		}
162 		if (me.sendB > 1023)
163 		{
164 			me.sendK += (me.sendB >> 10);
165 			me.sendB &= 0x03ff;
166 		}
167 	}
168 	return (retval);
169 }
170 
Inet_si2pB(struct SOCKADDR_IN * sin,char * buf,int sz)171 char	*Inet_si2pB(struct SOCKADDR_IN *sin, char *buf, int sz)
172 {
173 #ifdef INET6
174 	u_char	*cp;
175 
176 	/*
177 	  Hack to rerepresent ::ffff:x.x.x.x as x.x.x.x
178 	 */
179 	cp = (u_char *)sin->SIN_ADDR.s6_addr;
180 	if (cp[0] == 0 && cp[1] == 0 && cp[2] == 0 && cp[3] == 0 && cp[4] == 0
181 	    && cp[5] == 0 && cp[6] == 0 && cp[7] == 0 && cp[8] == 0
182 	    && cp[9] == 0 && cp[10] == 0xff
183 	    && cp[11] == 0xff)
184 	{
185 		(void)ircsprintf(buf, "%u.%u.%u.%u",
186 		    (u_int)(cp[12]), (u_int)(cp[13]),
187 		    (u_int)(cp[14]), (u_int)(cp[15]));
188 
189 		return (buf);
190 	}
191 	else
192 		return ((char *)inetntop(AFINET, &sin->SIN_ADDR.s6_addr, buf, sz));
193 #else
194 	return ((char *)inet_ntoa(sin->SIN_ADDR));
195 #endif
196 }
197 
Inet_si2p(struct SOCKADDR_IN * sin)198 char	*Inet_si2p(struct SOCKADDR_IN *sin)
199 {
200 	static char	buf[256];
201 
202 	return (Inet_si2pB(sin, buf, sizeof(buf)));
203 }
204 
205 /*
206   May return NULL if inet_ntop() fails.
207  */
Inet_ia2p(struct IN_ADDR * ia)208 char	*Inet_ia2p(struct IN_ADDR *ia)
209 {
210 #ifndef INET6
211 	return ((char *)inet_ntoa(*ia));
212 #else
213 	/* Hack to make proper addresses */
214 	static char buf[256];
215 	u_char	*cp;
216 
217 	cp = (u_char *)((struct IN_ADDR *)ia)->s6_addr;
218 	if (cp[0] == 0 && cp[1] == 0 && cp[2] == 0 && cp[3] == 0 && cp[4] == 0
219 	    && cp[5] == 0 && cp[6] == 0 && cp[7] == 0 && cp[8] == 0
220 	    && cp[9] == 0 && cp[10] == 0xff
221 	    && cp[11] == 0xff)
222 	{
223 		(void)ircsprintf(buf, "%u.%u.%u.%u",
224 		    (u_int)(cp[12]), (u_int)(cp[13]),
225 		    (u_int)(cp[14]), (u_int)(cp[15]));
226 
227 		return (buf);
228 	}
229 	else
230 		return((char *)inet_ntop(AFINET, ia, buf, sizeof(buf)));
231 #endif
232 }
233 
Inet_ia2pNB(struct IN_ADDR * ia,int compressed)234 char	*Inet_ia2pNB(struct IN_ADDR *ia, int compressed)
235 {
236 #ifndef INET6
237 	return ((char *)inet_ntoa(*ia));
238 #else
239 	static char buf[256];
240 	if (compressed)
241 		return ((char *)inet_ntop(AFINET, ia, buf, sizeof(buf)));
242 	else
243 		return ((char *)inetntop(AFINET, ia, buf, sizeof(buf)));
244 #endif
245 }
246