Hello, wise Perl gurus! I'm working on a protocol independent server using Socket6. It appears that the accept function can't handle IPv6 very well, so I am unable to use getnameinfo in order to figure out who the connecting host is. Any suggestions?

#!/usr/bin/perl -w # # $Id: program,v 1.11 2002/03/09 02:19:09 jj Exp $ # # Author: Shannon -jj Behrens # Date: Tue Sep 11 13:52:16 PDT 2001 # use strict; # Read "man getaddrinfo", "perldoc Socket6", and "perldoc perlipc". use Socket; use Socket6; # This is the port we should listen to for incoming bell # requests. $::BELL_PORT = 1029; # # A lot of other code has been snipped. # # This is a stub function that will do something with a # host and two ports. sub ring_bell { my ($pip, $vic_port, $rat_port) = @_; print "pip: $pip vic_port: $vic_port " . "rat_port: $rat_port\n"; } # Open up a socket on $::BELL_PORT. If a connection is # received, accept the Vic and Rat port values, call # ring_bell, and disconnect. This function must # be called from a forked process before Tk is used. This # function does not return. sub bell_daemon { my @addrinfo = getaddrinfo("", $::BELL_PORT, PF_INET6, SOCK_STREAM, 0, AI_PASSIVE); if (scalar(@addrinfo) < 5) { die "Error while trying to bind to port. " . "Reason: $1"; } my ($family, $socktype, $proto, $saddr) = @addrinfo; socket SERVER, $family, $socktype, $proto || die "Couldn't create socket. Reason: $1"; setsockopt SERVER, SOL_SOCKET, SO_REUSEADDR, pack("l", 1) || die "Couldn't set socket options. Reason: $1"; bind SERVER, $saddr || die "Couldn't bind to socket. Reason: $1"; listen SERVER, SOMAXCONN || die "Couldn't call listen on socket. Reason: $1"; for ( ; my $paddr = accept CLIENT, SERVER; close CLIENT) { my ($pip, $pport) = getnameinfo($paddr, NI_NUMERICHOST | NI_NUMERICSERV); my $line; if (!defined($line = <CLIENT>)) { warn "connecting socket didn't send " . "port values"; next; } my @port_values = split / /, $line; if (scalar(@port_values) != 2) { warn "connecting socket didn't send exactly " . "two port values"; next; } my ($vic_port, $rat_port) = @port_values; ring_bell $pip, $vic_port, $rat_port; } close SERVER; } bell_daemon;

In reply to Using accept for IPv6 by jjinux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.