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