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