You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by sa...@apache.org on 2006/01/09 06:51:49 UTC

svn commit: r367214 - in /webservices/axis2/trunk/c: include/axis2_network_handler.h modules/util/network_handler.c

Author: samisa
Date: Sun Jan  8 21:51:33 2006
New Revision: 367214

URL: http://svn.apache.org/viewcvs?rev=367214&view=rev
Log:
Added network handling functions

Added:
    webservices/axis2/trunk/c/include/axis2_network_handler.h
    webservices/axis2/trunk/c/modules/util/network_handler.c

Added: webservices/axis2/trunk/c/include/axis2_network_handler.h
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/c/include/axis2_network_handler.h?rev=367214&view=auto
==============================================================================
--- webservices/axis2/trunk/c/include/axis2_network_handler.h (added)
+++ webservices/axis2/trunk/c/include/axis2_network_handler.h Sun Jan  8 21:51:33 2006
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain count copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef AXIS2_NETWORK_HANDLER_H
+#define AXIS2_NETWORK_HANDLER_H
+
+#include <axis2.h>
+#include <axis2_defines.h>
+#include <axis2_env.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+/**
+ * @defgroup axis2_network_handler Network Handler
+ * @ingroup axis2_util 
+ * @{
+ */
+
+/**
+ * open a socket for a given server
+ * @param server ip address or the fqn of the server
+ * @param port port of the service
+ * @return opened socket
+ */ 
+AXIS2_DECLARE(int) 
+axis2_network_handler_open_socket(axis2_env_t **env, char *server, int port);
+
+/**
+ * creates a server socket for a given port
+ * @param port port of the socket to be bound
+ * @return creates server socket
+ */ 
+AXIS2_DECLARE(int) 
+axis2_network_handler_create_server_socket(axis2_env_t **env, int port);
+
+/**
+ * closes a socket
+ * @param opened socket that need to be closed
+ * @return status code
+ */
+AXIS2_DECLARE(axis2_status_t) 
+axis2_network_handler_close_socket (axis2_env_t **env, int socket);
+
+/**
+ * used to set up socket options such as timeouts, non-blocking ..etc
+ * @param socket valid socket (obtained by socket() or similar callo
+ * @param option the name of the option
+ * @param value Value to be set
+ * @return status of the operations as axis2_status_t
+ */
+AXIS2_DECLARE(axis2_status_t)
+axis2_network_handler_set_sock_option(axis2_env_t **env, int socket, 
+						int option, int value);
+						
+/** @} */
+    
+#ifdef __cplusplus
+}
+#endif
+
+#endif                          /* AXIS2_NETWORK_HANDLER_H */

Added: webservices/axis2/trunk/c/modules/util/network_handler.c
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/c/modules/util/network_handler.c?rev=367214&view=auto
==============================================================================
--- webservices/axis2/trunk/c/modules/util/network_handler.c (added)
+++ webservices/axis2/trunk/c/modules/util/network_handler.c Sun Jan  8 21:51:33 2006
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <axis2_network_handler.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <fcntl.h>
+
+int AXIS2_CALL
+axis2_network_handler_open_socket(axis2_env_t **env, char *server, int port)
+{
+	int sock = -1;
+	struct sockaddr_in sock_addr;
+	
+    AXIS2_PARAM_CHECK((*env)->error, server, AXIS2_CRTICAL_FAILURE);
+	
+	if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+	{
+		AXIS2_ERROR_SET((*env)->error, AXIS2_ERROR_SOCKET_ERROR, AXIS2_FAILURE);
+		return -1;
+	}
+	
+    memset(&sock_addr,0,sizeof(sock_addr));
+    sock_addr.sin_family = AF_INET;
+    sock_addr.sin_addr.s_addr = inet_addr(server);
+
+    if (sock_addr.sin_addr.s_addr == (in_addr_t)-1)
+    {
+        /**
+         * server may be a host name
+         */
+        struct hostent* lphost = NULL;
+        lphost = gethostbyname(server);
+
+        if (NULL != lphost)
+            sock_addr.sin_addr.s_addr = ((struct in_addr*)lphost->h_addr)->s_addr;
+        else
+        {
+            AXIS2_ERROR_SET((*env)->error, AXIS2_ERROR_INVALID_ADDRESS, 
+						AXIS2_FAILURE);
+            return -1;
+        }
+    }
+
+    sock_addr.sin_port = htons((uint16_t)port);
+	/** Connect to server */
+    if (connect(sock, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0)
+    {
+        close(sock);
+		AXIS2_ERROR_SET((*env)->error, AXIS2_ERROR_SOCKET_ERROR, AXIS2_FAILURE);
+        return -1;
+    }
+	return sock;
+}
+
+int AXIS2_CALL
+axis2_network_handler_create_server_socket(axis2_env_t **env, int port)
+{
+	int sock = -1;
+	unsigned int i = 0;
+	struct sockaddr_in sock_addr;
+	sock = socket(AF_INET, SOCK_STREAM, 0);
+	if(sock < 0)
+	{
+		AXIS2_ERROR_SET((*env)->error, AXIS2_ERROR_SOCKET_ERROR, AXIS2_FAILURE);
+		return -1;
+	}
+	/** Address re-use */
+	i = 1;
+	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR ,&i, sizeof(int));
+	/** Exec behaviour */
+	fcntl(sock, F_SETFD, FD_CLOEXEC);
+	
+	
+    memset(&sock_addr,0,sizeof(sock_addr));
+
+    sock_addr.sin_family = AF_INET;
+    sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
+    sock_addr.sin_port = htons((uint16_t)port);
+
+    /* Bind the socket to our port number */
+    if (bind(sock, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0)
+    {
+        AXIS2_ERROR_SET((*env)->error, AXIS2_ERROR_SOCKET_BIND_FAILED, 
+						AXIS2_FAILURE);
+		return -1;
+	}
+	return sock;
+}
+
+axis2_status_t AXIS2_CALL
+axis2_network_handler_close_socket (axis2_env_t **env, int socket)
+{
+	if(socket < 0)
+	{
+		AXIS2_ERROR_SET((*env)->error, AXIS2_ERROR_INVALID_SOCKET,
+							AXIS2_FAILURE);
+		return AXIS2_FAILURE;
+	}
+	if(0 != close(socket))
+	{
+		return AXIS2_FAILURE;
+	}
+	return AXIS2_SUCCESS;
+}
+
+axis2_status_t AXIS2_CALL
+axis2_network_handler_set_sock_option(axis2_env_t **env, int socket, 
+						int option, int value)
+{
+	if(option == SO_RCVTIMEO || option == SO_SNDTIMEO)
+	{
+		struct timeval tv;
+		/* we deal with milliseconds */
+		tv.tv_sec = value/1000;
+		tv.tv_usec = (value%1000) * 1000;
+		setsockopt(socket, SOL_SOCKET, option, &tv, sizeof(tv));
+		return AXIS2_SUCCESS;
+	}
+	return AXIS2_FAILURE;    
+}