| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "Socket 3" |
| 132 | .TH Socket 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa \- load the C socket.h defines and structure manipulators |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 1 |
| 138 | \& use Socket; |
| 139 | .Ve |
| 140 | .PP |
| 141 | .Vb 6 |
| 142 | \& $proto = getprotobyname('udp'); |
| 143 | \& socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto); |
| 144 | \& $iaddr = gethostbyname('hishost.com'); |
| 145 | \& $port = getservbyname('time', 'udp'); |
| 146 | \& $sin = sockaddr_in($port, $iaddr); |
| 147 | \& send(Socket_Handle, 0, 0, $sin); |
| 148 | .Ve |
| 149 | .PP |
| 150 | .Vb 7 |
| 151 | \& $proto = getprotobyname('tcp'); |
| 152 | \& socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto); |
| 153 | \& $port = getservbyname('smtp', 'tcp'); |
| 154 | \& $sin = sockaddr_in($port,inet_aton("127.1")); |
| 155 | \& $sin = sockaddr_in(7,inet_aton("localhost")); |
| 156 | \& $sin = sockaddr_in(7,INADDR_LOOPBACK); |
| 157 | \& connect(Socket_Handle,$sin); |
| 158 | .Ve |
| 159 | .PP |
| 160 | .Vb 3 |
| 161 | \& ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle)); |
| 162 | \& $peer_host = gethostbyaddr($iaddr, AF_INET); |
| 163 | \& $peer_addr = inet_ntoa($iaddr); |
| 164 | .Ve |
| 165 | .PP |
| 166 | .Vb 5 |
| 167 | \& $proto = getprotobyname('tcp'); |
| 168 | \& socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto); |
| 169 | \& unlink('/var/run/usock'); |
| 170 | \& $sun = sockaddr_un('/var/run/usock'); |
| 171 | \& connect(Socket_Handle,$sun); |
| 172 | .Ve |
| 173 | .SH "DESCRIPTION" |
| 174 | .IX Header "DESCRIPTION" |
| 175 | This module is just a translation of the C \fIsocket.h\fR file. |
| 176 | Unlike the old mechanism of requiring a translated \fIsocket.ph\fR |
| 177 | file, this uses the \fBh2xs\fR program (see the Perl source distribution) |
| 178 | and your native C compiler. This means that it has a |
| 179 | far more likely chance of getting the numbers right. This includes |
| 180 | all of the commonly used pound-defines like \s-1AF_INET\s0, \s-1SOCK_STREAM\s0, etc. |
| 181 | .PP |
| 182 | Also, some common socket \*(L"newline\*(R" constants are provided: the |
| 183 | constants \f(CW\*(C`CR\*(C'\fR, \f(CW\*(C`LF\*(C'\fR, and \f(CW\*(C`CRLF\*(C'\fR, as well as \f(CW$CR\fR, \f(CW$LF\fR, and |
| 184 | \&\f(CW$CRLF\fR, which map to \f(CW\*(C`\e015\*(C'\fR, \f(CW\*(C`\e012\*(C'\fR, and \f(CW\*(C`\e015\e012\*(C'\fR. If you do |
| 185 | not want to use the literal characters in your programs, then use |
| 186 | the constants provided here. They are not exported by default, but can |
| 187 | be imported individually, and with the \f(CW\*(C`:crlf\*(C'\fR export tag: |
| 188 | .PP |
| 189 | .Vb 1 |
| 190 | \& use Socket qw(:DEFAULT :crlf); |
| 191 | .Ve |
| 192 | .PP |
| 193 | In addition, some structure manipulation functions are available: |
| 194 | .IP "inet_aton \s-1HOSTNAME\s0" 4 |
| 195 | .IX Item "inet_aton HOSTNAME" |
| 196 | Takes a string giving the name of a host, and translates that to an |
| 197 | opaque string (if programming in C, struct in_addr). Takes arguments |
| 198 | of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name |
| 199 | cannot be resolved, returns undef. For multi-homed hosts (hosts with |
| 200 | more than one address), the first address found is returned. |
| 201 | .Sp |
| 202 | For portability do not assume that the result of \fIinet_aton()\fR is 32 |
| 203 | bits wide, in other words, that it would contain only the IPv4 address |
| 204 | in network order. |
| 205 | .IP "inet_ntoa \s-1IP_ADDRESS\s0" 4 |
| 206 | .IX Item "inet_ntoa IP_ADDRESS" |
| 207 | Takes a string (an opaque string as returned by \fIinet_aton()\fR, |
| 208 | or a v\-string representing the four octets of the IPv4 address in |
| 209 | network order) and translates it into a string of the form 'd.d.d.d' |
| 210 | where the 'd's are numbers less than 256 (the normal human-readable |
| 211 | four dotted number notation for Internet addresses). |
| 212 | .IP "\s-1INADDR_ANY\s0" 4 |
| 213 | .IX Item "INADDR_ANY" |
| 214 | Note: does not return a number, but a packed string. |
| 215 | .Sp |
| 216 | Returns the 4\-byte wildcard ip address which specifies any |
| 217 | of the hosts ip addresses. (A particular machine can have |
| 218 | more than one ip address, each address corresponding to |
| 219 | a particular network interface. This wildcard address |
| 220 | allows you to bind to all of them simultaneously.) |
| 221 | Normally equivalent to inet_aton('0.0.0.0'). |
| 222 | .IP "\s-1INADDR_BROADCAST\s0" 4 |
| 223 | .IX Item "INADDR_BROADCAST" |
| 224 | Note: does not return a number, but a packed string. |
| 225 | .Sp |
| 226 | Returns the 4\-byte 'this\-lan' ip broadcast address. |
| 227 | This can be useful for some protocols to solicit information |
| 228 | from all servers on the same \s-1LAN\s0 cable. |
| 229 | Normally equivalent to inet_aton('255.255.255.255'). |
| 230 | .IP "\s-1INADDR_LOOPBACK\s0" 4 |
| 231 | .IX Item "INADDR_LOOPBACK" |
| 232 | Note \- does not return a number. |
| 233 | .Sp |
| 234 | Returns the 4\-byte loopback address. Normally equivalent |
| 235 | to inet_aton('localhost'). |
| 236 | .IP "\s-1INADDR_NONE\s0" 4 |
| 237 | .IX Item "INADDR_NONE" |
| 238 | Note \- does not return a number. |
| 239 | .Sp |
| 240 | Returns the 4\-byte 'invalid' ip address. Normally equivalent |
| 241 | to inet_aton('255.255.255.255'). |
| 242 | .IP "sockaddr_family \s-1SOCKADDR\s0" 4 |
| 243 | .IX Item "sockaddr_family SOCKADDR" |
| 244 | Takes a sockaddr structure (as returned by \fIpack_sockaddr_in()\fR, |
| 245 | \&\fIpack_sockaddr_un()\fR or the perl builtin functions \fIgetsockname()\fR and |
| 246 | \&\fIgetpeername()\fR) and returns the address family tag. It will match the |
| 247 | constant \s-1AF_INET\s0 for a sockaddr_in and \s-1AF_UNIX\s0 for a sockaddr_un. It |
| 248 | can be used to figure out what unpacker to use for a sockaddr of |
| 249 | unknown type. |
| 250 | .IP "sockaddr_in \s-1PORT\s0, \s-1ADDRESS\s0" 4 |
| 251 | .IX Item "sockaddr_in PORT, ADDRESS" |
| 252 | .PD 0 |
| 253 | .IP "sockaddr_in \s-1SOCKADDR_IN\s0" 4 |
| 254 | .IX Item "sockaddr_in SOCKADDR_IN" |
| 255 | .PD |
| 256 | In a list context, unpacks its \s-1SOCKADDR_IN\s0 argument and returns an array |
| 257 | consisting of (\s-1PORT\s0, \s-1ADDRESS\s0). In a scalar context, packs its (\s-1PORT\s0, |
| 258 | \&\s-1ADDRESS\s0) arguments as a \s-1SOCKADDR_IN\s0 and returns it. If this is confusing, |
| 259 | use \fIpack_sockaddr_in()\fR and \fIunpack_sockaddr_in()\fR explicitly. |
| 260 | .IP "pack_sockaddr_in \s-1PORT\s0, \s-1IP_ADDRESS\s0" 4 |
| 261 | .IX Item "pack_sockaddr_in PORT, IP_ADDRESS" |
| 262 | Takes two arguments, a port number and an opaque string, \s-1IP_ADDRESS\s0 |
| 263 | (as returned by \fIinet_aton()\fR, or a v\-string). Returns the sockaddr_in |
| 264 | structure with those arguments packed in with \s-1AF_INET\s0 filled in. For |
| 265 | Internet domain sockets, this structure is normally what you need for |
| 266 | the arguments in \fIbind()\fR, \fIconnect()\fR, and \fIsend()\fR, and is also returned |
| 267 | by \fIgetpeername()\fR, \fIgetsockname()\fR and \fIrecv()\fR. |
| 268 | .IP "unpack_sockaddr_in \s-1SOCKADDR_IN\s0" 4 |
| 269 | .IX Item "unpack_sockaddr_in SOCKADDR_IN" |
| 270 | Takes a sockaddr_in structure (as returned by \fIpack_sockaddr_in()\fR) and |
| 271 | returns an array of two elements: the port and an opaque string |
| 272 | representing the \s-1IP\s0 address (you can use \fIinet_ntoa()\fR to convert the |
| 273 | address to the four-dotted numeric format). Will croak if the |
| 274 | structure does not have \s-1AF_INET\s0 in the right place. |
| 275 | .IP "sockaddr_un \s-1PATHNAME\s0" 4 |
| 276 | .IX Item "sockaddr_un PATHNAME" |
| 277 | .PD 0 |
| 278 | .IP "sockaddr_un \s-1SOCKADDR_UN\s0" 4 |
| 279 | .IX Item "sockaddr_un SOCKADDR_UN" |
| 280 | .PD |
| 281 | In a list context, unpacks its \s-1SOCKADDR_UN\s0 argument and returns an array |
| 282 | consisting of (\s-1PATHNAME\s0). In a scalar context, packs its \s-1PATHNAME\s0 |
| 283 | arguments as a \s-1SOCKADDR_UN\s0 and returns it. If this is confusing, use |
| 284 | \&\fIpack_sockaddr_un()\fR and \fIunpack_sockaddr_un()\fR explicitly. |
| 285 | These are only supported if your system has <\fIsys/un.h\fR>. |
| 286 | .IP "pack_sockaddr_un \s-1PATH\s0" 4 |
| 287 | .IX Item "pack_sockaddr_un PATH" |
| 288 | Takes one argument, a pathname. Returns the sockaddr_un structure with |
| 289 | that path packed in with \s-1AF_UNIX\s0 filled in. For unix domain sockets, this |
| 290 | structure is normally what you need for the arguments in \fIbind()\fR, |
| 291 | \&\fIconnect()\fR, and \fIsend()\fR, and is also returned by \fIgetpeername()\fR, |
| 292 | \&\fIgetsockname()\fR and \fIrecv()\fR. |
| 293 | .IP "unpack_sockaddr_un \s-1SOCKADDR_UN\s0" 4 |
| 294 | .IX Item "unpack_sockaddr_un SOCKADDR_UN" |
| 295 | Takes a sockaddr_un structure (as returned by \fIpack_sockaddr_un()\fR) |
| 296 | and returns the pathname. Will croak if the structure does not |
| 297 | have \s-1AF_UNIX\s0 in the right place. |