@@ -695,7 +695,7 @@ void AsyncClient::onPoll(AcConnectHandler cb, void* arg){
695
695
* Main Public Methods
696
696
* */
697
697
698
- bool AsyncClient::connect (IPAddress ip , uint16_t port){
698
+ bool AsyncClient::_connect ( ip_addr_t addr , uint16_t port){
699
699
if (_pcb){
700
700
log_w (" already connected, state %d" , _pcb->state );
701
701
return false ;
@@ -705,15 +705,7 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
705
705
return false ;
706
706
}
707
707
708
- ip_addr_t addr;
709
- #if LWIP_IPV4 && LWIP_IPV6
710
- addr.type = IPADDR_TYPE_V4;
711
- addr.u_addr .ip4 .addr = ip;
712
- #else
713
- addr.addr = ip;
714
- #endif
715
-
716
- tcp_pcb* pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
708
+ tcp_pcb* pcb = tcp_new_ip_type (addr.type );
717
709
if (!pcb){
718
710
log_e (" pcb == NULL" );
719
711
return false ;
@@ -724,22 +716,39 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
724
716
tcp_recv (pcb, &_tcp_recv);
725
717
tcp_sent (pcb, &_tcp_sent);
726
718
tcp_poll (pcb, &_tcp_poll, 1 );
727
- // _tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected);
728
719
_tcp_connect (pcb, _closed_slot, &addr, port,(tcp_connected_fn)&_tcp_connected);
729
720
return true ;
730
721
}
731
722
723
+ bool AsyncClient::connect (IPAddress ip, uint16_t port){
724
+ ip_addr_t addr;
725
+ addr.type = IPADDR_TYPE_V4;
726
+ addr.u_addr .ip4 .addr = ip;
727
+
728
+ return _connect (addr, port);
729
+ }
730
+
731
+ bool AsyncClient::connect (IPv6Address ip, uint16_t port){
732
+ ip_addr_t addr;
733
+ addr.type = IPADDR_TYPE_V6;
734
+ memcpy (addr.u_addr .ip6 .addr , static_cast <const uint32_t *>(ip), sizeof (uint32_t ) * 4 );
735
+
736
+ return _connect (addr, port);
737
+ }
738
+
732
739
bool AsyncClient::connect (const char * host, uint16_t port){
733
740
ip_addr_t addr;
734
-
741
+
735
742
if (!_start_async_task ()){
736
743
log_e (" failed to start task" );
737
744
return false ;
738
745
}
739
-
746
+
740
747
err_t err = dns_gethostbyname (host, &addr, (dns_found_callback)&_tcp_dns_found, this );
741
748
if (err == ERR_OK) {
742
- #if LWIP_IPV4 && LWIP_IPV6
749
+ if (addr.type == IPADDR_TYPE_V6) {
750
+ return connect (IPv6Address (addr.u_addr .ip6 .addr ), port);
751
+ }
743
752
return connect (IPAddress (addr.u_addr .ip4 .addr ), port);
744
753
#else
745
754
return connect (IPAddress (addr.addr ), port);
@@ -1011,10 +1020,8 @@ void AsyncClient::_dns_found(struct ip_addr *ipaddr){
1011
1020
#if LWIP_IPV4 && LWIP_IPV6
1012
1021
if (ipaddr && ipaddr->u_addr .ip4 .addr ){
1013
1022
connect (IPAddress (ipaddr->u_addr .ip4 .addr ), _connect_port);
1014
- #else
1015
- if (ipaddr && ipaddr->addr ){
1016
- connect (IPAddress (ipaddr->addr ), _connect_port);
1017
- #endif
1023
+ } else if (ipaddr && ipaddr->u_addr .ip6 .addr ){
1024
+ connect (IPv6Address (ipaddr->u_addr .ip6 .addr ), _connect_port);
1018
1025
} else {
1019
1026
if (_error_cb) {
1020
1027
_error_cb (_error_cb_arg, this , -55 );
@@ -1110,6 +1117,15 @@ uint32_t AsyncClient::getRemoteAddress() {
1110
1117
#endif
1111
1118
}
1112
1119
1120
+ ip6_addr_t AsyncClient::getRemoteAddress6 () {
1121
+ if (!_pcb) {
1122
+ ip6_addr_t nulladdr;
1123
+ ip6_addr_set_zero (&nulladdr);
1124
+ return nulladdr;
1125
+ }
1126
+ return _pcb->remote_ip .u_addr .ip6 ;
1127
+ }
1128
+
1113
1129
uint16_t AsyncClient::getRemotePort () {
1114
1130
if (!_pcb) {
1115
1131
return 0 ;
@@ -1128,6 +1144,15 @@ uint32_t AsyncClient::getLocalAddress() {
1128
1144
#endif
1129
1145
}
1130
1146
1147
+ ip6_addr_t AsyncClient::getLocalAddress6 () {
1148
+ if (!_pcb) {
1149
+ ip6_addr_t nulladdr;
1150
+ ip6_addr_set_zero (&nulladdr);
1151
+ return nulladdr;
1152
+ }
1153
+ return _pcb->local_ip .u_addr .ip6 ;
1154
+ }
1155
+
1131
1156
uint16_t AsyncClient::getLocalPort () {
1132
1157
if (!_pcb) {
1133
1158
return 0 ;
@@ -1139,6 +1164,10 @@ IPAddress AsyncClient::remoteIP() {
1139
1164
return IPAddress (getRemoteAddress ());
1140
1165
}
1141
1166
1167
+ IPv6Address AsyncClient::remoteIP6 () {
1168
+ return IPv6Address (getRemoteAddress6 ().addr );
1169
+ }
1170
+
1142
1171
uint16_t AsyncClient::remotePort () {
1143
1172
return getRemotePort ();
1144
1173
}
@@ -1147,6 +1176,10 @@ IPAddress AsyncClient::localIP() {
1147
1176
return IPAddress (getLocalAddress ());
1148
1177
}
1149
1178
1179
+ IPv6Address AsyncClient::localIP6 () {
1180
+ return IPv6Address (getLocalAddress6 ().addr );
1181
+ }
1182
+
1150
1183
uint16_t AsyncClient::localPort () {
1151
1184
return getLocalPort ();
1152
1185
}
@@ -1279,16 +1312,30 @@ int8_t AsyncClient::_s_connected(void * arg, void * pcb, int8_t err){
1279
1312
1280
1313
AsyncServer::AsyncServer (IPAddress addr, uint16_t port)
1281
1314
: _port (port)
1315
+ , _bind4 (true )
1282
1316
, _addr (addr)
1283
1317
, _noDelay (false )
1284
1318
, _pcb (0 )
1285
1319
, _connect_cb (0 )
1286
1320
, _connect_cb_arg (0 )
1287
1321
{}
1288
1322
1323
+ AsyncServer::AsyncServer (IPv6Address addr, uint16_t port)
1324
+ : _port (port)
1325
+ , _bind6 (true )
1326
+ , _addr6 (addr)
1327
+ , _noDelay (false )
1328
+ , _pcb (0 )
1329
+ , _connect_cb (0 )
1330
+ , _connect_cb_arg (0 )
1331
+ {}
1332
+
1289
1333
AsyncServer::AsyncServer (uint16_t port)
1290
1334
: _port (port)
1335
+ , _bind4 (true )
1336
+ , _bind6 (true )
1291
1337
, _addr ((uint32_t ) IPADDR_ANY)
1338
+ , _addr6 ()
1292
1339
, _noDelay (false )
1293
1340
, _pcb (0 )
1294
1341
, _connect_cb (0 )
@@ -1313,20 +1360,26 @@ void AsyncServer::begin(){
1313
1360
log_e (" failed to start task" );
1314
1361
return ;
1315
1362
}
1316
- int8_t err;
1317
- _pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
1363
+ int8_t err, bind_type;
1364
+
1365
+ if (_bind4 && _bind6) {
1366
+ bind_type = IPADDR_TYPE_ANY;
1367
+ } else if (_bind6) {
1368
+ bind_type = IPADDR_TYPE_V6;
1369
+ } else {
1370
+ bind_type = IPADDR_TYPE_V4;
1371
+ }
1372
+
1373
+ _pcb = tcp_new_ip_type (bind_type);
1318
1374
if (!_pcb){
1319
1375
log_e (" _pcb == NULL" );
1320
1376
return ;
1321
1377
}
1322
1378
1323
1379
ip_addr_t local_addr;
1324
- #if LWIP_IPV4 && LWIP_IPV6
1325
- local_addr.type = IPADDR_TYPE_V4;
1380
+ local_addr.type = bind_type;
1326
1381
local_addr.u_addr .ip4 .addr = (uint32_t ) _addr;
1327
- #else
1328
- local_addr.addr = (uint32_t ) _addr;
1329
- #endif
1382
+ memcpy (local_addr.u_addr .ip6 .addr , static_cast <const uint32_t *>(_addr6), sizeof (uint32_t ) * 4 );
1330
1383
err = _tcp_bind (_pcb, &local_addr, _port);
1331
1384
1332
1385
if (err != ERR_OK) {
0 commit comments