pub struct NetInfoReport { /* private fields */ }

Implementations§

Examples found in repository?
src/client.rs (line 186)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    pub fn run(
        name: Option<&'static str>,
        sender: Sender<Option<String>>,
        ifname: Option<String>,
    ) -> Result<JoinHandle<Result<(), RnetmgrConError>>, RnetmgrConError> {
        let handler = std::thread::spawn(move || -> Result<(), RnetmgrConError> {
            let mut netinfo_hash = HashMap::<String, NetInfoReport>::new();
            let ih = Ipcon::new(name, Some(IPF_SND_IF | IPF_RCV_IF))
                .change_context(RnetmgrConError::IpconError)?;

            ih.join_group(ipcon::IPCON_KERNEL_NAME, ipcon::IPCON_KERNEL_GROUP_NAME)
                .change_context(RnetmgrConError::IpconError)?;

            let netif_str = ifname.as_deref().unwrap_or("eth0");
            let req_msg = NetInfoReqMessage::ReqAddress(netif_str.to_owned());

            let try_send_request = || -> Result<(), RnetmgrConError> {
                ih.join_group(NETINFO_IPCON, NETINFO_IPCON_GROUP)
                    .change_context(RnetmgrConError::IpconError)?;
                jdebug!("joined {}@{} group", NETINFO_IPCON_GROUP, NETINFO_IPCON);

                let buf = serde_json::to_string(&req_msg)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                let c_buf = CString::new(buf)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                jdebug!("send request: {:?}", req_msg);
                ih.send_unicast_msg(NETINFO_IPCON, c_buf.as_bytes())
                    .change_context(RnetmgrConError::IpconError)
            };

            let _ = try_send_request();
            loop {
                jdebug!("Wait message");
                let msg = ih
                    .receive_msg()
                    .change_context(RnetmgrConError::IpconError)?;

                match msg {
                    IpconMsg::IpconMsgKevent(kevent) => {
                        if let Some((p, g)) = kevent.group_added() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!(
                                    "{}@{} added, send request.",
                                    NETINFO_IPCON_GROUP,
                                    NETINFO_IPCON
                                );
                                let _ = try_send_request();
                            }

                            continue;
                        }

                        if let Some((p, g)) = kevent.group_removed() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!("{}@{} removed", NETINFO_IPCON_GROUP, NETINFO_IPCON);
                                netinfo_hash.clear();
                                sender
                                    .send(None)
                                    .into_report()
                                    .change_context(RnetmgrConError::IpconError)?;
                            }

                            continue;
                        }
                    }

                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeGroup) =>
                    {
                        let group = m.group.unwrap();
                        jdebug!(msg = "GroupMsg", peer = m.peer, group = group);

                        if (NETINFO_IPCON == m.peer) && (NETINFO_IPCON_GROUP == group) {
                            let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                            match c_str {
                                Ok(Ok(s)) => match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.del(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    _ => {}
                                },
                                Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                                Err(e) => jwarn!("Invalid string: {}", e),
                            }
                        }
                    }
                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeNormal)
                            && (NETINFO_IPCON == m.peer) =>
                    {
                        jdebug!(msg = "UsrMsg", peer = m.peer, buf_size = m.buf.len());

                        let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                        match c_str {
                            Ok(Ok(s)) => {
                                jdebug!("json: {}", s);
                                match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::NewAddress(addr)) => {
                                        jdebug!(msg = "NewAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr)) => {
                                        jdebug!(msg = "DelAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::NewLink(link)) => {
                                        jdebug!(msg = "NewLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::DelLink(link)) => {
                                        jdebug!(msg = "DelLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::NoInfo) => {
                                        jdebug!(msg = "NoInfo");
                                    }

                                    Ok(NetInfoMessage::InvalidReq) => {
                                        jdebug!(msg = "InvalidReq");
                                    }

                                    Err(e) => {
                                        jerror!("{:?}", e);
                                    }
                                }
                            }
                            Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                            Err(e) => jwarn!("Invalid string : {}", e),
                        }
                    }
                    _ => {
                        jdebug!("Unexpected message");
                    }
                }
            }
        });

        Ok(handler)
    }
Examples found in repository?
src/client.rs (line 189)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    pub fn run(
        name: Option<&'static str>,
        sender: Sender<Option<String>>,
        ifname: Option<String>,
    ) -> Result<JoinHandle<Result<(), RnetmgrConError>>, RnetmgrConError> {
        let handler = std::thread::spawn(move || -> Result<(), RnetmgrConError> {
            let mut netinfo_hash = HashMap::<String, NetInfoReport>::new();
            let ih = Ipcon::new(name, Some(IPF_SND_IF | IPF_RCV_IF))
                .change_context(RnetmgrConError::IpconError)?;

            ih.join_group(ipcon::IPCON_KERNEL_NAME, ipcon::IPCON_KERNEL_GROUP_NAME)
                .change_context(RnetmgrConError::IpconError)?;

            let netif_str = ifname.as_deref().unwrap_or("eth0");
            let req_msg = NetInfoReqMessage::ReqAddress(netif_str.to_owned());

            let try_send_request = || -> Result<(), RnetmgrConError> {
                ih.join_group(NETINFO_IPCON, NETINFO_IPCON_GROUP)
                    .change_context(RnetmgrConError::IpconError)?;
                jdebug!("joined {}@{} group", NETINFO_IPCON_GROUP, NETINFO_IPCON);

                let buf = serde_json::to_string(&req_msg)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                let c_buf = CString::new(buf)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                jdebug!("send request: {:?}", req_msg);
                ih.send_unicast_msg(NETINFO_IPCON, c_buf.as_bytes())
                    .change_context(RnetmgrConError::IpconError)
            };

            let _ = try_send_request();
            loop {
                jdebug!("Wait message");
                let msg = ih
                    .receive_msg()
                    .change_context(RnetmgrConError::IpconError)?;

                match msg {
                    IpconMsg::IpconMsgKevent(kevent) => {
                        if let Some((p, g)) = kevent.group_added() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!(
                                    "{}@{} added, send request.",
                                    NETINFO_IPCON_GROUP,
                                    NETINFO_IPCON
                                );
                                let _ = try_send_request();
                            }

                            continue;
                        }

                        if let Some((p, g)) = kevent.group_removed() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!("{}@{} removed", NETINFO_IPCON_GROUP, NETINFO_IPCON);
                                netinfo_hash.clear();
                                sender
                                    .send(None)
                                    .into_report()
                                    .change_context(RnetmgrConError::IpconError)?;
                            }

                            continue;
                        }
                    }

                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeGroup) =>
                    {
                        let group = m.group.unwrap();
                        jdebug!(msg = "GroupMsg", peer = m.peer, group = group);

                        if (NETINFO_IPCON == m.peer) && (NETINFO_IPCON_GROUP == group) {
                            let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                            match c_str {
                                Ok(Ok(s)) => match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.del(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    _ => {}
                                },
                                Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                                Err(e) => jwarn!("Invalid string: {}", e),
                            }
                        }
                    }
                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeNormal)
                            && (NETINFO_IPCON == m.peer) =>
                    {
                        jdebug!(msg = "UsrMsg", peer = m.peer, buf_size = m.buf.len());

                        let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                        match c_str {
                            Ok(Ok(s)) => {
                                jdebug!("json: {}", s);
                                match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::NewAddress(addr)) => {
                                        jdebug!(msg = "NewAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr)) => {
                                        jdebug!(msg = "DelAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::NewLink(link)) => {
                                        jdebug!(msg = "NewLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::DelLink(link)) => {
                                        jdebug!(msg = "DelLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::NoInfo) => {
                                        jdebug!(msg = "NoInfo");
                                    }

                                    Ok(NetInfoMessage::InvalidReq) => {
                                        jdebug!(msg = "InvalidReq");
                                    }

                                    Err(e) => {
                                        jerror!("{:?}", e);
                                    }
                                }
                            }
                            Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                            Err(e) => jwarn!("Invalid string : {}", e),
                        }
                    }
                    _ => {
                        jdebug!("Unexpected message");
                    }
                }
            }
        });

        Ok(handler)
    }
Examples found in repository?
src/client.rs (line 206)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    pub fn run(
        name: Option<&'static str>,
        sender: Sender<Option<String>>,
        ifname: Option<String>,
    ) -> Result<JoinHandle<Result<(), RnetmgrConError>>, RnetmgrConError> {
        let handler = std::thread::spawn(move || -> Result<(), RnetmgrConError> {
            let mut netinfo_hash = HashMap::<String, NetInfoReport>::new();
            let ih = Ipcon::new(name, Some(IPF_SND_IF | IPF_RCV_IF))
                .change_context(RnetmgrConError::IpconError)?;

            ih.join_group(ipcon::IPCON_KERNEL_NAME, ipcon::IPCON_KERNEL_GROUP_NAME)
                .change_context(RnetmgrConError::IpconError)?;

            let netif_str = ifname.as_deref().unwrap_or("eth0");
            let req_msg = NetInfoReqMessage::ReqAddress(netif_str.to_owned());

            let try_send_request = || -> Result<(), RnetmgrConError> {
                ih.join_group(NETINFO_IPCON, NETINFO_IPCON_GROUP)
                    .change_context(RnetmgrConError::IpconError)?;
                jdebug!("joined {}@{} group", NETINFO_IPCON_GROUP, NETINFO_IPCON);

                let buf = serde_json::to_string(&req_msg)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                let c_buf = CString::new(buf)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                jdebug!("send request: {:?}", req_msg);
                ih.send_unicast_msg(NETINFO_IPCON, c_buf.as_bytes())
                    .change_context(RnetmgrConError::IpconError)
            };

            let _ = try_send_request();
            loop {
                jdebug!("Wait message");
                let msg = ih
                    .receive_msg()
                    .change_context(RnetmgrConError::IpconError)?;

                match msg {
                    IpconMsg::IpconMsgKevent(kevent) => {
                        if let Some((p, g)) = kevent.group_added() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!(
                                    "{}@{} added, send request.",
                                    NETINFO_IPCON_GROUP,
                                    NETINFO_IPCON
                                );
                                let _ = try_send_request();
                            }

                            continue;
                        }

                        if let Some((p, g)) = kevent.group_removed() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!("{}@{} removed", NETINFO_IPCON_GROUP, NETINFO_IPCON);
                                netinfo_hash.clear();
                                sender
                                    .send(None)
                                    .into_report()
                                    .change_context(RnetmgrConError::IpconError)?;
                            }

                            continue;
                        }
                    }

                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeGroup) =>
                    {
                        let group = m.group.unwrap();
                        jdebug!(msg = "GroupMsg", peer = m.peer, group = group);

                        if (NETINFO_IPCON == m.peer) && (NETINFO_IPCON_GROUP == group) {
                            let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                            match c_str {
                                Ok(Ok(s)) => match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.del(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    _ => {}
                                },
                                Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                                Err(e) => jwarn!("Invalid string: {}", e),
                            }
                        }
                    }
                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeNormal)
                            && (NETINFO_IPCON == m.peer) =>
                    {
                        jdebug!(msg = "UsrMsg", peer = m.peer, buf_size = m.buf.len());

                        let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                        match c_str {
                            Ok(Ok(s)) => {
                                jdebug!("json: {}", s);
                                match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::NewAddress(addr)) => {
                                        jdebug!(msg = "NewAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr)) => {
                                        jdebug!(msg = "DelAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::NewLink(link)) => {
                                        jdebug!(msg = "NewLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::DelLink(link)) => {
                                        jdebug!(msg = "DelLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::NoInfo) => {
                                        jdebug!(msg = "NoInfo");
                                    }

                                    Ok(NetInfoMessage::InvalidReq) => {
                                        jdebug!(msg = "InvalidReq");
                                    }

                                    Err(e) => {
                                        jerror!("{:?}", e);
                                    }
                                }
                            }
                            Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                            Err(e) => jwarn!("Invalid string : {}", e),
                        }
                    }
                    _ => {
                        jdebug!("Unexpected message");
                    }
                }
            }
        });

        Ok(handler)
    }
Examples found in repository?
src/client.rs (line 192)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    pub fn run(
        name: Option<&'static str>,
        sender: Sender<Option<String>>,
        ifname: Option<String>,
    ) -> Result<JoinHandle<Result<(), RnetmgrConError>>, RnetmgrConError> {
        let handler = std::thread::spawn(move || -> Result<(), RnetmgrConError> {
            let mut netinfo_hash = HashMap::<String, NetInfoReport>::new();
            let ih = Ipcon::new(name, Some(IPF_SND_IF | IPF_RCV_IF))
                .change_context(RnetmgrConError::IpconError)?;

            ih.join_group(ipcon::IPCON_KERNEL_NAME, ipcon::IPCON_KERNEL_GROUP_NAME)
                .change_context(RnetmgrConError::IpconError)?;

            let netif_str = ifname.as_deref().unwrap_or("eth0");
            let req_msg = NetInfoReqMessage::ReqAddress(netif_str.to_owned());

            let try_send_request = || -> Result<(), RnetmgrConError> {
                ih.join_group(NETINFO_IPCON, NETINFO_IPCON_GROUP)
                    .change_context(RnetmgrConError::IpconError)?;
                jdebug!("joined {}@{} group", NETINFO_IPCON_GROUP, NETINFO_IPCON);

                let buf = serde_json::to_string(&req_msg)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                let c_buf = CString::new(buf)
                    .into_report()
                    .change_context(RnetmgrConError::InvalidData)?;

                jdebug!("send request: {:?}", req_msg);
                ih.send_unicast_msg(NETINFO_IPCON, c_buf.as_bytes())
                    .change_context(RnetmgrConError::IpconError)
            };

            let _ = try_send_request();
            loop {
                jdebug!("Wait message");
                let msg = ih
                    .receive_msg()
                    .change_context(RnetmgrConError::IpconError)?;

                match msg {
                    IpconMsg::IpconMsgKevent(kevent) => {
                        if let Some((p, g)) = kevent.group_added() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!(
                                    "{}@{} added, send request.",
                                    NETINFO_IPCON_GROUP,
                                    NETINFO_IPCON
                                );
                                let _ = try_send_request();
                            }

                            continue;
                        }

                        if let Some((p, g)) = kevent.group_removed() {
                            if (NETINFO_IPCON == p) && (NETINFO_IPCON_GROUP == g) {
                                jdebug!("{}@{} removed", NETINFO_IPCON_GROUP, NETINFO_IPCON);
                                netinfo_hash.clear();
                                sender
                                    .send(None)
                                    .into_report()
                                    .change_context(RnetmgrConError::IpconError)?;
                            }

                            continue;
                        }
                    }

                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeGroup) =>
                    {
                        let group = m.group.unwrap();
                        jdebug!(msg = "GroupMsg", peer = m.peer, group = group);

                        if (NETINFO_IPCON == m.peer) && (NETINFO_IPCON_GROUP == group) {
                            let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                            match c_str {
                                Ok(Ok(s)) => match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.del(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    _ => {}
                                },
                                Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                                Err(e) => jwarn!("Invalid string: {}", e),
                            }
                        }
                    }
                    IpconMsg::IpconMsgUser(m)
                        if matches!(m.msg_type, IpconMsgType::IpconMsgTypeNormal)
                            && (NETINFO_IPCON == m.peer) =>
                    {
                        jdebug!(msg = "UsrMsg", peer = m.peer, buf_size = m.buf.len());

                        let c_str = CStr::from_bytes_with_nul(&m.buf).map(|a| a.to_str());

                        match c_str {
                            Ok(Ok(s)) => {
                                jdebug!("json: {}", s);
                                match serde_json::from_str::<NetInfoMessage>(s) {
                                    Ok(NetInfoMessage::NewAddress(addr))
                                        if addr.ifname.eq(netif_str) =>
                                    {
                                        let nim = netinfo_hash
                                            .entry(netif_str.to_owned())
                                            .or_insert_with(|| {
                                                NetInfoReport::new(netif_str.to_owned())
                                            });

                                        nim.add(addr.ipv4addr);

                                        sender
                                            .send(nim.get())
                                            .into_report()
                                            .change_context(RnetmgrConError::IOError)?;
                                    }

                                    Ok(NetInfoMessage::NewAddress(addr)) => {
                                        jdebug!(msg = "NewAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::DelAddress(addr)) => {
                                        jdebug!(msg = "DelAddress", addr = format!("{:?}", addr));
                                    }

                                    Ok(NetInfoMessage::NewLink(link)) => {
                                        jdebug!(msg = "NewLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::DelLink(link)) => {
                                        jdebug!(msg = "DelLink", link = format!("{:?}", link));
                                    }

                                    Ok(NetInfoMessage::NoInfo) => {
                                        jdebug!(msg = "NoInfo");
                                    }

                                    Ok(NetInfoMessage::InvalidReq) => {
                                        jdebug!(msg = "InvalidReq");
                                    }

                                    Err(e) => {
                                        jerror!("{:?}", e);
                                    }
                                }
                            }
                            Ok(Err(e)) => jwarn!("Invalid C string : {}", e),
                            Err(e) => jwarn!("Invalid string : {}", e),
                        }
                    }
                    _ => {
                        jdebug!("Unexpected message");
                    }
                }
            }
        });

        Ok(handler)
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Set the foreground color generically Read more
Set the background color generically. Read more
Change the foreground color to black
Change the background color to black
Change the foreground color to red
Change the background color to red
Change the foreground color to green
Change the background color to green
Change the foreground color to yellow
Change the background color to yellow
Change the foreground color to blue
Change the background color to blue
Change the foreground color to magenta
Change the background color to magenta
Change the foreground color to purple
Change the background color to purple
Change the foreground color to cyan
Change the background color to cyan
Change the foreground color to white
Change the background color to white
Change the foreground color to the terminal default
Change the background color to the terminal default
Change the foreground color to bright black
Change the background color to bright black
Change the foreground color to bright red
Change the background color to bright red
Change the foreground color to bright green
Change the background color to bright green
Change the foreground color to bright yellow
Change the background color to bright yellow
Change the foreground color to bright blue
Change the background color to bright blue
Change the foreground color to bright magenta
Change the background color to bright magenta
Change the foreground color to bright purple
Change the background color to bright purple
Change the foreground color to bright cyan
Change the background color to bright cyan
Change the foreground color to bright white
Change the background color to bright white
Make the text bold
Make the text dim
Make the text italicized
Make the text italicized
Make the text blink
Make the text blink (but fast!)
Swap the foreground and background colors
Hide the text
Cross out the text
Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Set the foreground color to a specific RGB value.
Set the background color to a specific RGB value.
Sets the foreground color to an RGB value.
Sets the background color to an RGB value.
Apply a runtime-determined style
Apply a given transformation function to all formatters if the given stream supports at least basic ANSI colors, allowing you to conditionally apply given styles/colors. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more