• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# Copyright 2022 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# Regenerate net_sys bindgen bindings.
7
8set -euo pipefail
9cd "$(dirname "${BASH_SOURCE[0]}")/.."
10
11source tools/impl/bindgen-common.sh
12
13# Replace definition of struct sockaddr with an import of libc::sockaddr.
14replace_sockaddr_libc() {
15    # Match the following structure definition across multiple lines:
16    #
17    # #[repr(C)]
18    # #[derive(Debug, Default, Copy, Clone)]
19    # pub struct sockaddr {
20    #     pub sa_family: sa_family_t,
21    #     pub sa_data: [::std::os::raw::c_char; 14usize],
22    # }
23    sed -E \
24        -e '1h;2,$H;$!d;g' \
25        -e 's/#\[repr\(C\)\]\n#\[derive\([^)]+\)\]\npub struct sockaddr \{\n(    pub [^\n]+\n)+\}/\nuse libc::sockaddr;\n/'
26}
27
28# Remove custom sa_family_t type in favor of libc::sa_family_t.
29remove_sa_family_t() {
30    grep -v "pub type sa_family_t = "
31}
32
33bindgen_generate \
34    --allowlist-type='ifreq' \
35    --allowlist-type='net_device_flags' \
36    --bitfield-enum='net_device_flags' \
37    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/if.h" \
38    | replace_linux_int_types \
39    | replace_sockaddr_libc \
40    | remove_sa_family_t \
41    | rustfmt \
42    > net_sys/src/iff.rs
43
44bindgen_generate \
45    --allowlist-type='sock_fprog' \
46    --allowlist-var='TUN_.*' \
47    "${BINDGEN_LINUX}/include/uapi/linux/if_tun.h" \
48    | replace_linux_int_types \
49    > net_sys/src/if_tun.rs
50
51bindgen_generate \
52    --allowlist-var='SIOC.*' \
53    "${BINDGEN_LINUX}/include/uapi/linux/sockios.h" \
54    | replace_linux_int_types \
55    > net_sys/src/sockios.rs
56