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 /**   Internet Protocol (IP)                                              */
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 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_ip_interface_address_set                        PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function sets the IP address and the network mask for the      */
45 /*    supplied IP instance.                                               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                IP control block pointer      */
50 /*    interface_index                       IP Interface Index            */
51 /*    ip_address                            IP address                    */
52 /*    network_mask                          Network mask                  */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    status                                Completion status             */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    tx_mutex_get                          Get protection mutex          */
61 /*    tx_mutex_put                          Put protection mutex          */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  04-25-2022     Yuxin Zhou               Modified comment(s), and      */
75 /*                                            added internal ip address   */
76 /*                                            change notification,        */
77 /*                                            resulting in version 6.1.11 */
78 /*                                                                        */
79 /**************************************************************************/
_nx_ip_interface_address_set(NX_IP * ip_ptr,UINT interface_index,ULONG ip_address,ULONG network_mask)80 UINT  _nx_ip_interface_address_set(NX_IP *ip_ptr, UINT interface_index, ULONG ip_address, ULONG network_mask)
81 {
82 
83 #ifndef NX_DISABLE_IPV4
84 TX_INTERRUPT_SAVE_AREA
85 
86 VOID  (*address_change_notify)(NX_IP *, VOID *);
87 VOID *additional_info;
88 VOID  (*address_change_notify_internal)(NX_IP *, VOID *);
89 ULONG previous_ip_address;
90 ULONG previous_network_mask;
91 
92 
93     /* If trace is enabled, insert this event into the trace buffer.  */
94     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_ADDRESS_SET, ip_ptr, ip_address, network_mask, 0, NX_TRACE_IP_EVENTS, 0, 0);
95 
96 
97     /* Get mutex protection.  */
98     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
99 
100     /* Disable interrupts.  */
101     TX_DISABLE
102 
103     /* Save previous IP address and network mask.  */
104     previous_ip_address =    ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_address;
105     previous_network_mask =  ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_network_mask;
106 
107     /* Pickup the current notification callback and additional information pointers.  */
108     address_change_notify =  ip_ptr -> nx_ip_address_change_notify;
109     additional_info =        ip_ptr -> nx_ip_address_change_notify_additional_info;
110 
111     /* Pickup the internal notification callback.  */
112     address_change_notify_internal = ip_ptr -> nx_ip_address_change_notify_internal;
113 
114     /* Setup the IP address and the network mask. */
115     ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_address      =  ip_address;
116     ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_network_mask =  network_mask;
117     ip_ptr -> nx_ip_interface[interface_index].nx_interface_ip_network      =  ip_address & network_mask;
118 
119     /* Ensure the RARP function is disabled.  */
120     ip_ptr -> nx_ip_rarp_periodic_update =  NX_NULL;
121     ip_ptr -> nx_ip_rarp_queue_process =    NX_NULL;
122 
123     /* Restore interrupts.  */
124     TX_RESTORE
125 
126     /* Release mutex protection.  */
127     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
128 
129     /* Determine if the application should be notified of the IP address and/or
130        network mask change.  */
131     if ((address_change_notify) &&
132         ((ip_address != previous_ip_address) || (network_mask != previous_network_mask)))
133     {
134 
135         /* Yes, call the application's IP address change notify function.  */
136         (address_change_notify)(ip_ptr, additional_info);
137     }
138 
139     /* Determine if the internal application should be notified of the IP address and/or
140        network mask change.  */
141     if ((address_change_notify_internal) &&
142         ((ip_address != previous_ip_address) || (network_mask != previous_network_mask)))
143     {
144 
145         /* Yes, call the application's IP address change notify function.  */
146         (address_change_notify_internal)(ip_ptr, NX_NULL);
147     }
148 
149     /* Initialize the ARP defend timeout.  */
150     ip_ptr -> nx_ip_interface[interface_index].nx_interface_arp_defend_timeout = 0;
151 
152     /* Return completion status.  */
153     return(NX_SUCCESS);
154 #else /* NX_DISABLE_IPV4  */
155     NX_PARAMETER_NOT_USED(ip_ptr);
156     NX_PARAMETER_NOT_USED(interface_index);
157     NX_PARAMETER_NOT_USED(ip_address);
158     NX_PARAMETER_NOT_USED(network_mask);
159 
160     return(NX_NOT_SUPPORTED);
161 #endif /* !NX_DISABLE_IPV4  */
162 }
163 
164