xref: /titanic_44/usr/src/lib/libnsl/rpc/rpc_td.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 2005 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 #if !defined(lint) && defined(SCCSIDS)
30 static char sccsid[] = "@(#)rpc_td.c 1.32 89/03/16 Copyr 1988 Sun Micro";
31 #endif
32 
33 #include "mt.h"
34 #include "rpc_mt.h"
35 #include <stdio.h>
36 #include <rpc/rpc.h>
37 #include <rpc/trace.h>
38 #include <errno.h>
39 #include <tiuser.h>
40 #include <string.h>
41 #include <stropts.h>
42 #include <netinet/tcp.h>
43 #include <stdlib.h>
44 
45 #define	MAXOPTSIZE 64
46 
47 int
48 __td_setnodelay(fd)
49 	int fd;
50 {
51 	int rval = 0;
52 	static mutex_t td_opt_lock = DEFAULTMUTEX;
53 	static struct t_optmgmt t_optreq, t_optret;
54 	int state;
55 
56 
57 	/* VARIABLES PROTECTED BY td_opt_lock: t_optreq, t_optret */
58 
59 	trace2(TR__td_setnodelay, 0, fd);
60 
61 	if ((state = t_getstate(fd)) == -1)
62 		return (-1);
63 
64 	mutex_lock(&td_opt_lock);
65 	if ((state == T_IDLE) && (t_optreq.flags != T_NEGOTIATE)) {
66 		int i = 1;
67 		struct opthdr *opt;
68 
69 		t_optreq.flags = T_NEGOTIATE;
70 		t_optreq.opt.maxlen = MAXOPTSIZE;
71 		t_optreq.opt.buf = (char *)malloc(MAXOPTSIZE);
72 		if (t_optreq.opt.buf == NULL) {
73 			mutex_unlock(&td_opt_lock);
74 			t_errno = TSYSERR;
75 			return (-1);
76 		}
77 		opt = (struct opthdr *)(t_optreq.opt.buf);
78 		opt->name = TCP_NODELAY;
79 		opt->len = 4;
80 		opt->level = IPPROTO_TCP;
81 		(void) memcpy((caddr_t)(t_optreq.opt.buf +
82 				sizeof (struct opthdr)), &i, sizeof (int));
83 		t_optreq.opt.len = (int)(sizeof (struct opthdr) +
84 						sizeof (int));
85 
86 		t_optret.opt.maxlen = MAXOPTSIZE;
87 		t_optret.opt.len = 0;
88 		t_optret.opt.buf = (char *)malloc(MAXOPTSIZE);
89 		if (t_optret.opt.buf == NULL) {
90 			mutex_unlock(&td_opt_lock);
91 			free(t_optreq.opt.buf);
92 			t_errno = TSYSERR;
93 			return (-1);
94 		}
95 	}
96 
97 	if (state == T_IDLE)
98 		rval = t_optmgmt(fd, &t_optreq, &t_optret);
99 
100 	mutex_unlock(&td_opt_lock);
101 	trace2(TR__td_setnodelay, 1, fd);
102 	return (rval);
103 }
104