@@ -340,18 +340,7 @@ static int s_update_local_endpoint(struct aws_socket *socket) {
340340 } else if (address .ss_family == AF_VSOCK ) {
341341 struct sockaddr_vm * s = (struct sockaddr_vm * )& address ;
342342
343- /* VSOCK port is 32bit, but aws_socket_endpoint.port is only 16bit.
344- * Hopefully this isn't an issue, since users can only pass in 16bit values.
345- * But if it becomes an issue, we'll need to make aws_socket_endpoint more flexible */
346- if (s -> svm_port > UINT16_MAX ) {
347- AWS_LOGF_ERROR (
348- AWS_LS_IO_SOCKET ,
349- "id=%p fd=%d: aws_socket_endpoint can't deal with VSOCK port > UINT16_MAX" ,
350- (void * )socket ,
351- socket -> io_handle .data .fd );
352- return aws_raise_error (AWS_IO_SOCKET_INVALID_ADDRESS );
353- }
354- tmp_endpoint .port = (uint16_t )s -> svm_port ;
343+ tmp_endpoint .port = s -> svm_port ;
355344
356345 snprintf (tmp_endpoint .address , sizeof (tmp_endpoint .address ), "%" PRIu32 , s -> svm_cid );
357346 return AWS_OP_SUCCESS ;
@@ -642,18 +631,22 @@ int aws_socket_connect(
642631 return AWS_OP_ERR ;
643632 }
644633
634+ if (aws_socket_validate_port_for_connect (remote_endpoint -> port , socket -> options .domain )) {
635+ return AWS_OP_ERR ;
636+ }
637+
645638 struct socket_address address ;
646639 AWS_ZERO_STRUCT (address );
647640 socklen_t sock_size = 0 ;
648641 int pton_err = 1 ;
649642 if (socket -> options .domain == AWS_SOCKET_IPV4 ) {
650643 pton_err = inet_pton (AF_INET , remote_endpoint -> address , & address .sock_addr_types .addr_in .sin_addr );
651- address .sock_addr_types .addr_in .sin_port = htons (remote_endpoint -> port );
644+ address .sock_addr_types .addr_in .sin_port = htons (( uint16_t ) remote_endpoint -> port );
652645 address .sock_addr_types .addr_in .sin_family = AF_INET ;
653646 sock_size = sizeof (address .sock_addr_types .addr_in );
654647 } else if (socket -> options .domain == AWS_SOCKET_IPV6 ) {
655648 pton_err = inet_pton (AF_INET6 , remote_endpoint -> address , & address .sock_addr_types .addr_in6 .sin6_addr );
656- address .sock_addr_types .addr_in6 .sin6_port = htons (remote_endpoint -> port );
649+ address .sock_addr_types .addr_in6 .sin6_port = htons (( uint16_t ) remote_endpoint -> port );
657650 address .sock_addr_types .addr_in6 .sin6_family = AF_INET6 ;
658651 sock_size = sizeof (address .sock_addr_types .addr_in6 );
659652 } else if (socket -> options .domain == AWS_SOCKET_LOCAL ) {
@@ -664,7 +657,7 @@ int aws_socket_connect(
664657 } else if (socket -> options .domain == AWS_SOCKET_VSOCK ) {
665658 pton_err = parse_cid (remote_endpoint -> address , & address .sock_addr_types .vm_addr .svm_cid );
666659 address .sock_addr_types .vm_addr .svm_family = AF_VSOCK ;
667- address .sock_addr_types .vm_addr .svm_port = ( unsigned int ) remote_endpoint -> port ;
660+ address .sock_addr_types .vm_addr .svm_port = remote_endpoint -> port ;
668661 sock_size = sizeof (address .sock_addr_types .vm_addr );
669662#endif
670663 } else {
@@ -676,21 +669,21 @@ int aws_socket_connect(
676669 int errno_value = errno ; /* Always cache errno before potential side-effect */
677670 AWS_LOGF_DEBUG (
678671 AWS_LS_IO_SOCKET ,
679- "id=%p fd=%d: failed to parse address %s:%d ." ,
672+ "id=%p fd=%d: failed to parse address %s:%u ." ,
680673 (void * )socket ,
681674 socket -> io_handle .data .fd ,
682675 remote_endpoint -> address ,
683- ( int ) remote_endpoint -> port );
676+ remote_endpoint -> port );
684677 return aws_raise_error (s_convert_pton_error (pton_err , errno_value ));
685678 }
686679
687680 AWS_LOGF_DEBUG (
688681 AWS_LS_IO_SOCKET ,
689- "id=%p fd=%d: connecting to endpoint %s:%d ." ,
682+ "id=%p fd=%d: connecting to endpoint %s:%u ." ,
690683 (void * )socket ,
691684 socket -> io_handle .data .fd ,
692685 remote_endpoint -> address ,
693- ( int ) remote_endpoint -> port );
686+ remote_endpoint -> port );
694687
695688 socket -> state = CONNECTING ;
696689 socket -> remote_endpoint = * remote_endpoint ;
@@ -806,26 +799,30 @@ int aws_socket_bind(struct aws_socket *socket, const struct aws_socket_endpoint
806799 return AWS_OP_ERR ;
807800 }
808801
802+ if (aws_socket_validate_port_for_bind (local_endpoint -> port , socket -> options .domain )) {
803+ return AWS_OP_ERR ;
804+ }
805+
809806 AWS_LOGF_INFO (
810807 AWS_LS_IO_SOCKET ,
811- "id=%p fd=%d: binding to %s:%d ." ,
808+ "id=%p fd=%d: binding to %s:%u ." ,
812809 (void * )socket ,
813810 socket -> io_handle .data .fd ,
814811 local_endpoint -> address ,
815- ( int ) local_endpoint -> port );
812+ local_endpoint -> port );
816813
817814 struct socket_address address ;
818815 AWS_ZERO_STRUCT (address );
819816 socklen_t sock_size = 0 ;
820817 int pton_err = 1 ;
821818 if (socket -> options .domain == AWS_SOCKET_IPV4 ) {
822819 pton_err = inet_pton (AF_INET , local_endpoint -> address , & address .sock_addr_types .addr_in .sin_addr );
823- address .sock_addr_types .addr_in .sin_port = htons (local_endpoint -> port );
820+ address .sock_addr_types .addr_in .sin_port = htons (( uint16_t ) local_endpoint -> port );
824821 address .sock_addr_types .addr_in .sin_family = AF_INET ;
825822 sock_size = sizeof (address .sock_addr_types .addr_in );
826823 } else if (socket -> options .domain == AWS_SOCKET_IPV6 ) {
827824 pton_err = inet_pton (AF_INET6 , local_endpoint -> address , & address .sock_addr_types .addr_in6 .sin6_addr );
828- address .sock_addr_types .addr_in6 .sin6_port = htons (local_endpoint -> port );
825+ address .sock_addr_types .addr_in6 .sin6_port = htons (( uint16_t ) local_endpoint -> port );
829826 address .sock_addr_types .addr_in6 .sin6_family = AF_INET6 ;
830827 sock_size = sizeof (address .sock_addr_types .addr_in6 );
831828 } else if (socket -> options .domain == AWS_SOCKET_LOCAL ) {
@@ -836,7 +833,7 @@ int aws_socket_bind(struct aws_socket *socket, const struct aws_socket_endpoint
836833 } else if (socket -> options .domain == AWS_SOCKET_VSOCK ) {
837834 pton_err = parse_cid (local_endpoint -> address , & address .sock_addr_types .vm_addr .svm_cid );
838835 address .sock_addr_types .vm_addr .svm_family = AF_VSOCK ;
839- address .sock_addr_types .vm_addr .svm_port = ( unsigned int ) local_endpoint -> port ;
836+ address .sock_addr_types .vm_addr .svm_port = local_endpoint -> port ;
840837 sock_size = sizeof (address .sock_addr_types .vm_addr );
841838#endif
842839 } else {
@@ -848,11 +845,11 @@ int aws_socket_bind(struct aws_socket *socket, const struct aws_socket_endpoint
848845 int errno_value = errno ; /* Always cache errno before potential side-effect */
849846 AWS_LOGF_ERROR (
850847 AWS_LS_IO_SOCKET ,
851- "id=%p fd=%d: failed to parse address %s:%d ." ,
848+ "id=%p fd=%d: failed to parse address %s:%u ." ,
852849 (void * )socket ,
853850 socket -> io_handle .data .fd ,
854851 local_endpoint -> address ,
855- ( int ) local_endpoint -> port );
852+ local_endpoint -> port );
856853 return aws_raise_error (s_convert_pton_error (pton_err , errno_value ));
857854 }
858855
@@ -882,7 +879,7 @@ int aws_socket_bind(struct aws_socket *socket, const struct aws_socket_endpoint
882879
883880 AWS_LOGF_DEBUG (
884881 AWS_LS_IO_SOCKET ,
885- "id=%p fd=%d: successfully bound to %s:%d " ,
882+ "id=%p fd=%d: successfully bound to %s:%u " ,
886883 (void * )socket ,
887884 socket -> io_handle .data .fd ,
888885 socket -> local_endpoint .address ,
@@ -996,7 +993,7 @@ static void s_socket_accept_event(
996993
997994 new_sock -> local_endpoint = socket -> local_endpoint ;
998995 new_sock -> state = CONNECTED_READ | CONNECTED_WRITE ;
999- uint16_t port = 0 ;
996+ uint32_t port = 0 ;
1000997
1001998 /* get the info on the incoming socket's address */
1002999 if (in_addr .ss_family == AF_INET ) {
0 commit comments