Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / net / third_party / nss / ssl / ssldef.c
bloba6613d94c29a8d8e0cd9d3e10f6b674af35d627a
1 /*
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 $ */
9 #include "cert.h"
10 #include "ssl.h"
11 #include "sslimpl.h"
13 #if defined(WIN32)
14 #define MAP_ERROR(from,to) if (err == from) { PORT_SetError(to); }
15 #define DEFINE_ERROR PRErrorCode err = PR_GetError();
16 #else
17 #define MAP_ERROR(from,to)
18 #define DEFINE_ERROR
19 #endif
21 int ssl_DefConnect(sslSocket *ss, const PRNetAddr *sa)
23 PRFileDesc *lower = ss->fd->lower;
24 int rv;
26 rv = lower->methods->connect(lower, sa, ss->cTimeout);
27 return rv;
30 int ssl_DefBind(sslSocket *ss, const PRNetAddr *addr)
32 PRFileDesc *lower = ss->fd->lower;
33 int rv;
35 rv = lower->methods->bind(lower, addr);
36 return rv;
39 int ssl_DefListen(sslSocket *ss, int backlog)
41 PRFileDesc *lower = ss->fd->lower;
42 int rv;
44 rv = lower->methods->listen(lower, backlog);
45 return rv;
48 int ssl_DefShutdown(sslSocket *ss, int how)
50 PRFileDesc *lower = ss->fd->lower;
51 int rv;
53 rv = lower->methods->shutdown(lower, how);
54 return rv;
57 int ssl_DefRecv(sslSocket *ss, unsigned char *buf, int len, int flags)
59 PRFileDesc *lower = ss->fd->lower;
60 int rv;
62 rv = lower->methods->recv(lower, (void *)buf, len, flags, ss->rTimeout);
63 if (rv < 0) {
64 DEFINE_ERROR
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);
69 rv = SECFailure;
71 return rv;
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;
83 int sent = 0;
85 #if NSS_DISABLE_NAGLE_DELAYS
86 /* Although this is overkill, we disable Nagle delays completely for
87 ** SSL sockets.
89 if (ss->opt.useSecurity && !ss->delayDisabled) {
90 ssl_EnableNagleDelay(ss, PR_FALSE); /* ignore error */
91 ss->delayDisabled = 1;
93 #endif
94 do {
95 int rv = lower->methods->send(lower, (const void *)(buf + sent),
96 len - sent, flags, ss->wTimeout);
97 if (rv < 0) {
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)
105 /* Loser */
106 return rv;
108 sent += rv;
110 if (IS_DTLS(ss) && (len > sent)) {
111 /* We got a partial write so just return it */
112 return sent;
114 } while (len > sent);
115 ss->lastWriteBlocked = 0;
116 return sent;
119 int ssl_DefRead(sslSocket *ss, unsigned char *buf, int len)
121 PRFileDesc *lower = ss->fd->lower;
122 int rv;
124 rv = lower->methods->read(lower, (void *)buf, len);
125 if (rv < 0) {
126 DEFINE_ERROR
127 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR)
129 return rv;
132 int ssl_DefWrite(sslSocket *ss, const unsigned char *buf, int len)
134 PRFileDesc *lower = ss->fd->lower;
135 int sent = 0;
137 do {
138 int rv = lower->methods->write(lower, (const void *)(buf + sent),
139 len - sent);
140 if (rv < 0) {
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)
148 /* Loser */
149 return rv;
151 sent += rv;
152 } while (len > sent);
153 ss->lastWriteBlocked = 0;
154 return sent;
157 int ssl_DefGetpeername(sslSocket *ss, PRNetAddr *name)
159 PRFileDesc *lower = ss->fd->lower;
160 int rv;
162 rv = lower->methods->getpeername(lower, name);
163 return rv;
166 int ssl_DefGetsockname(sslSocket *ss, PRNetAddr *name)
168 PRFileDesc *lower = ss->fd->lower;
169 int rv;
171 rv = lower->methods->getsockname(lower, name);
172 return rv;
175 int ssl_DefClose(sslSocket *ss)
177 PRFileDesc *fd;
178 PRFileDesc *popped;
179 int rv;
181 fd = ss->fd;
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);
188 if (fd->higher) {
189 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR);
190 return SECFailure;
192 ss->fd = NULL;
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);
206 ssl_FreeSocket(ss);
208 SSL_TRC(5, ("%d: SSL[%d]: closing, rv=%d errno=%d",
209 SSL_GETPID(), fd, rv, PORT_GetError()));
210 return rv;