1 #include <sstream>
2 #include <string>
3 
4 /**
5  * @brief parse session input payload.
6  *
7  * This function retrives the session id and session handle from the session
8  * object path.
9  * A valid object path will be in the form
10  * "/xyz/openbmc_project/ipmi/session/channel/sessionId_sessionHandle"
11  *
12  * Ex: "/xyz/openbmc_project/ipmi/session/eth0/12a4567d_8a"
13  * SessionId    : 0X12a4567d
14  * SessionHandle: 0X8a
15 
16  * @param[in] objectPath - session object path
17  * @param[in] sessionId - retrived session id will be asigned.
18  * @param[in] sessionHandle - retrived session handle will be asigned.
19  *
20  * @return true if session id and session handle are retrived else returns
21  * false.
22  */
23 bool parseCloseSessionInputPayload(const std::string& objectPath,
24                                    uint32_t& sessionId, uint8_t& sessionHandle)
25 {
26     if (objectPath.empty())
27     {
28         return false;
29     }
30     // getting the position of session id and session handle string from
31     // object path.
32     std::size_t ptrPosition = objectPath.rfind("/");
33     uint16_t tempSessionHandle = 0;
34 
35     if (ptrPosition != std::string::npos)
36     {
37         // get the sessionid & session handle string from the session object
38         // path Ex: sessionIdString: "12a4567d_8a"
39         std::string sessionIdString = objectPath.substr(ptrPosition + 1);
40         std::size_t pos = sessionIdString.rfind("_");
41 
42         if (pos != std::string::npos)
43         {
44             // extracting the session handle
45             std::string sessionHandleString = sessionIdString.substr(pos + 1);
46             // extracting the session id
47             sessionIdString = sessionIdString.substr(0, pos);
48             // converting session id string  and session handle string to
49             // hexadecimal.
50             std::stringstream handle(sessionHandleString);
51             handle >> std::hex >> tempSessionHandle;
52             sessionHandle = tempSessionHandle & 0xFF;
53             std::stringstream idString(sessionIdString);
54             idString >> std::hex >> sessionId;
55             return true;
56         }
57     }
58     return false;
59 }
60 
61 /**
62  * @brief is session object matched.
63  *
64  * This function checks whether the objectPath contains reqSessionId and
65  * reqSessionHandle, e.g., "/xyz/openbmc_project/ipmi/session/eth0/12a4567d_8a"
66  * matches sessionId 0x12a4567d and sessionHandle 0x8a.
67  *
68  * @param[in] objectPath - session object path
69  * @param[in] reqSessionId - request session id
70  * @param[in] reqSessionHandle - request session handle
71  *
72  * @return true if the object is matched else return false
73  **/
74 bool isSessionObjectMatched(const std::string objectPath,
75                             const uint32_t reqSessionId,
76                             const uint8_t reqSessionHandle)
77 {
78     uint32_t sessionId = 0;
79     uint8_t sessionHandle = 0;
80 
81     if (parseCloseSessionInputPayload(objectPath, sessionId, sessionHandle))
82     {
83         return (reqSessionId == sessionId) ||
84                (reqSessionHandle == sessionHandle);
85     }
86 
87     return false;
88 }
89