MCPcopy Create free account
hub / github.com/XTLS/Go / Write

Method Write

conn.go:1348–1415  ·  view source on GitHub ↗

Write writes data to the connection. As Write calls Handshake, in order to prevent indefinite blocking a deadline must be set for both Read and Write before Write is called when the handshake has not yet completed. See SetDeadline, SetReadDeadline, and SetWriteDeadline.

(b []byte)

Source from the content-addressed store, hash-verified

1346// has not yet completed. See SetDeadline, SetReadDeadline, and
1347// SetWriteDeadline.
1348func (c *Conn) Write(b []byte) (int, error) {
1349 if c.DirectOut {
1350 if s := len(b) - 31; s >= 0 && b[s] == 21 {
1351 if b[s+1] == 3 && b[s+2] == 3 && b[s+3] == 0 && b[s+4] == 26 {
1352 if c.SHOW {
1353 fmt.Println(c.MARK, "discarded 21 3 3 0 26 at s =", s)
1354 }
1355 c.conn.Write(b[:s])
1356 return s + 31, nil
1357 }
1358 }
1359 return c.conn.Write(b)
1360 }
1361
1362 // interlock with Close below
1363 for {
1364 x := atomic.LoadInt32(&c.activeCall)
1365 if x&1 != 0 {
1366 return 0, net.ErrClosed
1367 }
1368 if atomic.CompareAndSwapInt32(&c.activeCall, x, x+2) {
1369 break
1370 }
1371 }
1372 defer atomic.AddInt32(&c.activeCall, -2)
1373
1374 if err := c.Handshake(); err != nil {
1375 return 0, err
1376 }
1377
1378 c.out.Lock()
1379 defer c.out.Unlock()
1380
1381 if err := c.out.err; err != nil {
1382 return 0, err
1383 }
1384
1385 if !c.handshakeComplete() {
1386 return 0, alertInternalError
1387 }
1388
1389 if c.closeNotifySent {
1390 return 0, errShutdown
1391 }
1392
1393 // TLS 1.0 is susceptible to a chosen-plaintext
1394 // attack when using block mode ciphers due to predictable IVs.
1395 // This can be prevented by splitting each Application Data
1396 // record into two records, effectively randomizing the IV.
1397 //
1398 // https://www.openssl.org/~bodo/tls-cbc.txt
1399 // https://bugzilla.mozilla.org/show_bug.cgi?id=665814
1400 // https://www.imperialviolet.org/2012/01/15/beastfollowup.html
1401
1402 var m int
1403 if len(b) > 1 && c.vers == VersionTLS10 {
1404 if _, ok := c.out.cipher.(cipher.BlockMode); ok {
1405 n, err := c.writeRecordLocked(recordTypeApplicationData, b[:1])

Callers 15

writeKeyLogMethod · 0.45
sha1HashFunction · 0.45
md5SHA1HashFunction · 0.45
hashForServerKeyExchangeFunction · 0.45
readRecordOrCCSMethod · 0.45
writeMethod · 0.45
flushMethod · 0.45
loadSessionMethod · 0.45
handshakeMethod · 0.45
doFullHandshakeMethod · 0.45
readFinishedMethod · 0.45
readSessionTicketMethod · 0.45

Calls 4

HandshakeMethod · 0.95
handshakeCompleteMethod · 0.95
writeRecordLockedMethod · 0.95
setErrorLockedMethod · 0.80

Tested by

no test coverage detected