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

Function ql_syscall_bind

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

Source from the content-addressed store, hash-verified

385
386
387def ql_syscall_bind(ql: Qiling, sockfd: int, addr: int, addrlen: int):
388 sock = get_opened_socket(ql.os, sockfd)
389
390 if isinstance(sock, int):
391 return sock
392
393 data = ql.mem.read(addr, addrlen)
394
395 abits = ql.arch.bits
396 endian = ql.arch.endian
397
398 sockaddr = make_sockaddr(abits, endian)
399 sockaddr_obj = sockaddr.from_buffer(data)
400
401 sa_family = sockaddr_obj.sa_family
402
403 if sa_family == AF_UNIX:
404 hpath, vpath = ql_unix_socket_path(ql, data[2:])
405
406 ql.log.debug(f'Binding socket to "{vpath}"')
407 dest = hpath
408
409 elif sa_family == AF_INET:
410 sockaddr = make_sockaddr_in(abits, endian)
411 sockaddr_obj = sockaddr.from_buffer(data)
412
413 port = ntohs(ql, sockaddr_obj.sin_port)
414 host = inet_ntoa(sockaddr_obj.sin_addr.s_addr)
415
416 if ql.os.bindtolocalhost:
417 host = '127.0.0.1'
418
419 if not ql.os.root and port <= 1024:
420 port = port + 8000
421
422 ql.log.debug(f'Binding socket to {host}:{port}')
423 dest = (host, port)
424
425 elif sa_family == AF_INET6 and ql.os.ipv6:
426 sockaddr_in6 = make_sockaddr_in6(abits, endian)
427 sockaddr_obj = sockaddr_in6.from_buffer(data)
428
429 port = ntohs(ql, sockaddr_obj.sin6_port)
430 host = inet6_ntoa(sockaddr_obj.sin6_addr.s6_addr)
431
432 if ql.os.bindtolocalhost:
433 host = '::1'
434
435 if not ql.os.root and port <= 1024:
436 port = port + 8000
437
438 ql.log.debug(f'Binding socket to {host}:{port}')
439 dest = (host, port)
440
441 else:
442 return -EAFNOSUPPORT
443
444 try:

Callers

nothing calls this directly

Calls 10

get_opened_socketFunction · 0.85
make_sockaddrFunction · 0.85
ql_unix_socket_pathFunction · 0.85
ntohsFunction · 0.85
inet_ntoaFunction · 0.85
inet6_ntoaFunction · 0.85
bindMethod · 0.80
make_sockaddr_inFunction · 0.50
make_sockaddr_in6Function · 0.50
readMethod · 0.45

Tested by

no test coverage detected