File: open_device.c

package info (click to toggle)
cti-ifhp 2.1.8-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 932 kB
  • ctags: 845
  • sloc: ansic: 6,036; sh: 1,524; makefile: 318
file content (115 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**************************************************************************
 * LPRng IFHP Filter
 * Copyright 1994-1997 Patrick Powell, San Diego, CA <papowell@sdsu.edu>
 *
 * Based on the CTI printer filters.
 *  See COPYRIGHT for details.
 *
 * $Id: open_device.c,v 2.1 1997/01/05 18:41:41 papowell Exp $
 */

#include "portable.h"
#include "common.h"
#include "errorcodes.h"

extern char *device;
void Do_stty();

void open_device()
{
	char *s, *end;
	int i;
	int fd, port;
	struct hostent *hostent;
	struct sockaddr_in sin;
	int src_port = 0;
	char dcopy[128];

	log(3,"open_device: device '%s'", device);
	strncpy( dcopy, device, sizeof(dcopy)-1 );
	dcopy[sizeof(dcopy)-1] = 0;

	if( (dcopy[0] == '/') ){
		fd = open( dcopy, O_RDWR );
		if( fd < 0 ){
			logerr(0,"open_device: open '%s' failed");
			fexit( JABORT );
		}
		if( fd != 1 ){
			if( dup2( fd, 1 ) < 0 ){
				logerr(0,"open_device: open '%s' failed",dcopy);
				fexit( JABORT );
			}
			close( fd );
		}
		if( isatty( 1 ) && stty_args){
			Do_stty( 1, stty_args );
		}
	} else {
		if( (s = strchr( dcopy, '%' )) == 0 ){
			log(1,"open_device: missing port number '%s'",dcopy );
			fexit( JABORT );
		}
		end = s+1; 
		port = strtol( end, &end, 0 );
		if( port <= 0 ){
			log(1,"open_device: bad port number '%s'",s+1 );
			fexit( JABORT );
		}
		if( end && *end ){
			src_port = atoi( end+1 );
		}
		*s = 0;
		memset( &sin, 0, sizeof(sin) );
		sin.sin_family = AF_INET;
		if( (hostent = gethostbyname(dcopy)) ){
			/*
			 * set up the address information
			 */
			if( hostent->h_addrtype != AF_INET ){
				log(1,"open_device: bad address type for host '%s'", dcopy);
				fexit( JABORT );
			}
			memcpy( &sin.sin_addr, hostent->h_addr, hostent->h_length );
		} else {
			sin.sin_addr.s_addr = inet_addr(dcopy);
			if( sin.sin_addr.s_addr == -1){
				log(1,"open_device: getconnection: unknown host '%s'", dcopy);
				fexit( JABORT );
			}
		}
		sin.sin_port = htons( port );
		log(2,"open_device: destination '%s' port %d",
			inet_ntoa( sin.sin_addr ), ntohs( sin.sin_port ) );
		fd = socket (AF_INET, SOCK_STREAM, 0);
		if( fd >= 0 && src_port ){
			struct sockaddr_in src_sin;
			memset( &src_sin, 0, sizeof(src_sin) );
			src_sin.sin_family = AF_INET;
			src_sin.sin_port = htons( src_port );
			if( bind( fd, (struct sockaddr *)&src_sin,
				sizeof( src_sin )) < 0 ){
				logerr(0,"open_device: bind failed");
				fexit(JABORT);
			}
		}
		if (fd < 0) {
			logerr(0,"open_device: socket call failed");
			fexit( JABORT );
		}
		i = connect (fd, (struct sockaddr *) & sin, sizeof (sin));
		if( i < 0 ){
			logerr(0,"open_device: connect to '%s port %d' failed",
				inet_ntoa( sin.sin_addr ), ntohs( sin.sin_port ) );
			fexit(JABORT);
		}
		if( fd != 1 ){
			log(4,"open_device: dup %d to %d", fd, 1 );
			if( dup2( fd, 1 ) < 0 ){
				logerr(0,"open_device: dup failed");
				fexit(JABORT);
			}
			close( fd );
		}
	}
}