All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Stancek <jstancek@redhat.com>
To: ltp-list@lists.sourceforge.net
Subject: [LTP] (no subject)
Date: Mon, 10 Mar 2014 15:22:34 +0100	[thread overview]
Message-ID: <2618bd2ff004c79412ab2045f9a1d6c9f8afccec.1394460184.git.jstancek@redhat.com> (raw)
In-Reply-To: <b187cebc670c5e8d23f2405d95f82405a0416095.1394190599.git.jstancek@redhat.com>

From: Jan Stancek <jstancek@redhat.com>
Date: Fri, 7 Mar 2014 10:08:30 +0100
Subject: [PATCH v2 1/2] add tst_get_unused_port()

Returns unused port number for specified domain/type.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 include/test.h |    7 +++++
 lib/tst_net.c  |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+), 0 deletions(-)
 create mode 100644 lib/tst_net.c

Changes in v2:
- use sockaddr_storage as suggested by Mike

diff --git a/include/test.h b/include/test.h
index 81fca3e..49a0f47 100644
--- a/include/test.h
+++ b/include/test.h
@@ -288,6 +288,13 @@ int tst_fill_file(const char *path, char pattern, size_t bs, size_t bcount);
 uid_t tst_get_unused_uid(void);
 gid_t tst_get_unused_gid(void);
 
+/* lib/tst_net.c
+ *
+ * Return unused port
+ */
+unsigned short tst_get_unused_port(unsigned short family, int type,
+	void (cleanup_fn)(void));
+
 #ifdef TST_USE_COMPAT16_SYSCALL
 #define TCID_BIT_SUFFIX "_16"
 #elif  TST_USE_NEWER64_SYSCALL
diff --git a/lib/tst_net.c b/lib/tst_net.c
new file mode 100644
index 0000000..3391dee
--- /dev/null
+++ b/lib/tst_net.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2014 Linux Test Project, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Further, this software is distributed without any warranty that it
+ * is free of the rightful claim of any third person regarding
+ * infringement or the like.  Any license provided herein, whether
+ * implied or otherwise, applies only to this software file.  Patent
+ * licenses, if any, provided herein do not apply to combinations of
+ * this program with other software, or any other product whatsoever.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation, Inc.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include "test.h"
+
+unsigned short tst_get_unused_port(unsigned short family, int type,
+	void (cleanup_fn)(void))
+{
+	int sock;
+	socklen_t slen;
+	struct sockaddr_storage _addr;
+	struct sockaddr *addr = (struct sockaddr *)&_addr;
+	struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
+	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
+
+	switch (family) {
+	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:
+		tst_brkm(TBROK, cleanup_fn,
+			"tst_get_unused_port unknown family");
+	}
+
+	sock = socket(addr->sa_family, type, 0);
+	if (sock < 0)
+		tst_brkm(TBROK | TERRNO, cleanup_fn, "socket failed");
+
+	if (bind(sock, addr, slen) < 0)
+		tst_brkm(TBROK | TERRNO, cleanup_fn, "bind failed");
+
+	if (getsockname(sock, addr, &slen) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup_fn, "getsockname failed");
+
+	if (close(sock) == -1)
+		tst_brkm(TBROK | TERRNO, cleanup_fn, "close failed");
+
+	switch (family) {
+	case AF_INET:
+		return addr4->sin_port;
+	case AF_INET6:
+		return addr6->sin6_port;
+	default:
+		return -1;
+	}
+}
-- 
1.7.1


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

  parent reply	other threads:[~2014-03-10 14:22 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-07 11:39 [LTP] [PATCH 0/2] do not pick free/unused port by chance Jan Stancek
2014-03-07 11:39 ` [LTP] [PATCH 1/2] add tst_get_unused_port() Jan Stancek
2014-03-09 23:40   ` Mike Frysinger
2014-03-10 14:22   ` Jan Stancek [this message]
2014-03-10 14:28   ` [LTP] [PATCH v2 " Jan Stancek
2014-03-11 15:24     ` chrubis
2014-03-07 11:39 ` [LTP] [PATCH 2/2] do not pick free/unused port by chance Jan Stancek
     [not found] <tencent_048887541FB562D43FE2D104@qq.com>
2024-04-28  7:16 ` [LTP] (no subject) =?gb18030?B?wrfssw==?=
  -- strict thread matches above, loose matches on Subject: below --
2024-04-19  7:07 [LTP] [PATCH v2] Add case about arch_prctl syscall lufei
2024-04-21  7:15 ` [LTP] (no subject) lufei
2024-04-26  8:36   ` Cyril Hrubis
2024-04-26  9:42     ` 路斐
2024-04-26 10:28       ` Cyril Hrubis
2024-04-26 12:27         ` 路斐
2024-04-26 12:47           ` Jan Stancek
2024-04-28  7:44 ` lufei
2021-11-15  8:15 Joerg Vehlow
2019-10-07  6:45 Joerg Vehlow
2019-10-07  6:53 ` Joerg Vehlow
2019-08-21  2:25 Jim Woo
2019-08-22 12:45 ` Cyril Hrubis
2013-08-05 14:44 Stanislav Kholmanskikh
2009-07-01 20:08 Henry Yei
2009-06-24 22:58 Henry Yei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2618bd2ff004c79412ab2045f9a1d6c9f8afccec.1394460184.git.jstancek@redhat.com \
    --to=jstancek@redhat.com \
    --cc=ltp-list@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.