*
* \sa ipv6(7), getaddrinfo(3), bind(2), connect(2).
*/
-int makesock(unsigned l3type, unsigned l4type, int passive,
- const char *host, unsigned short port_number,
+int makesock(unsigned l4type, bool passive,
+ const char *host, uint16_t port_number,
struct flowopts *fo)
{
struct addrinfo *local = NULL, *src,
*remote = NULL, *dst, hints;
- int rc, on = 1, sockfd = -1,
+ unsigned int l3type = AF_UNSPEC;
+ int rc, sockfd = -1,
socktype = sock_type(l4type);
char port[6]; /* port number has at most 5 digits */
if (sockfd < 0)
goto get_next_dst;
- /*
- * Set those options that need to be set before establishing
- * the connection. Reuse the address on passive (listening)
- * sockets to avoid failure on restart.
- */
- if (passive && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
- &on, sizeof(on)) == -1) {
- PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
- strerror(errno));
- return -ERRNO_TO_PARA_ERROR(errno);
- }
flowopt_setopts(sockfd, fo);
if (src) {
/**
* Create a passive / listening socket.
*
- * \param l3type The network-layer type (\p AF_xxx).
* \param l4type The transport-layer type (\p IPPROTO_xxx).
* \param port The decimal port number to listen on.
+ * \param fo Flowopts (if any) to set before starting to listen.
*
* \return Positive integer (socket descriptor) on success, negative value
* otherwise.
*
* \sa makesock(), ip(7), ipv6(7), bind(2), listen(2).
*/
-int para_listen(unsigned l3type, unsigned l4type, unsigned short port)
+int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo)
{
- int ret, fd = makesock(l3type, l4type, 1, NULL, port, NULL);
+ int fd, ret;
+
+ if (fo == NULL)
+ fo = flowopt_new();
+
+ /* Reuse the address to avoid failure on restart. */
+ OPT_ENABLE(fo, SOL_SOCKET, SO_REUSEADDR);
+ fd = makesock(l4type, 1, NULL, port, fo);
if (fd > 0) {
ret = listen(fd, BACKLOG);
if (ret < 0) {
*/
const uint8_t *dccp_available_ccids(uint8_t *ccids, uint8_t *nccids)
{
- int fd = makesock(AF_UNSPEC, IPPROTO_DCCP, 1, NULL, 0, NULL);
+ int fd = makesock(IPPROTO_DCCP, 1, NULL, 0, NULL);
if (fd < 0)
return NULL;