Ruslan Osmanov avatar Ruslan Osmanov committed c4e69f6

Add: EventListener::getSocketName method
Preparing for 1.5.0 stable

Comments (0)

Files changed (8)

classes/event_util.c

 	zval             *zport    = NULL;
 	evutil_socket_t   fd;
 
-	php_sockaddr_storage  sa_storage;
-	struct sockaddr      *sa         = (struct sockaddr *) &sa_storage;
-	socklen_t             sa_len     = sizeof(php_sockaddr_storage);
-	long                  port       = -1;
-
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zz|z",
 				&ppzfd, &zaddress, &zport) == FAILURE) {
 		return;
 		RETURN_FALSE;
 	}
 
-	if (getsockname(fd, sa, &sa_len)) {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING,
-				"Unable to retreive socket name, errno: %d", errno);
+	if (_php_event_getsockname(fd, &zaddress, &zport TSRMLS_CC) == FAILURE) {
 		RETURN_FALSE;
 	}
 
-	switch (sa->sa_family) {
-		case AF_INET:
-			{
-				struct sockaddr_in *sin = (struct sockaddr_in *) sa;
-				char addr[INET_ADDRSTRLEN + 1];
-
-				if (evutil_inet_ntop(sa->sa_family, &sin->sin_addr,
-							(void *) &addr, sizeof(addr))) {
-					zval_dtor(zaddress);
-					ZVAL_STRING(zaddress, addr, 1);
-
-					if (zport != NULL) {
-						port = ntohs(sin->sin_port);
-					}
-				}
-			}
-			break;
-#if HAVE_IPV6
-		case AF_INET6:
-			{
-				struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
-				char addr6[INET6_ADDRSTRLEN + 1];
-
-				if (evutil_inet_ntop(sa->sa_family, &sin6->sin6_addr,
-							(void *) &addr6, sizeof(addr6))) {
-					zval_dtor(zaddress);
-					ZVAL_STRING(zaddress, addr6, 1);
-
-					if (zport != NULL) {
-						port = ntohs(sin6->sin6_port);
-					}
-				}
-			}
-			break;
-#endif
-#ifdef AF_UNIX
-		case AF_UNIX:
-			{
-				struct sockaddr_un *ua = (struct sockaddr_un *) sa;
-
-				zval_dtor(zaddress);
-				ZVAL_STRING(zaddress, ua->sun_path, 1);
-			}
-			break;
-#endif
-		default:
-			php_error_docref(NULL TSRMLS_CC, E_WARNING,
-					"Unsupported address family: %d", sa->sa_family);
-			RETURN_FALSE;
-	}
-
-	if (port != -1) {
-		zval_dtor(zport);
-		ZVAL_LONG(zport, port);
-	}
-
 	RETVAL_TRUE;
 }
 /* }}} */

classes/listener.c

 /* }}} */
 #endif
 
+/* {{{ proto bool EventListener::getSocketName(string &address[, int &port]);
+ * Retreives the current address to which the listener's socket is bound.
+ * Returns &true; on success. Otherwise &false;.*/
+PHP_METHOD(EventListener, getSocketName)
+{
+	php_event_listener_t  *l;
+	zval                  *zlistener = getThis();
+	zval                  *zaddress;
+	zval                  *zport     = NULL;
+	evutil_socket_t        fd;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z",
+				&zaddress, &zport) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_LISTENER(l, zlistener);
+
+	fd = evconnlistener_get_fd(l->listener);
+	if (fd <= 0) {
+		RETURN_FALSE;
+	}
+
+	if (_php_event_getsockname(fd, &zaddress, &zport TSRMLS_CC) == FAILURE) {
+		RETURN_FALSE;
+	}
+	RETVAL_TRUE;
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
     <email>osmanov@php.net</email>
     <active>yes</active>
   </lead>
-  <date>2013-03-19</date>
+  <date>2013-03-20</date>
   <!--{{{ Current version -->
   <version>
     <release>1.5.0</release>
     <api>1.5.0</api>
   </version>
   <stability>
-    <release>beta</release>
-    <api>beta</api>
+    <release>stable</release>
+    <api>stable</api>
   </stability>
   <license uri="http://www.php.net/license">PHP</license>
   <notes><![CDATA[
   Del: EventBufferPosition class removed, EventBuffer::search now accepts and returns integers
   Add: EventBuffer::searchEol method
   Add: EventUtil::getSocketName method
+  Add: EventListener::getSocketName method
   ]]></notes>
   <!--}}}-->
   <!--{{{ Contents -->
   </extsrcrelease>
   <!--{{{ changelog-->
   <changelog>
-    <!--{{{ 1.5.0-beta -->
+    <!--{{{ 1.5.0 -->
     <release>
       <version>
         <release>1.5.0</release>
         <api>1.5.0</api>
       </version>
       <stability>
-        <release>beta</release>
-        <api>beta</api>
+        <release>stable</release>
+        <api>stable</api>
       </stability>
       <license uri="http://www.php.net/license">PHP</license>
       <notes><![CDATA[
         Del: EventBufferPosition class removed, EventBuffer::search now accepts and returns integers
         Add: EventBuffer::searchEol method
         Add: EventUtil::getSocketName method
+        Add: EventListener::getSocketName method
         ]]></notes>
     </release>
     <!--}}}-->
 #ifndef PHP_EVENT_H
 #define PHP_EVENT_H
 
-#define PHP_EVENT_VERSION "1.5.0-beta"
+#define PHP_EVENT_VERSION "1.5.0"
 
 
 extern zend_module_entry event_module_entry;
 	ZEND_ARG_INFO(0, cb)
 ZEND_END_ARG_INFO();
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_evconnlistener_get_fd, 0, 0, 1)
+	ZEND_ARG_INFO(1, address)
+	ZEND_ARG_INFO(1, port)
+ZEND_END_ARG_INFO();
+
 
 /* ARGINFO for extra API END }}} */
 #endif
 	PHP_ME(EventListener, disable,          arginfo_event__void,                 ZEND_ACC_PUBLIC)
 	PHP_ME(EventListener, setCallback,      arginfo_evconnlistener_set_cb,       ZEND_ACC_PUBLIC)
 	PHP_ME(EventListener, setErrorCallback, arginfo_evconnlistener_set_error_cb, ZEND_ACC_PUBLIC)
+	PHP_ME(EventListener, getSocketName,    arginfo_evconnlistener_get_fd,       ZEND_ACC_PUBLIC)
 #if LIBEVENT_VERSION_NUMBER >= 0x02000300
 	PHP_ME(EventListener, getBase, arginfo_event__void, ZEND_ACC_PUBLIC)
 #endif
 PHP_METHOD(EventListener, disable);
 PHP_METHOD(EventListener, setCallback);
 PHP_METHOD(EventListener, setErrorCallback);
+PHP_METHOD(EventListener, getSocketName);
 #if LIBEVENT_VERSION_NUMBER >= 0x02000300
 PHP_METHOD(EventListener, getBase);
 #endif
 }
 /* }}} */
 
+/* {{{ _php_event_getsockname */
+int _php_event_getsockname(evutil_socket_t fd, zval **ppzaddress, zval **ppzport TSRMLS_CC)
+{
+	php_sockaddr_storage  sa_storage;
+	struct sockaddr      *sa         = (struct sockaddr *) &sa_storage;
+	socklen_t             sa_len     = sizeof(php_sockaddr_storage);
+	long                  port       = -1;
+
+	if (getsockname(fd, sa, &sa_len)) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING,
+				"Unable to retreive socket name, errno: %d", errno);
+		return FAILURE;
+	}
+
+	switch (sa->sa_family) {
+		case AF_INET:
+			{
+				struct sockaddr_in *sin = (struct sockaddr_in *) sa;
+				char addr[INET_ADDRSTRLEN + 1];
+
+				if (evutil_inet_ntop(sa->sa_family, &sin->sin_addr,
+							(void *) &addr, sizeof(addr))) {
+					if (*ppzaddress) {
+						zval_dtor(*ppzaddress);
+					}
+					ZVAL_STRING(*ppzaddress, addr, 1);
+
+					if (*ppzport != NULL) {
+						port = ntohs(sin->sin_port);
+					}
+				}
+			}
+			break;
+#if HAVE_IPV6
+		case AF_INET6:
+			{
+				struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
+				char addr6[INET6_ADDRSTRLEN + 1];
+
+				if (evutil_inet_ntop(sa->sa_family, &sin6->sin6_addr,
+							(void *) &addr6, sizeof(addr6))) {
+					if (*ppzaddress) {
+						zval_dtor(*ppzaddress);
+					}
+					ZVAL_STRING(*ppzaddress, addr6, 1);
+
+					if (*ppzport != NULL) {
+						port = ntohs(sin6->sin6_port);
+					}
+				}
+			}
+			break;
+#endif
+#ifdef AF_UNIX
+		case AF_UNIX:
+			{
+				struct sockaddr_un *ua = (struct sockaddr_un *) sa;
+
+				if (*ppzaddress) {
+					zval_dtor(*ppzaddress);
+				}
+				ZVAL_STRING(*ppzaddress, ua->sun_path, 1);
+			}
+			break;
+#endif
+		default:
+			php_error_docref(NULL TSRMLS_CC, E_WARNING,
+					"Unsupported address family: %d", sa->sa_family);
+			return FAILURE;
+	}
+
+	if (port != -1) {
+		if (*ppzport) {
+			zval_dtor(*ppzport);
+		}
+		ZVAL_LONG(*ppzport, port);
+	}
+
+	return SUCCESS;
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
 #define PHP_EVENT_UTIL_H
 
 php_socket_t php_event_zval_to_fd(zval **ppfd TSRMLS_DC);
+int _php_event_getsockname(evutil_socket_t fd, zval **ppzaddress, zval **ppzport TSRMLS_CC);
 
 #define php_event_is_pending(e) \
 	event_pending((e), EV_READ | EV_WRITE | EV_SIGNAL | EV_TIMEOUT, NULL)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.