Commits

Ruslan Osmanov committed 727cee5

Change: Eventbufferevent's methods connect, connectHost changed arg set
Add: eventbufferevent::connectUnix

  • Participants
  • Parent commits c322691

Comments (0)

Files changed (5)

classes/buffer_event.c

 }
 /* }}} */
 
-/* {{{ proto bool EventBufferEvent::connect(string addr[, bool sync_resolve = FALSE[, int family = EventUtil::AF_UNSPEC]]);
+/* {{{ proto bool EventBufferEvent::connect(string addr);
  *
  * Connect buffer event's socket to given address(optionally with port).  The
  * function available since libevent 2.0.2-alpha.
  * the bufferevent, this function allocates a socket stream and makes it
  * non-blocking internally.
  *
- * If sync_resolve parameter is TRUE, the function tries to resolve the
- * hostname within addr *syncronously*(!).  Otherwise addr parameter expected
+ * addr parameter expected
  * to be an IP address with optional port number. Recognized formats are:
  *
  *    [IPv6Address]:port
 	char               *addr;
 	int                 addr_len;
 	struct sockaddr     sa;
-	struct sockaddr_un *sun;
 	socklen_t           sa_len;
-	zend_bool           sync_resolve = 0;
-	long                family       = AF_UNSPEC;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl",
-				&addr, &addr_len, &sync_resolve, &family) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
+				&addr, &addr_len) == FAILURE) {
 		return;
 	}
 
-	if (family & ~(AF_UNSPEC | AF_INET | AF_INET6 | AF_UNIX)) {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING,
-				"Unsupported address family: %ld", family);
-		RETURN_FALSE;
-	}
-
 	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
 	_ret_if_invalid_bevent_ptr(bev);
 
-	if (family == AF_UNIX) {
-		sun = (struct sockaddr_un *) &sa;
-
-		sun->sun_family = AF_UNIX;
-		strcpy(sun->sun_path, addr);
-
-		sa_len = sizeof(struct sockaddr_un);
-	} else if (sync_resolve) {
-		/* The PHP API *syncronously* resolves hostname, if it doesn't look
-		 * like IP(v4/v6) */
-		if (php_network_parse_network_address_with_port(addr, addr_len, &sa, &sa_len TSRMLS_CC) != SUCCESS) {
-			/* The function reports errors, if necessary */
-			RETURN_FALSE;
-		}
-	} else {
-		/* Numeric addresses only. Don't try to resolve hostname. */
-		if (evutil_parse_sockaddr_port(addr, &sa, (int *) &sa_len)) {
-			php_error_docref(NULL TSRMLS_CC, E_WARNING,
-					"Failed parsing address: the address is not well-formed, "
-					"or the port is out of range");
-			RETURN_FALSE;
-		}
+	/* Numeric addresses only. Don't try to resolve hostname. */
+	if (evutil_parse_sockaddr_port(addr, &sa, (int *) &sa_len)) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING,
+				"Failed parsing address: the address is not well-formed, "
+				"or the port is out of range");
+		RETURN_FALSE;
 	}
 
 	/* bufferevent_socket_connect() allocates a socket stream internally, if we
 }
 /* }}} */
 
+/* {{{ proto bool EventBufferEvent::connectUnix(string addr);
+ *
+ * Connect buffer event's file descriptor to given UNIX domain socket.
+ */
+PHP_METHOD(EventBufferEvent, connectUnix)
+{
+	php_event_bevent_t *bev;
+	zval               *zbevent      = getThis();
+	char               *addr;
+	int                 addr_len;
+	struct sockaddr_un  sun;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
+				&addr, &addr_len) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
+	_ret_if_invalid_bevent_ptr(bev);
+
+	sun.sun_family = AF_UNIX;
+	strcpy(sun.sun_path, addr);
+
+	if (bufferevent_socket_connect(bev->bevent, (struct sockaddr *) &sun, sizeof(struct sockaddr_un))) {
+		RETURN_FALSE;
+	}
+
+	RETVAL_TRUE;
+}
+/* }}} */
+
 /* {{{ proto bool EventBufferEvent::connectHost(EventDnsBase dns_base, string hostname, int port[, int family = EventUtil::AF_UNSPEC]);
  *
  * Resolves the DNS name hostname, looking for addresses of type

examples/unix-domain-listener-client.php

 		$this->bev = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE,
 			array ($this, "read_cb"), NULL, array ($this, "event_cb"));
 
-		if (!$this->bev->connect($sock_path, TRUE, EventUtil::AF_UNIX)) {
+		if (!$this->bev->connectUnix($sock_path)) {
 			trigger_error("Failed to connect to socket `$sock_path'", E_USER_ERROR);
 		}
 
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_bufferevent_connect, 0, 0, 1)
 	ZEND_ARG_INFO(0, addr)
-	ZEND_ARG_INFO(0, sync_resolve)
-	ZEND_ARG_INFO(0, family)
+ZEND_END_ARG_INFO();
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_bufferevent_connect_unix, 0, 0, 1)
+	ZEND_ARG_INFO(0, addr)
 ZEND_END_ARG_INFO();
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_bufferevent_socket_connect_hostname, 0, 0, 3)
 	PHP_ME(EventBufferEvent, __construct,        arginfo_bufferevent__construct,              ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
 	PHP_ME(EventBufferEvent, free,               arginfo_event__void,                         ZEND_ACC_PUBLIC)
 	PHP_ME(EventBufferEvent, connect,            arginfo_bufferevent_connect,                 ZEND_ACC_PUBLIC)
+	PHP_ME(EventBufferEvent, connectUnix,        arginfo_bufferevent_connect_unix,            ZEND_ACC_PUBLIC)
 	PHP_ME(EventBufferEvent, connectHost,        arginfo_bufferevent_socket_connect_hostname, ZEND_ACC_PUBLIC)
 	PHP_ME(EventBufferEvent, getDnsErrorString,  arginfo_event__void,                         ZEND_ACC_PUBLIC)
 	PHP_ME(EventBufferEvent, setCallbacks,       arginfo_bufferevent_set_callbacks,           ZEND_ACC_PUBLIC)
 PHP_METHOD(EventBufferEvent, free);
 PHP_METHOD(EventBufferEvent, createPair);
 PHP_METHOD(EventBufferEvent, connect);
+PHP_METHOD(EventBufferEvent, connectUnix);
 PHP_METHOD(EventBufferEvent, connectHost);
 PHP_METHOD(EventBufferEvent, setCallbacks);
 PHP_METHOD(EventBufferEvent, enable);

tests/04-bevent-socket.phpt

 $base = new EventBase();
 $bev = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE);
 
-if (!$bev->connect("www.php.net:80", TRUE)) {
+if (!$bev->connectHost(NULL, "www.php.net", 80, EventUtil::AF_INET)) {
 	exit("Connection failed\n");
 }