1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   User Datagram Protocol (UDP)                                        */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "nx_ip.h"
29 #include "nx_udp.h"
30 
31 
32 /* Bring in externs for caller checking code.  */
33 
34 NX_CALLER_CHECKING_EXTERNS
35 
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _nxe_udp_socket_create                              PORTABLE C      */
42 /*                                                           6.1          */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Yuxin Zhou, Microsoft Corporation                                   */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function checks for errors in the UDP socket create            */
50 /*    function call.                                                      */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    ip_ptr                                IP instance pointer           */
55 /*    socket_ptr                            Pointer to new UDP socket     */
56 /*    name                                  Name of new UDP socket        */
57 /*    type_of_service                       Type of service for this UDP  */
58 /*                                            socket                      */
59 /*    fragment                              Flag to enable IP fragmenting */
60 /*    time_to_live                          Time to live value for socket */
61 /*    queue_maximum                         Maximum depth of receive queue*/
62 /*    udp_socket_size                       Size of UDP socket            */
63 /*                                                                        */
64 /*  OUTPUT                                                                */
65 /*                                                                        */
66 /*    status                                Completion status             */
67 /*                                                                        */
68 /*  CALLS                                                                 */
69 /*                                                                        */
70 /*    _nx_udp_socket_create                 Actual UDP socket create      */
71 /*                                            function                    */
72 /*    tx_mutex_get                          Get protection mutex          */
73 /*    tx_mutex_put                          Put protection mutex          */
74 /*                                                                        */
75 /*  CALLED BY                                                             */
76 /*                                                                        */
77 /*    Application Code                                                    */
78 /*                                                                        */
79 /*  RELEASE HISTORY                                                       */
80 /*                                                                        */
81 /*    DATE              NAME                      DESCRIPTION             */
82 /*                                                                        */
83 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
84 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
85 /*                                            resulting in version 6.1    */
86 /*                                                                        */
87 /**************************************************************************/
_nxe_udp_socket_create(NX_IP * ip_ptr,NX_UDP_SOCKET * socket_ptr,CHAR * name,ULONG type_of_service,ULONG fragment,UINT time_to_live,ULONG queue_maximum,UINT udp_socket_size)88 UINT  _nxe_udp_socket_create(NX_IP *ip_ptr, NX_UDP_SOCKET *socket_ptr, CHAR *name,
89                              ULONG type_of_service, ULONG fragment, UINT time_to_live,
90                              ULONG queue_maximum, UINT udp_socket_size)
91 {
92 
93 UINT           status;
94 NX_UDP_SOCKET *created_socket;
95 ULONG          created_count;
96 
97 
98     /* Check for invalid input pointers.  */
99     if ((ip_ptr == NX_NULL) || (ip_ptr -> nx_ip_id != NX_IP_ID) ||
100         (socket_ptr == NX_NULL) || (udp_socket_size != (UINT)sizeof(NX_UDP_SOCKET)))
101     {
102         return(NX_PTR_ERROR);
103     }
104 
105     /* Check for appropriate caller.  */
106     NX_INIT_AND_THREADS_CALLER_CHECKING
107 
108     /* Get protection mutex.  */
109     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
110 
111     /* Pickup created count and created socket pointer.  */
112     created_count =   ip_ptr -> nx_ip_udp_created_sockets_count;
113     created_socket =  ip_ptr -> nx_ip_udp_created_sockets_ptr;
114 
115     /* Loop to look for socket already created.  */
116     while (created_count--)
117     {
118 
119         /* Compare the new socket with the already created socket.  */
120         if (socket_ptr == created_socket)
121         {
122 
123             /* Error, socket already created!  */
124 
125             /* Release protection mutex.  */
126             tx_mutex_put(&(ip_ptr -> nx_ip_protection));
127 
128             /* Return error.  */
129             return(NX_PTR_ERROR);
130         }
131 
132         /* Move to next created socket.  */
133         created_socket =  created_socket -> nx_udp_socket_created_next;
134     }
135 
136     /* Release protection mutex.  */
137     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
138 
139     /* Check to see if UDP is enabled.  */
140     if (!ip_ptr -> nx_ip_udp_packet_receive)
141     {
142         return(NX_NOT_ENABLED);
143     }
144 
145     /* Check for valid type of service.  */
146     if (type_of_service & ~(NX_IP_TOS_MASK))
147     {
148         return(NX_OPTION_ERROR);
149     }
150 
151     /* Check for valid fragment option.  */
152     if ((fragment != NX_FRAGMENT_OKAY) &&
153         (fragment != NX_DONT_FRAGMENT))
154     {
155         return(NX_OPTION_ERROR);
156     }
157 
158     /* Check for valid time to live option.  */
159     if (((ULONG)time_to_live) > NX_IP_TIME_TO_LIVE_MASK)
160     {
161         return(NX_OPTION_ERROR);
162     }
163 
164     /* Call actual UDP socket create function.  */
165     status =  _nx_udp_socket_create(ip_ptr, socket_ptr, name, type_of_service,
166                                     fragment, time_to_live, queue_maximum);
167 
168     /* Return completion status.  */
169     return(status);
170 }
171 
172