From f7440c020d6a15402e572324554ae1f7d9f3c12c Mon Sep 17 00:00:00 2001 From: Evan Klinger Date: Mon, 1 Nov 1999 01:07:51 +0000 Subject: [PATCH] (PHP getservby{name,port}) New functions. @- Added new getservby{name,port} functions. (Evan) --- ext/standard/basic_functions.c | 50 ++++++++++++++++++++++++++++++++-- ext/standard/basic_functions.h | 3 ++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 5b4f0ba49c..9fcc28f93a 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -28,6 +28,7 @@ #include #include #include +#include #if HAVE_UNISTD_H #include #endif @@ -208,7 +209,8 @@ function_entry basic_functions[] = { PHP_FE(mt_rand, NULL) PHP_FE(mt_srand, NULL) PHP_FE(mt_getrandmax, NULL) - + PHP_FE(getservbyname, NULL) + PHP_FE(getservbyport, NULL) PHP_FE(gethostbyaddr, NULL) PHP_FE(gethostbyname, NULL) PHP_FE(gethostbynamel, NULL) @@ -3107,10 +3109,54 @@ PHP_FUNCTION(array_reverse) } /* }}} */ +/* {{{ proto int getservbyname(string service, string protocol) + Returns port associated with service. protocol must be "tcp" or "udp". */ +PHP_FUNCTION(getservbyname) +{ + pval **name,**proto; + struct servent *serv; + + if(ARG_COUNT(ht) != 2 || getParametersEx(2,&name,&proto) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(name); + convert_to_string_ex(proto); + + serv = getservbyname((*name)->value.str.val,(*proto)->value.str.val); + + if(serv == NULL) + RETURN_FALSE; + + RETURN_LONG(ntohs(serv->s_port)); +} +/* }}} */ + +/* {{{ proto string getservbyport(int port, string protocol) + Returns service name associated with port. Protocol must be "tcp" or "udp". */ +PHP_FUNCTION(getservbyport) +{ + pval **port,**proto; + struct servent *serv; + + if(ARG_COUNT(ht) != 2 || getParametersEx(2,&port,&proto) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long_ex(port); + convert_to_string_ex(proto); + + serv = getservbyport(htons((*port)->value.lval),(*proto)->value.str.val); + + if(serv == NULL) + RETURN_FALSE; + + RETURN_STRING(serv->s_name,1); +} +/* }}} */ + /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: - */ + */ diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index a65848e647..f4c82fcaf1 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -133,6 +133,9 @@ PHP_FUNCTION(array_values); PHP_FUNCTION(array_count_values); PHP_FUNCTION(array_reverse); +PHP_FUNCTION(getservbyname); +PHP_FUNCTION(getservbyport); + #if HAVE_PUTENV typedef struct { char *putenv_string; -- 2.40.0