4343#define DBG_LVL DBG_INFO
4444#include <rtdbg.h>
4545
46- #define SOCKET_TABLE_STEP_LEN 4
47-
4846/* the socket table used to dynamic allocate sockets */
4947struct sal_socket_table
5048{
@@ -434,31 +432,51 @@ int sal_netdev_cleanup(struct netdev *netdev)
434432 * -1 : input the wrong family
435433 * -2 : input the wrong socket type
436434 * -3 : get network interface failed
435+ * -4 : invalid protocol or combo
437436 */
438437static int socket_init (int family , int type , int protocol , struct sal_socket * * res )
439438{
440-
441439 struct sal_socket * sock ;
442440 struct sal_proto_family * pf ;
443441 struct netdev * netdv_def = netdev_default ;
444442 struct netdev * netdev = RT_NULL ;
445443 rt_bool_t flag = RT_FALSE ;
446444
445+ /* Existing range checks for family and type */
447446 if (family < 0 || family > AF_MAX )
448447 {
448+ LOG_E ("Invalid family: %d (must be 0 ~ %d)" , family , AF_MAX );
449449 return -1 ;
450450 }
451451
452452 if (type < 0 || type > SOCK_MAX )
453453 {
454+ LOG_E ("Invalid type: %d (must be 0 ~ %d)" , type , SOCK_MAX );
454455 return -2 ;
455456 }
456457
458+ /* Range check for protocol */
459+ if (!VALID_PROTOCOL (protocol ))
460+ {
461+ LOG_E ("Invalid protocol: %d (must be 0 ~ %d)" , protocol , IPPROTO_RAW );
462+ rt_set_errno (EINVAL );
463+ return -4 ;
464+ }
465+
457466 sock = * res ;
458467 sock -> domain = family ;
459468 sock -> type = type ;
460469 sock -> protocol = protocol ;
461470
471+ /* Combo compatibility check */
472+ if (!VALID_COMBO (family , type , protocol ))
473+ {
474+ LOG_E ("Invalid combo: domain=%d, type=%d, protocol=%d" , family , type , protocol );
475+ rt_set_errno (EINVAL );
476+ return -4 ;
477+ }
478+
479+ /* Existing netdev selection logic */
462480 if (netdv_def && netdev_is_up (netdv_def ))
463481 {
464482 /* check default network interface device protocol family */
@@ -483,6 +501,8 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r
483501 sock -> netdev = netdev ;
484502 }
485503
504+ LOG_D ("Socket init success: domain=%d, type=%d, protocol=%d, netdev=%s" ,
505+ family , type , protocol , sock -> netdev ? sock -> netdev -> name : "default" );
486506 return 0 ;
487507}
488508
@@ -1051,7 +1071,7 @@ int sal_socket(int domain, int type, int protocol)
10511071 {
10521072 LOG_E ("SAL socket protocol family input failed, return error %d." , retval );
10531073 socket_delete (socket );
1054- return -1 ;
1074+ return retval ;
10551075 }
10561076
10571077 /* valid the network interface socket opreation */
0 commit comments