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