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/. */
13 #define MAP_ERROR(from,to) if (err == from) { PORT_SetError(to); }
14 #define DEFINE_ERROR PRErrorCode err = PR_GetError();
16 #define MAP_ERROR(from,to)
20 int ssl_DefConnect(sslSocket *ss, const PRNetAddr *sa)
22 PRFileDesc *lower = ss->fd->lower;
25 rv = lower->methods->connect(lower, sa, ss->cTimeout);
29 int ssl_DefBind(sslSocket *ss, const PRNetAddr *addr)
31 PRFileDesc *lower = ss->fd->lower;
34 rv = lower->methods->bind(lower, addr);
38 int ssl_DefListen(sslSocket *ss, int backlog)
40 PRFileDesc *lower = ss->fd->lower;
43 rv = lower->methods->listen(lower, backlog);
47 int ssl_DefShutdown(sslSocket *ss, int how)
49 PRFileDesc *lower = ss->fd->lower;
52 rv = lower->methods->shutdown(lower, how);
56 int ssl_DefRecv(sslSocket *ss, unsigned char *buf, int len, int flags)
58 PRFileDesc *lower = ss->fd->lower;
61 rv = lower->methods->recv(lower, (void *)buf, len, flags, ss->rTimeout);
64 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR)
65 } else if (rv > len) {
66 PORT_Assert(rv <= len);
67 PORT_SetError(PR_BUFFER_OVERFLOW_ERROR);
73 /* Default (unencrypted) send.
74 * For blocking sockets, always returns len or SECFailure, no short writes.
75 * For non-blocking sockets:
76 * Returns positive count if any data was written, else returns SECFailure.
77 * Short writes may occur. Does not return SECWouldBlock.
79 int ssl_DefSend(sslSocket *ss, const unsigned char *buf, int len, int flags)
81 PRFileDesc *lower = ss->fd->lower;
84 #if NSS_DISABLE_NAGLE_DELAYS
85 /* Although this is overkill, we disable Nagle delays completely for
88 if (ss->opt.useSecurity && !ss->delayDisabled) {
89 ssl_EnableNagleDelay(ss, PR_FALSE); /* ignore error */
90 ss->delayDisabled = 1;
94 int rv = lower->methods->send(lower, (const void *)(buf + sent),
95 len - sent, flags, ss->wTimeout);
97 PRErrorCode err = PR_GetError();
98 if (err == PR_WOULD_BLOCK_ERROR) {
99 ss->lastWriteBlocked = 1;
100 return sent ? sent : SECFailure;
102 ss->lastWriteBlocked = 0;
103 MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR)
109 if (IS_DTLS(ss) && (len > sent)) {
110 /* We got a partial write so just return it */
113 } while (len > sent);
114 ss->lastWriteBlocked = 0;
118 int ssl_DefRead(sslSocket *ss, unsigned char *buf, int len)
120 PRFileDesc *lower = ss->fd->lower;
123 rv = lower->methods->read(lower, (void *)buf, len);
126 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR)
131 int ssl_DefWrite(sslSocket *ss, const unsigned char *buf, int len)
133 PRFileDesc *lower = ss->fd->lower;
137 int rv = lower->methods->write(lower, (const void *)(buf + sent),
140 PRErrorCode err = PR_GetError();
141 if (err == PR_WOULD_BLOCK_ERROR) {
142 ss->lastWriteBlocked = 1;
143 return sent ? sent : SECFailure;
145 ss->lastWriteBlocked = 0;
146 MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR)
151 } while (len > sent);
152 ss->lastWriteBlocked = 0;
156 int ssl_DefGetpeername(sslSocket *ss, PRNetAddr *name)
158 PRFileDesc *lower = ss->fd->lower;
161 rv = lower->methods->getpeername(lower, name);
165 int ssl_DefGetsockname(sslSocket *ss, PRNetAddr *name)
167 PRFileDesc *lower = ss->fd->lower;
170 rv = lower->methods->getsockname(lower, name);
174 int ssl_DefClose(sslSocket *ss)
182 /* First, remove the SSL layer PRFileDesc from the socket's stack,
183 ** then invoke the SSL layer's PRFileDesc destructor.
184 ** This must happen before the next layer down is closed.
186 PORT_Assert(fd->higher == NULL);
188 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR);
193 /* PR_PopIOLayer will swap the contents of the top two PRFileDescs on
194 ** the stack, and then remove the second one. This way, the address
195 ** of the PRFileDesc on the top of the stack doesn't change.
197 popped = PR_PopIOLayer(fd, PR_TOP_IO_LAYER);
198 popped->dtor(popped);
200 /* fd is now the PRFileDesc for the next layer down.
201 ** Now close the underlying socket.
203 rv = fd->methods->close(fd);
207 SSL_TRC(5, ("%d: SSL[%d]: closing, rv=%d errno=%d",
208 SSL_GETPID(), fd, rv, PORT_GetError()));