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 <libnl++/Message.h>
20 
21 namespace android::nl {
22 
23 /**
24  * In-place message mutator.
25  *
26  * Useful for making small changes (such as adjusting const-sized attributes or struct fields)
27  * efficiently and in-place. However, if you need to rebuild the message (e.g. to modify variable
28  * sized attributes or add/remove them), you need to use MessageFactory instead.
29  */
30 class MessageMutator {
31   public:
32     /**
33      * Construct message mutator object from editable buffer.
34      */
35     MessageMutator(nlmsghdr* buffer, size_t totalLen);
36 
37     nlmsghdr* operator->() const;
38     operator Buffer<nlmsghdr>() const;
39 
40     /**
41      * Read current attribute value.
42      *
43      * \param Read-only attribute buffer.
44      * \returns Attribute value.
45      */
46     uint64_t read(Buffer<nlattr> attr) const;
47 
48     /**
49      * Write new attribute value.
50      *
51      * \param Read-only attribute buffer.
52      * \param val New value to set.
53      */
54     void write(Buffer<nlattr> attr, uint64_t val) const;
55 
56   private:
57     const Buffer<nlmsghdr> mConstBuffer;
58     nlmsghdr* mMutableBuffer;
59 };
60 
61 }  // namespace android::nl
62