xref: /titanic_41/usr/src/lib/libwanboot/common/socket_inet.c (revision 6185db853e024a486ff8837e6784dd290d866112)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <unistd.h>
33 #include <poll.h>
34 #include <errno.h>
35 
36 #include <socket_inet.h>
37 
38 /*
39  * Name:	socket_read
40  * Description:	Use recv in non-secure sockets.
41  * Scope:	private
42  * Arguments:	fildes		- Socket file descriptor.
43  *		buf		- Buffer to read data into.
44  *		nbyte		- Number of bytes to read.
45  *              read_timeout    - Timeout value in seconds.
46  * Returns:	n		- Number of bytes read. -1 on error.
47  */
48 int
49 socket_read(int fildes, void *buf, size_t nbyte, int read_timeout)
50 {
51 	struct pollfd pfd;
52 
53 	pfd.fd = fildes;
54 	pfd.events = POLLIN;
55 
56 	switch (poll(&pfd, 1, read_timeout * 1000)) {
57 	case 0:
58 		errno = EINTR;
59 		return (-1);
60 	case -1:
61 		return (-1);
62 	default:
63 		break;
64 	}
65 
66 	return (recv(fildes, buf, nbyte, 0));
67 }
68 
69 /*
70  * Name:	socket_write
71  * Description:	Use sendto for non-secure connections.
72  * Scope:	private
73  * Arguments:	fildes		- Socket file descriptor.
74  *		buf		- Buffer containing data to be written.
75  *		nbyte		- Number of bytes to write.
76  *              addr            - Connection address
77  * Returns:	n		- Number of bytes written. -1 on error.
78  */
79 int
80 socket_write(int fildes, const void *buf, size_t nbyte,
81     struct sockaddr_in *addr)
82 {
83 	return (sendto(fildes, buf, nbyte, 0, (struct sockaddr *)addr,
84 	    sizeof (*addr)));
85 }
86 
87 int
88 socket_close(int fildes)
89 {
90 	return (close(fildes));
91 }
92