addr: Extract 'new-address-message->address'. * ip/addr.scm (new-address-message->address): New procedure. (get-addrs): Use it, and use 'filter-map' instead of 'filter' followed by 'map'. Signed-off-by: Julien Lepiller <julien@lepiller.eu>
ip/addr.scm
183 | 183 | (close-port sock) | |
184 | 184 | (answer-ok? (last answer))))) | |
185 | 185 | ||
186 | + | (define (new-address-message->address msg) | |
187 | + | "If MSG has type 'RTM_NEWADDR', return the corresponding <addr> object. | |
188 | + | Otherwise return #f." | |
189 | + | (and (eqv? (message-kind msg) RTM_NEWADDR) | |
190 | + | (let* ((data (message-data msg)) | |
191 | + | (attrs (addr-message-attrs data))) | |
192 | + | (make-addr (addr-message-family data) | |
193 | + | (addr-message-prefix-len data) | |
194 | + | (map int->ifa-flag | |
195 | + | (split-flags (logior (addr-message-flags data) | |
196 | + | (get-attr attrs IFA_FLAGS)))) | |
197 | + | (addr-message-scope data) | |
198 | + | (addr-message-index data) | |
199 | + | (get-attr attrs IFA_LABEL) | |
200 | + | (get-attr attrs IFA_ADDRESS) | |
201 | + | (get-attr attrs IFA_BROADCAST) | |
202 | + | (get-attr attrs IFA_CACHEINFO))))) | |
203 | + | ||
186 | 204 | (define (get-addrs) | |
187 | 205 | (define request-num (random 65535)) | |
188 | 206 | (define message | |
… | |||
195 | 213 | (let ((sock (connect-route))) | |
196 | 214 | (send-msg message sock) | |
197 | 215 | (let* ((answer (receive-and-decode-msg sock %default-route-decoder)) | |
198 | - | (addrs (filter | |
199 | - | (lambda (msg) (equal? (message-kind msg) RTM_NEWADDR)) | |
200 | - | answer)) | |
201 | - | (addrs (map | |
202 | - | (lambda (msg) | |
203 | - | (let* ((data (message-data msg)) | |
204 | - | (attrs (addr-message-attrs data))) | |
205 | - | (make-addr | |
206 | - | (addr-message-family data) | |
207 | - | (addr-message-prefix-len data) | |
208 | - | (map | |
209 | - | int->ifa-flag | |
210 | - | (split-flags (logior (addr-message-flags data) | |
211 | - | (get-attr attrs IFA_FLAGS)))) | |
212 | - | (addr-message-scope data) | |
213 | - | (addr-message-index data) | |
214 | - | (get-attr attrs IFA_LABEL) | |
215 | - | (get-attr attrs IFA_ADDRESS) | |
216 | - | (get-attr attrs IFA_BROADCAST) | |
217 | - | (get-attr attrs IFA_CACHEINFO)))) | |
218 | - | addrs))) | |
216 | + | (addrs (filter-map new-address-message->address answer))) | |
219 | 217 | (close-port sock) | |
220 | 218 | addrs))) | |
221 | 219 |