2 * "Default" SSLSocket methods, used by sockets that do neither SSL nor socks.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* $Id: ssldef.c,v 1.13 2012/04/25 14:50:12 gerv%gerv.net Exp $ */
14 #define MAP_ERROR(from,to) if (err == from) { PORT_SetError(to); }
15 #define DEFINE_ERROR PRErrorCode err = PR_GetError();
17 #define MAP_ERROR(from,to)
21 int ssl_DefConnect(sslSocket
*ss
, const PRNetAddr
*sa
)
23 PRFileDesc
*lower
= ss
->fd
->lower
;
26 rv
= lower
->methods
->connect(lower
, sa
, ss
->cTimeout
);
30 int ssl_DefBind(sslSocket
*ss
, const PRNetAddr
*addr
)
32 PRFileDesc
*lower
= ss
->fd
->lower
;
35 rv
= lower
->methods
->bind(lower
, addr
);
39 int ssl_DefListen(sslSocket
*ss
, int backlog
)
41 PRFileDesc
*lower
= ss
->fd
->lower
;
44 rv
= lower
->methods
->listen(lower
, backlog
);
48 int ssl_DefShutdown(sslSocket
*ss
, int how
)
50 PRFileDesc
*lower
= ss
->fd
->lower
;
53 rv
= lower
->methods
->shutdown(lower
, how
);
57 int ssl_DefRecv(sslSocket
*ss
, unsigned char *buf
, int len
, int flags
)
59 PRFileDesc
*lower
= ss
->fd
->lower
;
62 rv
= lower
->methods
->recv(lower
, (void *)buf
, len
, flags
, ss
->rTimeout
);
65 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR
, PR_CONNECT_RESET_ERROR
)
66 } else if (rv
> len
) {
67 PORT_Assert(rv
<= len
);
68 PORT_SetError(PR_BUFFER_OVERFLOW_ERROR
);
74 /* Default (unencrypted) send.
75 * For blocking sockets, always returns len or SECFailure, no short writes.
76 * For non-blocking sockets:
77 * Returns positive count if any data was written, else returns SECFailure.
78 * Short writes may occur. Does not return SECWouldBlock.
80 int ssl_DefSend(sslSocket
*ss
, const unsigned char *buf
, int len
, int flags
)
82 PRFileDesc
*lower
= ss
->fd
->lower
;
85 #if NSS_DISABLE_NAGLE_DELAYS
86 /* Although this is overkill, we disable Nagle delays completely for
89 if (ss
->opt
.useSecurity
&& !ss
->delayDisabled
) {
90 ssl_EnableNagleDelay(ss
, PR_FALSE
); /* ignore error */
91 ss
->delayDisabled
= 1;
95 int rv
= lower
->methods
->send(lower
, (const void *)(buf
+ sent
),
96 len
- sent
, flags
, ss
->wTimeout
);
98 PRErrorCode err
= PR_GetError();
99 if (err
== PR_WOULD_BLOCK_ERROR
) {
100 ss
->lastWriteBlocked
= 1;
101 return sent
? sent
: SECFailure
;
103 ss
->lastWriteBlocked
= 0;
104 MAP_ERROR(PR_CONNECT_ABORTED_ERROR
, PR_CONNECT_RESET_ERROR
)
110 if (IS_DTLS(ss
) && (len
> sent
)) {
111 /* We got a partial write so just return it */
114 } while (len
> sent
);
115 ss
->lastWriteBlocked
= 0;
119 int ssl_DefRead(sslSocket
*ss
, unsigned char *buf
, int len
)
121 PRFileDesc
*lower
= ss
->fd
->lower
;
124 rv
= lower
->methods
->read(lower
, (void *)buf
, len
);
127 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR
, PR_CONNECT_RESET_ERROR
)
132 int ssl_DefWrite(sslSocket
*ss
, const unsigned char *buf
, int len
)
134 PRFileDesc
*lower
= ss
->fd
->lower
;
138 int rv
= lower
->methods
->write(lower
, (const void *)(buf
+ sent
),
141 PRErrorCode err
= PR_GetError();
142 if (err
== PR_WOULD_BLOCK_ERROR
) {
143 ss
->lastWriteBlocked
= 1;
144 return sent
? sent
: SECFailure
;
146 ss
->lastWriteBlocked
= 0;
147 MAP_ERROR(PR_CONNECT_ABORTED_ERROR
, PR_CONNECT_RESET_ERROR
)
152 } while (len
> sent
);
153 ss
->lastWriteBlocked
= 0;
157 int ssl_DefGetpeername(sslSocket
*ss
, PRNetAddr
*name
)
159 PRFileDesc
*lower
= ss
->fd
->lower
;
162 rv
= lower
->methods
->getpeername(lower
, name
);
166 int ssl_DefGetsockname(sslSocket
*ss
, PRNetAddr
*name
)
168 PRFileDesc
*lower
= ss
->fd
->lower
;
171 rv
= lower
->methods
->getsockname(lower
, name
);
175 int ssl_DefClose(sslSocket
*ss
)
183 /* First, remove the SSL layer PRFileDesc from the socket's stack,
184 ** then invoke the SSL layer's PRFileDesc destructor.
185 ** This must happen before the next layer down is closed.
187 PORT_Assert(fd
->higher
== NULL
);
189 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR
);
194 /* PR_PopIOLayer will swap the contents of the top two PRFileDescs on
195 ** the stack, and then remove the second one. This way, the address
196 ** of the PRFileDesc on the top of the stack doesn't change.
198 popped
= PR_PopIOLayer(fd
, PR_TOP_IO_LAYER
);
199 popped
->dtor(popped
);
201 /* fd is now the PRFileDesc for the next layer down.
202 ** Now close the underlying socket.
204 rv
= fd
->methods
->close(fd
);
208 SSL_TRC(5, ("%d: SSL[%d]: closing, rv=%d errno=%d",
209 SSL_GETPID(), fd
, rv
, PORT_GetError()));