network.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*LINTLIBRARY*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <netdb.h>
#include <errno.h>
#include <syslog.h>
static int read_wait_time_sec = 60;
static int write_wait_time_sec = 10;
/*
* This module implements a set of functions to handle network
* communications. It attempts to hide any "uglyness" that might be
* necessary for such communications.
*/
/*
* null() is to be used as a signal handler that does nothing. It is used in
* place of SIG_IGN, because we want the signal to be delivered and
* interupt the current system call.
*/
static void
null(int i)
{
}
/*
* net_open() opens a tcp connection to the printer port on the host specified
* in the arguments passed in. If the connection is not made in the
* timeout (in seconds) passed in, an error it returned. If the host is
* unknown, an error is returned. If all is well, a file descriptor is
* returned to be used for future communications.
*/
int
{
struct sockaddr_in6 sin;
void (*old_handler)();
int s,
err,
unsigned timo = 1;
timeout);
/*
* Get the host address and port number to connect to.
*/
return (-1);
}
return (NETWORK_ERROR_HOST);
}
return (NETWORK_ERROR_SERVICE);
}
/*
* Try connecting to the server.
*
* Use 0 as lport means that rresvport_af() will bind to a port in
* the anonymous priviledged port range.
*/
lport = 0;
if (s < 0)
return (NETWORK_ERROR_PORT);
(void) alarm(0);
(void) close(s);
if (errno == EADDRINUSE) {
goto retry;
}
timo *= 2;
goto retry;
}
return (NETWORK_ERROR_UNKNOWN);
}
(void) alarm(0);
return (s);
}
/*
* net_close() closes a TCP connection opened by net_open()
*/
int
{
}
/*
* net_read() reads up to the length specified into the buffer supplied from
* the network connection specified
*/
int
{
int rc;
void (*old_handler)();
(void) alarm(read_wait_time_sec);
(void) alarm(0);
return (rc);
}
/*
* net_write() writes the buffer specified out to the network connection
* supplied.
*/
int
{
int rc;
void (*old_handler)();
(void) alarm(write_wait_time_sec);
(void) alarm(0);
return (rc);
}
/*
* net_response() reads in a byte from the network connection and
* returns -1 if it isn't 0.
*/
int
net_response(int nd)
{
char c;
return (-1);
}
return (0);
}
/*
* net_printf() sends a text message out to the network connection supplied
* using the same semantics as printf(3C) for stdio.
*/
int
{
char *buf;
int err;
int size;
int rc;
return (-1);
}
return (-1);
}
}
return (rc);
}
/*
* net_gets() read from the network connection until either a newline
* is encountered, or the buffer passed in is full. This is similiar
* to fgets(3C)
*/
char *
{
char tmp;
int count = 0;
if (count != 0)
return (buf);
return (NULL);
}
/*
* net_send_message() sends a message out the network connection using
* net_printf() and returns the result from net_response()
*/
int
{
return (-1);
}
return (-1);
return (net_response(nd));
}
/*
* net_send_file() sends the appropriate rfc1179 file transfer sub message
* to notify the remote side it is sending a file. It then sends the
* file if the remote side responds that it is ready. If the remote side
* can't accept the file an error is returned. If the transfer fails,
* an error is returned.
*/
int
{
char *truncated_name,
*mptr,
int count,
retries = 0,
size,
else
} else {
}
if (size < 0) {
return (NETWORK_ERROR_UNKNOWN);
}
/* send XFER command */
truncated_name) != 0) {
return (NETWORK_ERROR_SEND_RESPONSE);
}
/* send DATA and ACK */
while (count > 0) {
int rc;
if (rc < 0) {
if (retries++ < 5) {
/*
* will lose if syslogd down
*/
"net_send_file error on write: %m %d",
retries);
continue;
}
return (-1);
} else {
retries = 0;
}
}
return (NETWORK_ERROR_SEND_FAILED);
}
return (0);
}