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

Function ql_syscall_getsockname

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

Source from the content-addressed store, hash-verified

450
451
452def ql_syscall_getsockname(ql: Qiling, sockfd: int, addr: int, addrlenptr: int):
453 sock = get_opened_socket(ql.os, sockfd)
454
455 if isinstance(sock, int):
456 return sock
457
458 addrlen = ql.mem.read_ptr(addrlenptr) if addrlenptr else 0
459
460 abits = ql.arch.bits
461 endian = ql.arch.endian
462
463 sockname = sock.getsockname()
464 obj = None
465
466 if sock.family == AF_UNIX:
467 hpath = sockname
468 vpath = ql.os.path.host_to_virtual_path(hpath)
469
470 if addrlen:
471 # addrlen indicates the total obj size allowed to be written.
472 # that already includes the family field (2) and the path null
473 # terminator (1)
474 vpath = vpath[:addrlen - 2 - 1]
475
476 sockaddr_un = make_sockaddr_un(abits, endian, len(vpath) + 1)
477
478 obj = sockaddr_un()
479 obj.sun_family = AF_UNIX
480 obj.sun_path = vpath.encode() + b'\x00'
481
482 elif sock.family == AF_INET:
483 sockaddr_in = make_sockaddr_in(abits, endian)
484 host, port = sockname
485
486 obj = sockaddr_in()
487 obj.sin_family = AF_INET
488 obj.sin_port = htons(ql, port)
489 obj.sin_addr.s_addr = inet_aton(str(host))
490
491 elif sock.family == AF_INET6 and ql.os.ipv6:
492 sockaddr_in6 = make_sockaddr_in6(abits, endian)
493 host, port = sockname
494
495 obj = sockaddr_in6()
496 obj.sin6_family = AF_INET6
497 obj.sin6_port = htons(ql, port)
498 obj.sin6_addr.s6_addr = inet6_aton(str(host))
499
500 if obj:
501 objsize = obj.sizeof()
502
503 if objsize <= addrlen:
504 obj.save_to(ql.mem, addr)
505
506 if addrlenptr:
507 ql.mem.write_ptr(addrlenptr, objsize)
508
509 ql.log.debug("getsockname(%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
getsocknameMethod · 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