| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * "Default" SSLSocket methods, used by sockets that do neither SSL nor socks. | |
| 3 * | |
| 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 | |
| 8 #include "cert.h" | |
| 9 #include "ssl.h" | |
| 10 #include "sslimpl.h" | |
| 11 | |
| 12 #if defined(WIN32) | |
| 13 #define MAP_ERROR(from, to) \ | |
| 14 if (err == from) { \ | |
| 15 PORT_SetError(to); \ | |
| 16 } | |
| 17 #define DEFINE_ERROR PRErrorCode err = PR_GetError(); | |
| 18 #else | |
| 19 #define MAP_ERROR(from, to) | |
| 20 #define DEFINE_ERROR | |
| 21 #endif | |
| 22 | |
| 23 int | |
| 24 ssl_DefConnect(sslSocket *ss, const PRNetAddr *sa) | |
| 25 { | |
| 26 PRFileDesc *lower = ss->fd->lower; | |
| 27 int rv; | |
| 28 | |
| 29 rv = lower->methods->connect(lower, sa, ss->cTimeout); | |
| 30 return rv; | |
| 31 } | |
| 32 | |
| 33 int | |
| 34 ssl_DefBind(sslSocket *ss, const PRNetAddr *addr) | |
| 35 { | |
| 36 PRFileDesc *lower = ss->fd->lower; | |
| 37 int rv; | |
| 38 | |
| 39 rv = lower->methods->bind(lower, addr); | |
| 40 return rv; | |
| 41 } | |
| 42 | |
| 43 int | |
| 44 ssl_DefListen(sslSocket *ss, int backlog) | |
| 45 { | |
| 46 PRFileDesc *lower = ss->fd->lower; | |
| 47 int rv; | |
| 48 | |
| 49 rv = lower->methods->listen(lower, backlog); | |
| 50 return rv; | |
| 51 } | |
| 52 | |
| 53 int | |
| 54 ssl_DefShutdown(sslSocket *ss, int how) | |
| 55 { | |
| 56 PRFileDesc *lower = ss->fd->lower; | |
| 57 int rv; | |
| 58 | |
| 59 rv = lower->methods->shutdown(lower, how); | |
| 60 return rv; | |
| 61 } | |
| 62 | |
| 63 int | |
| 64 ssl_DefRecv(sslSocket *ss, unsigned char *buf, int len, int flags) | |
| 65 { | |
| 66 PRFileDesc *lower = ss->fd->lower; | |
| 67 int rv; | |
| 68 | |
| 69 rv = lower->methods->recv(lower, (void *)buf, len, flags, ss->rTimeout); | |
| 70 if (rv < 0) { | |
| 71 DEFINE_ERROR | |
| 72 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR) | |
| 73 } else if (rv > len) { | |
| 74 PORT_Assert(rv <= len); | |
| 75 PORT_SetError(PR_BUFFER_OVERFLOW_ERROR); | |
| 76 rv = SECFailure; | |
| 77 } | |
| 78 return rv; | |
| 79 } | |
| 80 | |
| 81 /* Default (unencrypted) send. | |
| 82 * For blocking sockets, always returns len or SECFailure, no short writes. | |
| 83 * For non-blocking sockets: | |
| 84 * Returns positive count if any data was written, else returns SECFailure. | |
| 85 * Short writes may occur. Does not return SECWouldBlock. | |
| 86 */ | |
| 87 int | |
| 88 ssl_DefSend(sslSocket *ss, const unsigned char *buf, int len, int flags) | |
| 89 { | |
| 90 PRFileDesc *lower = ss->fd->lower; | |
| 91 int sent = 0; | |
| 92 | |
| 93 #if NSS_DISABLE_NAGLE_DELAYS | |
| 94 /* Although this is overkill, we disable Nagle delays completely for | |
| 95 ** SSL sockets. | |
| 96 */ | |
| 97 if (ss->opt.useSecurity && !ss->delayDisabled) { | |
| 98 ssl_EnableNagleDelay(ss, PR_FALSE); /* ignore error */ | |
| 99 ss->delayDisabled = 1; | |
| 100 } | |
| 101 #endif | |
| 102 do { | |
| 103 int rv = lower->methods->send(lower, (const void *)(buf + sent), | |
| 104 len - sent, flags, ss->wTimeout); | |
| 105 if (rv < 0) { | |
| 106 PRErrorCode err = PR_GetError(); | |
| 107 if (err == PR_WOULD_BLOCK_ERROR) { | |
| 108 ss->lastWriteBlocked = 1; | |
| 109 return sent ? sent : SECFailure; | |
| 110 } | |
| 111 ss->lastWriteBlocked = 0; | |
| 112 MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR) | |
| 113 /* Loser */ | |
| 114 return rv; | |
| 115 } | |
| 116 sent += rv; | |
| 117 | |
| 118 if (IS_DTLS(ss) && (len > sent)) { | |
| 119 /* We got a partial write so just return it */ | |
| 120 return sent; | |
| 121 } | |
| 122 } while (len > sent); | |
| 123 ss->lastWriteBlocked = 0; | |
| 124 return sent; | |
| 125 } | |
| 126 | |
| 127 int | |
| 128 ssl_DefRead(sslSocket *ss, unsigned char *buf, int len) | |
| 129 { | |
| 130 PRFileDesc *lower = ss->fd->lower; | |
| 131 int rv; | |
| 132 | |
| 133 rv = lower->methods->read(lower, (void *)buf, len); | |
| 134 if (rv < 0) { | |
| 135 DEFINE_ERROR | |
| 136 MAP_ERROR(PR_SOCKET_SHUTDOWN_ERROR, PR_CONNECT_RESET_ERROR) | |
| 137 } | |
| 138 return rv; | |
| 139 } | |
| 140 | |
| 141 int | |
| 142 ssl_DefWrite(sslSocket *ss, const unsigned char *buf, int len) | |
| 143 { | |
| 144 PRFileDesc *lower = ss->fd->lower; | |
| 145 int sent = 0; | |
| 146 | |
| 147 do { | |
| 148 int rv = lower->methods->write(lower, (const void *)(buf + sent), | |
| 149 len - sent); | |
| 150 if (rv < 0) { | |
| 151 PRErrorCode err = PR_GetError(); | |
| 152 if (err == PR_WOULD_BLOCK_ERROR) { | |
| 153 ss->lastWriteBlocked = 1; | |
| 154 return sent ? sent : SECFailure; | |
| 155 } | |
| 156 ss->lastWriteBlocked = 0; | |
| 157 MAP_ERROR(PR_CONNECT_ABORTED_ERROR, PR_CONNECT_RESET_ERROR) | |
| 158 /* Loser */ | |
| 159 return rv; | |
| 160 } | |
| 161 sent += rv; | |
| 162 } while (len > sent); | |
| 163 ss->lastWriteBlocked = 0; | |
| 164 return sent; | |
| 165 } | |
| 166 | |
| 167 int | |
| 168 ssl_DefGetpeername(sslSocket *ss, PRNetAddr *name) | |
| 169 { | |
| 170 PRFileDesc *lower = ss->fd->lower; | |
| 171 int rv; | |
| 172 | |
| 173 rv = lower->methods->getpeername(lower, name); | |
| 174 return rv; | |
| 175 } | |
| 176 | |
| 177 int | |
| 178 ssl_DefGetsockname(sslSocket *ss, PRNetAddr *name) | |
| 179 { | |
| 180 PRFileDesc *lower = ss->fd->lower; | |
| 181 int rv; | |
| 182 | |
| 183 rv = lower->methods->getsockname(lower, name); | |
| 184 return rv; | |
| 185 } | |
| 186 | |
| 187 int | |
| 188 ssl_DefClose(sslSocket *ss) | |
| 189 { | |
| 190 PRFileDesc *fd; | |
| 191 PRFileDesc *popped; | |
| 192 int rv; | |
| 193 | |
| 194 fd = ss->fd; | |
| 195 | |
| 196 /* First, remove the SSL layer PRFileDesc from the socket's stack, | |
| 197 ** then invoke the SSL layer's PRFileDesc destructor. | |
| 198 ** This must happen before the next layer down is closed. | |
| 199 */ | |
| 200 PORT_Assert(fd->higher == NULL); | |
| 201 if (fd->higher) { | |
| 202 PORT_SetError(PR_BAD_DESCRIPTOR_ERROR); | |
| 203 return SECFailure; | |
| 204 } | |
| 205 ss->fd = NULL; | |
| 206 | |
| 207 /* PR_PopIOLayer will swap the contents of the top two PRFileDescs on | |
| 208 ** the stack, and then remove the second one. This way, the address | |
| 209 ** of the PRFileDesc on the top of the stack doesn't change. | |
| 210 */ | |
| 211 popped = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); | |
| 212 popped->dtor(popped); | |
| 213 | |
| 214 /* fd is now the PRFileDesc for the next layer down. | |
| 215 ** Now close the underlying socket. | |
| 216 */ | |
| 217 rv = fd->methods->close(fd); | |
| 218 | |
| 219 ssl_FreeSocket(ss); | |
| 220 | |
| 221 SSL_TRC(5, ("%d: SSL[%d]: closing, rv=%d errno=%d", | |
| 222 SSL_GETPID(), fd, rv, PORT_GetError())); | |
| 223 return rv; | |
| 224 } | |
| OLD | NEW |