The "bottom level" TI-RPC functions require a bound network socket.

This function is to avoid code duplication.

Signed-off-by: Stanislav Kholmanskikh <[email protected]>
---
 .../rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.c    |   52 ++++++++++++++++++++
 .../rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.h    |   13 +++++
 2 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.c 
b/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.c
index 68abbd8..7bc4e32 100644
--- a/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.c
+++ b/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.c
@@ -15,3 +15,55 @@
  * along with this program; if not, write the Free Software Foundation,
  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/ip.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+int bound_socket(int domain, int type)
+{
+       int sock;
+       struct sockaddr_storage addr;
+       struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
+       struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
+       socklen_t slen;
+
+       switch (domain) {
+       case AF_INET:
+               addr4->sin_family = AF_INET;
+               addr4->sin_port = 0;
+               addr4->sin_addr.s_addr = INADDR_ANY;
+               slen = sizeof(*addr4);
+               break;
+
+       case AF_INET6:
+               addr6->sin6_family = AF_INET6;
+               addr6->sin6_port = 0;
+               addr6->sin6_addr = in6addr_any;
+               slen = sizeof(*addr6);
+               break;
+
+       default:
+               errno = EAFNOSUPPORT;
+               return -1;
+       }
+
+       if ((type != SOCK_STREAM) && (type != SOCK_DGRAM)) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       sock = socket(domain, type, 0);
+       if (sock < 0)
+               return -1;
+
+       if (bind(sock, (struct sockaddr *)&addr, slen) < 0) {
+               close(sock);
+               return -1;
+       }
+
+       return sock;
+}
diff --git a/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.h 
b/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.h
index 68abbd8..1293f2a 100644
--- a/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.h
+++ b/testcases/network/rpc/rpc-tirpc/tests_pack/lib/librpc-tirpc.h
@@ -15,3 +15,16 @@
  * along with this program; if not, write the Free Software Foundation,
  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
+
+#ifndef __LIBRPC_TIRPC_H__
+#define __LIBRPC_TIRPC_H__
+
+/*
+ * Returns a network socket bound to an arbitrary port.
+ * domain - AF_INET or AF_INET6,
+ * type - SOCK_DGRAM, SOCK_STREAM
+ * Returns -1 if failed (with set errno)
+ */
+int bound_socket(int domain, int type);
+
+#endif /* __LIBRPC_TIRPC_H__ */
-- 
1.7.1


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to