@@ -325,7 +325,7 @@ unsafe fn accept_impl(
325
325
) -> libc:: c_int {
326
326
let result = HostNetworkState :: with_socket (
327
327
sock_fd,
328
- |socket| -> Result < SocketAddr , ( libc:: c_int , libc:: c_int ) > {
328
+ |socket| -> Result < ( SocketAddr , libc :: c_int ) , ( libc:: c_int , libc:: c_int ) > {
329
329
let node = plugin:: node ( ) ;
330
330
let net = plugin:: simulator :: < NetSim > ( ) ;
331
331
let network = net. network . lock ( ) . unwrap ( ) ;
@@ -343,7 +343,8 @@ unsafe fn accept_impl(
343
343
// We can't simulate blocking accept in a single-threaded simulator, so if there is no
344
344
// connection waiting for us, just bail.
345
345
network
346
- . accept_connect ( node, endpoint. addr )
346
+ . accept_connect ( socket. ty , node, endpoint. addr )
347
+ . map ( |addr| ( addr, socket. ty ) )
347
348
. ok_or ( ( -1 , libc:: ECONNABORTED ) )
348
349
} ,
349
350
)
@@ -352,18 +353,18 @@ unsafe fn accept_impl(
352
353
Result :: Err ( ( -1 , libc:: ENOTSOCK ) )
353
354
} ) ;
354
355
355
- let remote_addr = match result {
356
+ let ( remote_addr, proto ) = match result {
356
357
Err ( ( ret, err) ) => {
357
358
trace ! ( "error status: {} {}" , ret, err) ;
358
359
set_errno ( err) ;
359
360
return ret;
360
361
}
361
- Ok ( addr ) => addr ,
362
+ Ok ( res ) => res ,
362
363
} ;
363
364
364
365
write_socket_addr ( address, address_len, remote_addr) ;
365
366
366
- let endpoint = Endpoint :: connect_sync ( remote_addr)
367
+ let endpoint = Endpoint :: connect_sync ( proto , remote_addr)
367
368
. expect ( "connection failure should already have been detected" ) ;
368
369
369
370
let fd = alloc_fd ( ) ;
@@ -396,7 +397,7 @@ define_sys_interceptor!(
396
397
397
398
HostNetworkState :: with_socket( sock_fd, |socket| {
398
399
assert!( socket. endpoint. is_none( ) , "socket already bound" ) ;
399
- match Endpoint :: bind_sync( socket_addr) {
400
+ match Endpoint :: bind_sync( socket . ty , socket_addr) {
400
401
Ok ( ep) => {
401
402
socket. endpoint = Some ( Arc :: new( ep) ) ;
402
403
0
@@ -438,7 +439,7 @@ define_sys_interceptor!(
438
439
return Err ( ( -1 , libc:: EISCONN ) ) ;
439
440
}
440
441
441
- let ep = Endpoint :: connect_sync( sock_addr) . map_err( |e| match e. kind( ) {
442
+ let ep = Endpoint :: connect_sync( socket . ty , sock_addr) . map_err( |e| match e. kind( ) {
442
443
io:: ErrorKind :: AddrInUse => ( -1 , libc:: EADDRINUSE ) ,
443
444
io:: ErrorKind :: AddrNotAvailable => ( -1 , libc:: EADDRNOTAVAIL ) ,
444
445
_ => {
@@ -453,7 +454,7 @@ define_sys_interceptor!(
453
454
// the other end goes away).
454
455
let net = plugin:: simulator:: <NetSim >( ) ;
455
456
let network = net. network. lock( ) . unwrap( ) ;
456
- if !network. signal_connect( ep. addr, sock_addr) {
457
+ if !network. signal_connect( socket . ty , ep. addr, sock_addr) {
457
458
return Err ( ( -1 , libc:: ECONNREFUSED ) ) ;
458
459
}
459
460
@@ -544,8 +545,7 @@ define_sys_interceptor!(
544
545
match ( level, name) {
545
546
// called by anemo::Network::start (via socket2)
546
547
// skip returning any value here since Sui only uses it to log an error anyway
547
- ( libc:: SOL_SOCKET , libc:: SO_RCVBUF ) |
548
- ( libc:: SOL_SOCKET , libc:: SO_SNDBUF ) => 0 ,
548
+ ( libc:: SOL_SOCKET , libc:: SO_RCVBUF ) | ( libc:: SOL_SOCKET , libc:: SO_SNDBUF ) => 0 ,
549
549
550
550
_ => {
551
551
warn!( "unhandled getsockopt {} {}" , level, name) ;
@@ -1015,6 +1015,7 @@ pub struct Endpoint {
1015
1015
net : Arc < NetSim > ,
1016
1016
node : NodeId ,
1017
1017
addr : SocketAddr ,
1018
+ proto : libc:: c_int ,
1018
1019
peer : Option < SocketAddr > ,
1019
1020
live_tcp_ids : Mutex < HashSet < u32 > > ,
1020
1021
}
@@ -1030,16 +1031,17 @@ impl std::fmt::Debug for Endpoint {
1030
1031
}
1031
1032
1032
1033
impl Endpoint {
1033
- /// Bind synchronously (for UDP)
1034
- pub fn bind_sync ( addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1034
+ /// Bind synchronously
1035
+ pub fn bind_sync ( proto : libc :: c_int , addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1035
1036
let net = plugin:: simulator :: < NetSim > ( ) ;
1036
1037
let node = plugin:: node ( ) ;
1037
1038
let addr = addr. to_socket_addrs ( ) ?. next ( ) . unwrap ( ) ;
1038
- let addr = net. network . lock ( ) . unwrap ( ) . bind ( node, addr) ?;
1039
+ let addr = net. network . lock ( ) . unwrap ( ) . bind ( node, proto , addr) ?;
1039
1040
let ep = Endpoint {
1040
1041
net,
1041
1042
node,
1042
1043
addr,
1044
+ proto,
1043
1045
peer : None ,
1044
1046
live_tcp_ids : Default :: default ( ) ,
1045
1047
} ;
@@ -1063,30 +1065,31 @@ impl Endpoint {
1063
1065
}
1064
1066
1065
1067
/// Creates a [`Endpoint`] from the given address.
1066
- pub async fn bind ( addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1068
+ pub async fn bind ( proto : libc :: c_int , addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1067
1069
let net = plugin:: simulator :: < NetSim > ( ) ;
1068
1070
let node = plugin:: node ( ) ;
1069
1071
let addr = addr. to_socket_addrs ( ) ?. next ( ) . unwrap ( ) ;
1070
1072
net. rand_delay ( ) . await ;
1071
- let addr = net. network . lock ( ) . unwrap ( ) . bind ( node, addr) ?;
1073
+ let addr = net. network . lock ( ) . unwrap ( ) . bind ( node, proto , addr) ?;
1072
1074
Ok ( Endpoint {
1073
1075
net,
1074
1076
node,
1075
1077
addr,
1078
+ proto,
1076
1079
peer : None ,
1077
1080
live_tcp_ids : Default :: default ( ) ,
1078
1081
} )
1079
1082
}
1080
1083
1081
1084
/// Connects this [`Endpoint`] to a remote address.
1082
- pub async fn connect ( addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1085
+ pub async fn connect ( proto : libc :: c_int , addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1083
1086
let net = plugin:: simulator :: < NetSim > ( ) ;
1084
1087
net. rand_delay ( ) . await ;
1085
- Self :: connect_sync ( addr)
1088
+ Self :: connect_sync ( proto , addr)
1086
1089
}
1087
1090
1088
1091
/// For libc::connect()
1089
- pub fn connect_sync ( addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1092
+ pub fn connect_sync ( proto : libc :: c_int , addr : impl ToSocketAddrs ) -> io:: Result < Self > {
1090
1093
let net = plugin:: simulator :: < NetSim > ( ) ;
1091
1094
let node = plugin:: node ( ) ;
1092
1095
let peer = addr. to_socket_addrs ( ) ?. next ( ) . unwrap ( ) ;
@@ -1095,11 +1098,12 @@ impl Endpoint {
1095
1098
} else {
1096
1099
SocketAddr :: from ( ( Ipv4Addr :: UNSPECIFIED , 0 ) )
1097
1100
} ;
1098
- let addr = net. network . lock ( ) . unwrap ( ) . bind ( node, addr) ?;
1101
+ let addr = net. network . lock ( ) . unwrap ( ) . bind ( node, proto , addr) ?;
1099
1102
Ok ( Endpoint {
1100
1103
net,
1101
1104
node,
1102
1105
addr,
1106
+ proto,
1103
1107
peer : Some ( peer) ,
1104
1108
live_tcp_ids : Default :: default ( ) ,
1105
1109
} )
@@ -1128,7 +1132,7 @@ impl Endpoint {
1128
1132
. network
1129
1133
. lock ( )
1130
1134
. unwrap ( )
1131
- . deregister_tcp_id ( self . node , remote_sock, id) ;
1135
+ . deregister_tcp_id ( self . node , self . proto , remote_sock, id) ;
1132
1136
}
1133
1137
1134
1138
/// Returns the local socket address.
@@ -1234,7 +1238,7 @@ impl Endpoint {
1234
1238
. network
1235
1239
. lock ( )
1236
1240
. unwrap ( )
1237
- . send ( plugin:: node ( ) , self . addr , dst, tag, data)
1241
+ . send ( plugin:: node ( ) , self . proto , self . addr , dst, tag, data)
1238
1242
}
1239
1243
1240
1244
/// Receives a raw message.
@@ -1244,12 +1248,12 @@ impl Endpoint {
1244
1248
#[ cfg_attr( docsrs, doc( cfg( msim) ) ) ]
1245
1249
pub async fn recv_from_raw ( & self , tag : u64 ) -> io:: Result < ( Payload , SocketAddr ) > {
1246
1250
trace ! ( "awaiting recv: {} tag={:x}" , self . addr, tag) ;
1247
- let recver = self
1248
- . net
1249
- . network
1250
- . lock ( )
1251
- . unwrap ( )
1252
- . recv ( plugin:: node ( ) , self . addr , tag) ;
1251
+ let recver =
1252
+ self . net
1253
+ . network
1254
+ . lock ( )
1255
+ . unwrap ( )
1256
+ . recv ( plugin:: node ( ) , self . proto , self . addr , tag) ;
1253
1257
let msg = recver
1254
1258
. await
1255
1259
. map_err ( |_| io:: Error :: new ( io:: ErrorKind :: BrokenPipe , "network is down" ) ) ?;
@@ -1266,7 +1270,7 @@ impl Endpoint {
1266
1270
. network
1267
1271
. lock ( )
1268
1272
. unwrap ( )
1269
- . recv_sync ( plugin:: node ( ) , self . addr , tag)
1273
+ . recv_sync ( plugin:: node ( ) , self . proto , self . addr , tag)
1270
1274
. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: WouldBlock , "recv call would blck" ) ) ?;
1271
1275
1272
1276
trace ! (
@@ -1320,12 +1324,13 @@ impl Endpoint {
1320
1324
/// Check if there is a message waiting that can be received without blocking.
1321
1325
/// If not, schedule a wakeup using the context.
1322
1326
pub fn recv_ready ( & self , cx : Option < & mut Context < ' _ > > , tag : u64 ) -> io:: Result < bool > {
1323
- Ok ( self
1324
- . net
1325
- . network
1326
- . lock ( )
1327
- . unwrap ( )
1328
- . recv_ready ( cx, plugin:: node ( ) , self . addr , tag) )
1327
+ Ok ( self . net . network . lock ( ) . unwrap ( ) . recv_ready (
1328
+ cx,
1329
+ plugin:: node ( ) ,
1330
+ self . proto ,
1331
+ self . addr ,
1332
+ tag,
1333
+ ) )
1329
1334
}
1330
1335
}
1331
1336
@@ -1338,7 +1343,7 @@ impl Drop for Endpoint {
1338
1343
1339
1344
// avoid panic on panicking
1340
1345
if let Ok ( mut network) = self . net . network . lock ( ) {
1341
- network. close ( self . node , self . addr ) ;
1346
+ network. close ( self . proto , self . node , self . addr ) ;
1342
1347
}
1343
1348
}
1344
1349
}
@@ -1372,7 +1377,7 @@ mod tests {
1372
1377
1373
1378
let barrier_ = barrier. clone ( ) ;
1374
1379
node1. spawn ( async move {
1375
- let net = Endpoint :: bind ( addr1) . await . unwrap ( ) ;
1380
+ let net = Endpoint :: bind ( libc :: SOCK_STREAM , addr1) . await . unwrap ( ) ;
1376
1381
barrier_. wait ( ) . await ;
1377
1382
1378
1383
net. send_to ( addr2, 1 , payload ! ( vec![ 1 ] ) ) . await . unwrap ( ) ;
@@ -1382,7 +1387,7 @@ mod tests {
1382
1387
} ) ;
1383
1388
1384
1389
let f = node2. spawn ( async move {
1385
- let net = Endpoint :: bind ( addr2) . await . unwrap ( ) ;
1390
+ let net = Endpoint :: bind ( libc :: SOCK_STREAM , addr2) . await . unwrap ( ) ;
1386
1391
barrier. wait ( ) . await ;
1387
1392
1388
1393
let mut buf = vec ! [ 0 ; 0x10 ] ;
@@ -1411,14 +1416,14 @@ mod tests {
1411
1416
1412
1417
let barrier_ = barrier. clone ( ) ;
1413
1418
node1. spawn ( async move {
1414
- let net = Endpoint :: bind ( addr1) . await . unwrap ( ) ;
1419
+ let net = Endpoint :: bind ( libc :: SOCK_STREAM , addr1) . await . unwrap ( ) ;
1415
1420
barrier_. wait ( ) . await ;
1416
1421
1417
1422
net. send_to ( addr2, 1 , payload ! ( vec![ 1 ] ) ) . await . unwrap ( ) ;
1418
1423
} ) ;
1419
1424
1420
1425
let f = node2. spawn ( async move {
1421
- let net = Endpoint :: bind ( addr2) . await . unwrap ( ) ;
1426
+ let net = Endpoint :: bind ( libc :: SOCK_STREAM , addr2) . await . unwrap ( ) ;
1422
1427
let mut buf = vec ! [ 0 ; 0x10 ] ;
1423
1428
timeout ( Duration :: from_secs ( 1 ) , net. recv_from ( 1 , & mut buf) )
1424
1429
. await
@@ -1443,7 +1448,7 @@ mod tests {
1443
1448
let node1 = runtime. create_node ( ) . ip ( addr1. ip ( ) ) . build ( ) ;
1444
1449
1445
1450
let f = node1. spawn ( async move {
1446
- let net = Endpoint :: bind ( addr1) . await . unwrap ( ) ;
1451
+ let net = Endpoint :: bind ( libc :: SOCK_STREAM , addr1) . await . unwrap ( ) ;
1447
1452
let err = net. recv_from ( 1 , & mut [ ] ) . await . unwrap_err ( ) ;
1448
1453
assert_eq ! ( err. kind( ) , std:: io:: ErrorKind :: BrokenPipe ) ;
1449
1454
// FIXME: should still error
@@ -1466,36 +1471,47 @@ mod tests {
1466
1471
1467
1472
let f = node. spawn ( async move {
1468
1473
// unspecified
1469
- let ep = Endpoint :: bind ( "0.0.0.0:0" ) . await . unwrap ( ) ;
1474
+ let ep = Endpoint :: bind ( libc:: SOCK_STREAM , "0.0.0.0:0" )
1475
+ . await
1476
+ . unwrap ( ) ;
1470
1477
let addr = ep. local_addr ( ) . unwrap ( ) ;
1471
1478
assert_eq ! ( addr. ip( ) , ip) ;
1472
1479
assert_ne ! ( addr. port( ) , 0 ) ;
1473
1480
1474
1481
// unspecified v6
1475
- let ep = Endpoint :: bind ( ":::0" ) . await . unwrap ( ) ;
1482
+ let ep = Endpoint :: bind ( libc :: SOCK_STREAM , ":::0" ) . await . unwrap ( ) ;
1476
1483
let addr = ep. local_addr ( ) . unwrap ( ) ;
1477
1484
assert_eq ! ( addr. ip( ) , ip) ;
1478
1485
assert_ne ! ( addr. port( ) , 0 ) ;
1479
1486
1480
1487
// localhost
1481
- let ep = Endpoint :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
1488
+ let ep = Endpoint :: bind ( libc:: SOCK_STREAM , "127.0.0.1:0" )
1489
+ . await
1490
+ . unwrap ( ) ;
1482
1491
let addr = ep. local_addr ( ) . unwrap ( ) ;
1483
1492
assert_eq ! ( addr. ip( ) . to_string( ) , "127.0.0.1" ) ;
1484
1493
assert_ne ! ( addr. port( ) , 0 ) ;
1485
1494
1486
1495
// localhost v6
1487
- let ep = Endpoint :: bind ( "::1:0" ) . await . unwrap ( ) ;
1496
+ let ep = Endpoint :: bind ( libc :: SOCK_STREAM , "::1:0" ) . await . unwrap ( ) ;
1488
1497
let addr = ep. local_addr ( ) . unwrap ( ) ;
1489
1498
assert_eq ! ( addr. ip( ) . to_string( ) , "::1" ) ;
1490
1499
assert_ne ! ( addr. port( ) , 0 ) ;
1491
1500
1492
1501
// wrong IP
1493
- let err = Endpoint :: bind ( "10.0.0.2:0" ) . await . err ( ) . unwrap ( ) ;
1502
+ let err = Endpoint :: bind ( libc:: SOCK_STREAM , "10.0.0.2:0" )
1503
+ . await
1504
+ . err ( )
1505
+ . unwrap ( ) ;
1494
1506
assert_eq ! ( err. kind( ) , std:: io:: ErrorKind :: AddrNotAvailable ) ;
1495
1507
1496
1508
// drop and reuse port
1497
- let _ = Endpoint :: bind ( "10.0.0.1:100" ) . await . unwrap ( ) ;
1498
- let _ = Endpoint :: bind ( "10.0.0.1:100" ) . await . unwrap ( ) ;
1509
+ let _ = Endpoint :: bind ( libc:: SOCK_STREAM , "10.0.0.1:100" )
1510
+ . await
1511
+ . unwrap ( ) ;
1512
+ let _ = Endpoint :: bind ( libc:: SOCK_STREAM , "10.0.0.1:100" )
1513
+ . await
1514
+ . unwrap ( ) ;
1499
1515
} ) ;
1500
1516
runtime. block_on ( f) . unwrap ( ) ;
1501
1517
}
@@ -1512,8 +1528,12 @@ mod tests {
1512
1528
1513
1529
let barrier_ = barrier. clone ( ) ;
1514
1530
let f1 = node1. spawn ( async move {
1515
- let ep1 = Endpoint :: bind ( "127.0.0.1:1" ) . await . unwrap ( ) ;
1516
- let ep2 = Endpoint :: bind ( "10.0.0.1:2" ) . await . unwrap ( ) ;
1531
+ let ep1 = Endpoint :: bind ( libc:: SOCK_STREAM , "127.0.0.1:1" )
1532
+ . await
1533
+ . unwrap ( ) ;
1534
+ let ep2 = Endpoint :: bind ( libc:: SOCK_STREAM , "10.0.0.1:2" )
1535
+ . await
1536
+ . unwrap ( ) ;
1517
1537
barrier_. wait ( ) . await ;
1518
1538
1519
1539
// FIXME: ep1 should not receive messages from other node
@@ -1525,7 +1545,9 @@ mod tests {
1525
1545
ep2. recv_from ( 1 , & mut [ ] ) . await . unwrap ( ) ;
1526
1546
} ) ;
1527
1547
let f2 = node2. spawn ( async move {
1528
- let ep = Endpoint :: bind ( "127.0.0.1:1" ) . await . unwrap ( ) ;
1548
+ let ep = Endpoint :: bind ( libc:: SOCK_STREAM , "127.0.0.1:1" )
1549
+ . await
1550
+ . unwrap ( ) ;
1529
1551
barrier. wait ( ) . await ;
1530
1552
1531
1553
ep. send_to ( "10.0.0.1:1" , 1 , payload ! ( vec![ 1 ] ) )
@@ -1550,7 +1572,7 @@ mod tests {
1550
1572
1551
1573
let barrier_ = barrier. clone ( ) ;
1552
1574
node1. spawn ( async move {
1553
- let ep = Endpoint :: bind ( addr1) . await . unwrap ( ) ;
1575
+ let ep = Endpoint :: bind ( libc :: SOCK_STREAM , addr1) . await . unwrap ( ) ;
1554
1576
assert_eq ! ( ep. local_addr( ) . unwrap( ) , addr1) ;
1555
1577
barrier_. wait ( ) . await ;
1556
1578
@@ -1565,7 +1587,7 @@ mod tests {
1565
1587
1566
1588
let f = node2. spawn ( async move {
1567
1589
barrier. wait ( ) . await ;
1568
- let ep = Endpoint :: connect ( addr1) . await . unwrap ( ) ;
1590
+ let ep = Endpoint :: connect ( libc :: SOCK_STREAM , addr1) . await . unwrap ( ) ;
1569
1591
assert_eq ! ( ep. peer_addr( ) . unwrap( ) , addr1) ;
1570
1592
1571
1593
ep. send ( 1 , payload ! ( b"ping" . to_vec( ) ) ) . await . unwrap ( ) ;
0 commit comments