1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <linux/netlink.h> 20 21 namespace android::nl::impl { 22 23 // The following definitions are C++ equivalents of NLMSG_* macros from linux/netlink.h. 24 25 /** 26 * Equivalent to NLMSG_ALIGNTO. 27 */ 28 constexpr size_t alignto = NLMSG_ALIGNTO; 29 static_assert(NLMSG_ALIGNTO == NLA_ALIGNTO); 30 31 /** 32 * Equivalent to NLMSG_ALIGN(len). 33 */ align(size_t len)34constexpr size_t align(size_t len) { 35 return (len + alignto - 1) & ~(alignto - 1); 36 } 37 38 /** 39 * Equivalent to NLMSG_SPACE(len). 40 */ 41 template <typename H> space(size_t len)42constexpr size_t space(size_t len) { 43 return align(align(sizeof(H)) + len); 44 } 45 46 /** 47 * Equivalent to NLMSG_DATA(hdr) + NLMSG_ALIGN(offset). 48 */ 49 template <typename H, typename D> 50 constexpr D* data(H* header, size_t offset = 0) { 51 return reinterpret_cast<D*>(uintptr_t(header) + space<H>(offset)); 52 } 53 54 } // namespace android::nl::impl 55