File: opensocket.pl

package info (click to toggle)
checkservice 1.1.0-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 276 kB
  • ctags: 80
  • sloc: perl: 1,027; php: 301; makefile: 79
file content (34 lines) | stat: -rw-r--r-- 1,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# this file is included by all plugins, their is nu usefullness in
# running it standalone!

use FileHandle;

sub OpenSocket {
# Make a Berkeley socket connection between this program and a TCP port
#  on another (or this) host. Port can be a number or a named service
#
  local($OtherHostname, $Port) = @_;
  local($OurHostname, $sockaddr, $name, $aliases, $proto, $type, $len,
        $ThisAddr, $that);
  local @RemoteHostname = gethostbyname($OtherHostname); 
  $OurHostname = "localhost";
 
  @RemoteHostname || return 0; 
 
  ($name, $aliases, $proto) = getprotobyname('tcp'); 
  ($name, $aliases, $Port) = getservbyname($Port, 'tcp') unless $Port =~ /^\d+$/; 
  ($name, $aliases, $type, $len, $ThisAddr) = gethostbyname($OurHostname); 
  ($name, $aliases, $type, $len, $OtherHostAddr) = @RemoteHostname; 

  $sockaddr = 'S n a4 x8';    # Format for packed network address
  $that = pack($sockaddr, &AF_INET, $Port, $OtherHostAddr);

  $result = socket(S, &PF_INET, &SOCK_STREAM, $proto) || return 0;

  $result = connect(S, $that) || return 0;

  S->autoflush(1);			# set S to be un-buffered  
  return 1;                           	# success
}

return 1;