MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / sys_getpeername

Function sys_getpeername

components/lwp/lwp_syscall.c:5253–5295  ·  view source on GitHub ↗

* @brief Retrieves the address of the peer connected to a socket. * * This system call retrieves the address of the peer (remote endpoint) that is connected to the specified * socket. The socket must be connected (i.e., for stream-oriented protocols such as TCP). The address * of the peer is stored in the `name` structure, and the size of the structure is updated in `namelen`. * * @param[in]

Source from the content-addressed store, hash-verified

5251 * Failing to provide a correctly sized `namelen` could result in buffer overflows or undefined behavior.
5252 */
5253sysret_t sys_getpeername(int socket, struct musl_sockaddr *name, socklen_t *namelen)
5254{
5255 int ret = -1;
5256 struct sockaddr sa;
5257 struct musl_sockaddr kname;
5258 socklen_t unamelen;
5259 socklen_t knamelen;
5260
5261 if (!lwp_user_accessable(namelen, sizeof(socklen_t)))
5262 {
5263 return -EFAULT;
5264 }
5265 lwp_get_from_user(&unamelen, namelen, sizeof(socklen_t));
5266 if (!unamelen)
5267 {
5268 return -EINVAL;
5269 }
5270
5271 if (!lwp_user_accessable(name, unamelen))
5272 {
5273 return -EFAULT;
5274 }
5275
5276 knamelen = sizeof(struct sockaddr);
5277 ret = getpeername(socket, &sa, &knamelen);
5278
5279 if (ret == 0)
5280 {
5281 sockaddr_tomusl(&sa, &kname);
5282 if (unamelen > sizeof(struct musl_sockaddr))
5283 {
5284 unamelen = sizeof(struct musl_sockaddr);
5285 }
5286 lwp_put_to_user(name, &kname, unamelen);
5287 lwp_put_to_user(namelen, &unamelen, sizeof(socklen_t));
5288 }
5289 else
5290 {
5291 ret = GET_ERRNO();
5292 }
5293
5294 return ret;
5295}
5296
5297/**
5298 * @brief Retrieves the local address of the socket.

Callers

nothing calls this directly

Calls 5

lwp_user_accessableFunction · 0.85
lwp_get_from_userFunction · 0.85
getpeernameFunction · 0.85
sockaddr_tomuslFunction · 0.85
lwp_put_to_userFunction · 0.85

Tested by

no test coverage detected