MCPcopy Create free account
hub / github.com/qilingframework/qiling / ql_syscall_getpeername

Function ql_syscall_getpeername

qiling/os/posix/syscall/socket.py:514–573  ·  view source on GitHub ↗
(ql: Qiling, sockfd: int, addr: int, addrlenptr: int)

Source from the content-addressed store, hash-verified

512
513
514def ql_syscall_getpeername(ql: Qiling, sockfd: int, addr: int, addrlenptr: int):
515 sock = get_opened_socket(ql.os, sockfd)
516
517 if isinstance(sock, int):
518 return sock
519
520 addrlen = ql.mem.read_ptr(addrlenptr) if addrlenptr else 0
521
522 abits = ql.arch.bits
523 endian = ql.arch.endian
524
525 peername = sock.getpeername()
526 obj = None
527
528 if sock.family == AF_UNIX:
529 hpath = peername
530 vpath = ql.os.path.host_to_virtual_path(hpath)
531
532 if addrlen:
533 # addrlen indicates the total obj size allowed to be written.
534 # that already includes the family field (2) and the path null
535 # terminator (1)
536 vpath = vpath[:addrlen - 2 - 1]
537
538 sockaddr_un = make_sockaddr_un(abits, endian, len(vpath) + 1)
539
540 obj = sockaddr_un()
541 obj.sun_family = AF_UNIX
542 obj.sun_path = vpath.encode() + b'\x00'
543
544 elif sock.family == AF_INET:
545 sockaddr_in = make_sockaddr_in(abits, endian)
546 host, port = peername
547
548 obj = sockaddr_in()
549 obj.sin_family = AF_INET
550 obj.sin_port = htons(ql, port)
551 obj.sin_addr.s_addr = inet_aton(str(host))
552
553 elif sock.family == AF_INET6 and ql.os.ipv6:
554 sockaddr_in6 = make_sockaddr_in6(abits, endian)
555 host, port = peername
556
557 obj = sockaddr_in6()
558 obj.sin6_family = AF_INET6
559 obj.sin6_port = htons(ql, port)
560 obj.sin6_addr.s6_addr = inet6_aton(str(host))
561
562 if obj:
563 objsize = obj.sizeof()
564
565 if objsize <= addrlen:
566 obj.save_to(ql.mem, addr)
567
568 if addrlenptr:
569 ql.mem.write_ptr(addrlenptr, objsize)
570
571 ql.log.debug("getpeername(%d, %#x, %#x) = %d" % (sockfd, addr, addrlenptr, 0))

Callers

nothing calls this directly

Calls 15

get_opened_socketFunction · 0.85
make_sockaddr_unFunction · 0.85
sockaddr_unClass · 0.85
htonsFunction · 0.85
inet_atonFunction · 0.85
inet6_atonFunction · 0.85
read_ptrMethod · 0.80
getpeernameMethod · 0.80
host_to_virtual_pathMethod · 0.80
sizeofMethod · 0.80
save_toMethod · 0.80
write_ptrMethod · 0.80

Tested by

no test coverage detected