@@ -322,56 +322,30 @@ static int net_conn_change_remote(struct net_conn *conn,
322
322
return 0 ;
323
323
}
324
324
325
- int net_conn_register (uint16_t proto , enum net_sock_type type , uint8_t family ,
326
- const struct sockaddr * remote_addr ,
327
- const struct sockaddr * local_addr ,
328
- uint16_t remote_port ,
329
- uint16_t local_port ,
330
- struct net_context * context ,
331
- net_conn_cb_t cb ,
332
- void * user_data ,
333
- struct net_conn_handle * * handle )
325
+ static int net_conn_change_local (struct net_conn * conn ,
326
+ const struct sockaddr * local_addr ,
327
+ uint16_t local_port )
334
328
{
335
- struct net_conn * conn ;
336
- uint8_t flags = 0U ;
337
- int ret ;
338
-
339
- conn = conn_find_handler (context != NULL ? net_context_get_iface (context ) : NULL ,
340
- proto , family , remote_addr , local_addr ,
341
- remote_port , local_port ,
342
- context != NULL ?
343
- net_context_is_reuseport_set (context ) :
344
- false);
345
- if (conn ) {
346
- NET_ERR ("Identical connection handler %p already found." , conn );
347
- return - EADDRINUSE ;
348
- }
349
-
350
- conn = conn_get_unused ();
351
- if (!conn ) {
352
- NET_ERR ("Not enough connection contexts. "
353
- "Consider increasing CONFIG_NET_MAX_CONN." );
354
- return - ENOENT ;
355
- }
329
+ NET_DBG ("[%zu] connection handler %p changed local" ,
330
+ conn - conns , conn );
356
331
357
- if (local_addr ) {
332
+ if (local_addr != NULL ) {
358
333
if (IS_ENABLED (CONFIG_NET_IPV6 ) &&
359
334
local_addr -> sa_family == AF_INET6 ) {
360
335
memcpy (& conn -> local_addr , local_addr ,
361
336
sizeof (struct sockaddr_in6 ));
362
337
363
338
if (!net_ipv6_is_addr_unspecified (
364
- & net_sin6 (local_addr )->
365
- sin6_addr )) {
366
- flags |= NET_CONN_LOCAL_ADDR_SPEC ;
339
+ & net_sin6 (local_addr )-> sin6_addr )) {
340
+ conn -> flags |= NET_CONN_LOCAL_ADDR_SPEC ;
367
341
}
368
342
} else if (IS_ENABLED (CONFIG_NET_IPV4 ) &&
369
343
local_addr -> sa_family == AF_INET ) {
370
344
memcpy (& conn -> local_addr , local_addr ,
371
345
sizeof (struct sockaddr_in ));
372
346
373
347
if (net_sin (local_addr )-> sin_addr .s_addr ) {
374
- flags |= NET_CONN_LOCAL_ADDR_SPEC ;
348
+ conn -> flags |= NET_CONN_LOCAL_ADDR_SPEC ;
375
349
}
376
350
} else if (IS_ENABLED (CONFIG_NET_SOCKETS_CAN ) &&
377
351
local_addr -> sa_family == AF_CAN ) {
@@ -383,10 +357,54 @@ int net_conn_register(uint16_t proto, enum net_sock_type type, uint8_t family,
383
357
sizeof (struct sockaddr_ll ));
384
358
} else {
385
359
NET_ERR ("Local address family not set" );
386
- goto error ;
360
+ return - EINVAL ;
387
361
}
388
362
389
- flags |= NET_CONN_LOCAL_ADDR_SET ;
363
+ conn -> flags |= NET_CONN_LOCAL_ADDR_SET ;
364
+ } else {
365
+ conn -> flags &= ~NET_CONN_LOCAL_ADDR_SPEC ;
366
+ conn -> flags &= ~NET_CONN_LOCAL_ADDR_SET ;
367
+ }
368
+
369
+ if (local_port > 0U ) {
370
+ conn -> flags |= NET_CONN_LOCAL_PORT_SPEC ;
371
+ net_sin (& conn -> local_addr )-> sin_port = htons (local_port );
372
+ } else {
373
+ conn -> flags &= ~NET_CONN_LOCAL_PORT_SPEC ;
374
+ }
375
+
376
+ return 0 ;
377
+ }
378
+
379
+ int net_conn_register (uint16_t proto , enum net_sock_type type , uint8_t family ,
380
+ const struct sockaddr * remote_addr ,
381
+ const struct sockaddr * local_addr ,
382
+ uint16_t remote_port ,
383
+ uint16_t local_port ,
384
+ struct net_context * context ,
385
+ net_conn_cb_t cb ,
386
+ void * user_data ,
387
+ struct net_conn_handle * * handle )
388
+ {
389
+ struct net_conn * conn ;
390
+ int ret ;
391
+
392
+ conn = conn_find_handler (context != NULL ? net_context_get_iface (context ) : NULL ,
393
+ proto , family , remote_addr , local_addr ,
394
+ remote_port , local_port ,
395
+ context != NULL ?
396
+ net_context_is_reuseport_set (context ) :
397
+ false);
398
+ if (conn != NULL ) {
399
+ NET_ERR ("Identical connection handler %p already found." , conn );
400
+ return - EADDRINUSE ;
401
+ }
402
+
403
+ conn = conn_get_unused ();
404
+ if (conn == NULL ) {
405
+ NET_ERR ("Not enough connection contexts. "
406
+ "Consider increasing CONFIG_NET_MAX_CONN." );
407
+ return - ENOENT ;
390
408
}
391
409
392
410
if (remote_addr && local_addr ) {
@@ -396,25 +414,20 @@ int net_conn_register(uint16_t proto, enum net_sock_type type, uint8_t family,
396
414
}
397
415
}
398
416
399
- if (local_port ) {
400
- flags |= NET_CONN_LOCAL_PORT_SPEC ;
401
- net_sin (& conn -> local_addr )-> sin_port = htons (local_port );
402
- }
403
-
404
417
net_conn_change_callback (conn , cb , user_data );
405
418
406
- conn -> flags = flags ;
407
419
conn -> proto = proto ;
408
420
conn -> type = type ;
409
421
conn -> family = family ;
410
422
conn -> context = context ;
411
423
412
- /*
413
- * Since the net_conn_change_remote() updates the flags in connection,
414
- * must to be called after set the flags to connection.
415
- */
424
+ ret = net_conn_change_local (conn , local_addr , local_port );
425
+ if (ret < 0 ) {
426
+ goto error ;
427
+ }
428
+
416
429
ret = net_conn_change_remote (conn , remote_addr , remote_port );
417
- if (ret ) {
430
+ if (ret < 0 ) {
418
431
goto error ;
419
432
}
420
433
@@ -461,7 +474,9 @@ int net_conn_update(struct net_conn_handle *handle,
461
474
net_conn_cb_t cb ,
462
475
void * user_data ,
463
476
const struct sockaddr * remote_addr ,
464
- uint16_t remote_port )
477
+ uint16_t remote_port ,
478
+ const struct sockaddr * local_addr ,
479
+ uint16_t local_port )
465
480
{
466
481
struct net_conn * conn = (struct net_conn * )handle ;
467
482
int ret ;
@@ -476,6 +491,11 @@ int net_conn_update(struct net_conn_handle *handle,
476
491
477
492
net_conn_change_callback (conn , cb , user_data );
478
493
494
+ ret = net_conn_change_local (conn , local_addr , local_port );
495
+ if (ret < 0 ) {
496
+ return ret ;
497
+ }
498
+
479
499
ret = net_conn_change_remote (conn , remote_addr , remote_port );
480
500
481
501
return ret ;
0 commit comments