2 * "Default" SSLSocket methods, used by sockets that do neither SSL nor socks.
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is the Netscape security libraries.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1994-2000
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
39 /* $Id: ssldef.c,v 1.11 2006/04/20 08:46:34 nelson%bolyard.com Exp $ */
46 #define MAP_ERROR(from,to) if (err == from) { PORT_SetError(to); }
47 #define DEFINE_ERROR PRErrorCode err = PR_GetError();
49 #define MAP_ERROR(from,to)
53 int ssl_DefConnect(sslSocket
*ss
, const PRNetAddr
*sa
)
55 PRFileDesc
*lower
= ss
->fd
->lower
;
58 rv
= lower
->methods
->connect(lower
, sa
, ss
->cTimeout
);
62 int ssl_DefBind(sslSocket
*ss
, const PRNetAddr
*addr
)
64 PRFileDesc
*lower
= ss
->fd
->lower
;
67 rv
= lower
->methods
->bind(lower
, addr
);
71 int ssl_DefListen(sslSocket
*ss
, int backlog
)
73 PRFileDesc
*lower
= ss
->fd
->lower
;
76 rv
= lower
->methods
->listen(lower
, backlog
);
80 int ssl_DefShutdown(sslSocket
*ss
, int how
)
82 PRFileDesc
*lower
= ss
->fd
->lower
;
85 rv
= lower
->methods
->shutdown(lower
, how
);
89 int ssl_DefRecv(sslSocket
*ss
, unsigned char *buf
, int len
, int flags
)
91 PRFileDesc
*lower
= ss
->fd
->lower
;
94 rv
= lower
->methods
->recv(lower
, (void *)buf
, len
, flags
, ss
->rTimeout
);
97 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR
, PR_CONNECT_RESET_ERROR
)
98 } else if (rv
> len
) {
99 PORT_Assert(rv
<= len
);
100 PORT_SetError(PR_BUFFER_OVERFLOW_ERROR
);
106 /* Default (unencrypted) send.
107 * For blocking sockets, always returns len or SECFailure, no short writes.
108 * For non-blocking sockets:
109 * Returns positive count if any data was written, else returns SECFailure.
110 * Short writes may occur. Does not return SECWouldBlock.
112 int ssl_DefSend(sslSocket
*ss
, const unsigned char *buf
, int len
, int flags
)
114 PRFileDesc
*lower
= ss
->fd
->lower
;
117 #if NSS_DISABLE_NAGLE_DELAYS
118 /* Although this is overkill, we disable Nagle delays completely for
121 if (ss
->opt
.useSecurity
&& !ss
->delayDisabled
) {
122 ssl_EnableNagleDelay(ss
, PR_FALSE
); /* ignore error */
123 ss
->delayDisabled
= 1;
127 int rv
= lower
->methods
->send(lower
, (const void *)(buf
+ sent
),
128 len
- sent
, flags
, ss
->wTimeout
);
130 PRErrorCode err
= PR_GetError();
131 if (err
== PR_WOULD_BLOCK_ERROR
) {
132 ss
->lastWriteBlocked
= 1;
133 return sent
? sent
: SECFailure
;
135 ss
->lastWriteBlocked
= 0;
136 MAP_ERROR(PR_CONNECT_ABORTED_ERROR
, PR_CONNECT_RESET_ERROR
)
141 } while (len
> sent
);
142 ss
->lastWriteBlocked
= 0;
146 int ssl_DefRead(sslSocket
*ss
, unsigned char *buf
, int len
)
148 PRFileDesc
*lower
= ss
->fd
->lower
;
151 rv
= lower
->methods
->read(lower
, (void *)buf
, len
);
154 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR
, PR_CONNECT_RESET_ERROR
)
159 int ssl_DefWrite(sslSocket
*ss
, const unsigned char *buf
, int len
)
161 PRFileDesc
*lower
= ss
->fd
->lower
;
165 int rv
= lower
->methods
->write(lower
, (const void *)(buf
+ sent
),
168 PRErrorCode err
= PR_GetError();
169 if (err
== PR_WOULD_BLOCK_ERROR
) {
170 ss
->lastWriteBlocked
= 1;
171 return sent
? sent
: SECFailure
;
173 ss
->lastWriteBlocked
= 0;
174 MAP_ERROR(PR_CONNECT_ABORTED_ERROR
, PR_CONNECT_RESET_ERROR
)
179 } while (len
> sent
);
180 ss
->lastWriteBlocked
= 0;
184 int ssl_DefGetpeername(sslSocket
*ss
, PRNetAddr
*name
)
186 PRFileDesc
*lower
= ss
->fd
->lower
;
189 rv
= lower
->methods
->getpeername(lower
, name
);
193 int ssl_DefGetsockname(sslSocket
*ss
, PRNetAddr
*name
)
195 PRFileDesc
*lower
= ss
->fd
->lower
;
198 rv
= lower
->methods
->getsockname(lower
, name
);
202 int ssl_DefClose(sslSocket
*ss
)
210 /* First, remove the SSL layer PRFileDesc from the socket's stack,
211 ** then invoke the SSL layer's PRFileDesc destructor.
212 ** This must happen before the next layer down is closed.
214 PORT_Assert(fd
->higher
== NULL
);
216 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR
);
221 /* PR_PopIOLayer will swap the contents of the top two PRFileDescs on
222 ** the stack, and then remove the second one. This way, the address
223 ** of the PRFileDesc on the top of the stack doesn't change.
225 popped
= PR_PopIOLayer(fd
, PR_TOP_IO_LAYER
);
226 popped
->dtor(popped
);
228 /* fd is now the PRFileDesc for the next layer down.
229 ** Now close the underlying socket.
231 rv
= fd
->methods
->close(fd
);
235 SSL_TRC(5, ("%d: SSL[%d]: closing, rv=%d errno=%d",
236 SSL_GETPID(), fd
, rv
, PORT_GetError()));