@@ -498,6 +498,25 @@ fn socket_inet_ntoa(packed_ip: PyBytesRef, vm: &VirtualMachine) -> PyResult {
498
498
Ok ( vm. ctx . new_str ( Ipv4Addr :: from ( ip_num) . to_string ( ) ) )
499
499
}
500
500
501
+ fn socket_getservbyname (
502
+ servicename : PyStrRef ,
503
+ protocolname : OptionalArg < PyStrRef > ,
504
+ vm : & VirtualMachine ,
505
+ ) -> PyResult {
506
+ use std:: ffi:: CString ;
507
+ let cstr_name = CString :: new ( servicename. borrow_value ( ) )
508
+ . map_err ( |_| vm. new_value_error ( "embedded null character" . to_owned ( ) ) ) ?;
509
+ let protocolname = protocolname. as_ref ( ) . map_or ( "" , |s| s. borrow_value ( ) ) ;
510
+ let cstr_proto = CString :: new ( protocolname)
511
+ . map_err ( |_| vm. new_value_error ( "embedded null character" . to_owned ( ) ) ) ?;
512
+ let serv = unsafe { c:: getservbyname ( cstr_name. as_ptr ( ) , cstr_proto. as_ptr ( ) ) } ;
513
+ if serv. is_null ( ) {
514
+ return Err ( vm. new_os_error ( "service/proto not found" . to_owned ( ) ) ) ;
515
+ }
516
+ let port = unsafe { ( * serv) . s_port } ;
517
+ Ok ( vm. ctx . new_int ( u16:: from_be ( port as u16 ) ) )
518
+ }
519
+
501
520
#[ derive( FromArgs ) ]
502
521
struct GAIOptions {
503
522
#[ pyarg( positional) ]
@@ -795,6 +814,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
795
814
"inet_ntop" => ctx. new_function( socket_inet_ntop) ,
796
815
"getprotobyname" => ctx. new_function( socket_getprotobyname) ,
797
816
"getnameinfo" => ctx. new_function( socket_getnameinfo) ,
817
+ "getservbyname" => ctx. new_function( socket_getservbyname) ,
798
818
// constants
799
819
"AF_UNSPEC" => ctx. new_int( 0 ) ,
800
820
"AF_INET" => ctx. new_int( c:: AF_INET ) ,
0 commit comments