xref: /titanic_52/usr/src/lib/libnsl/rpc/rpc_td.c (revision d583b39bfb4e2571d3e41097c5c357ffe353ad45)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include "mt.h"
31 #include "rpc_mt.h"
32 #include <stdio.h>
33 #include <rpc/rpc.h>
34 #include <errno.h>
35 #include <tiuser.h>
36 #include <string.h>
37 #include <stropts.h>
38 #include <netinet/tcp.h>
39 #include <stdlib.h>
40 
41 #define	MAXOPTSIZE 64
42 
43 int
44 __td_setnodelay(int fd)
45 {
46 	int rval = 0;
47 	static mutex_t td_opt_lock = DEFAULTMUTEX;
48 	static struct t_optmgmt t_optreq, t_optret;
49 	int state;
50 
51 
52 	/* VARIABLES PROTECTED BY td_opt_lock: t_optreq, t_optret */
53 
54 	if ((state = t_getstate(fd)) == -1)
55 		return (-1);
56 
57 	(void) mutex_lock(&td_opt_lock);
58 	if ((state == T_IDLE) && (t_optreq.flags != T_NEGOTIATE)) {
59 		int i = 1;
60 		struct opthdr *opt;
61 
62 		t_optreq.flags = T_NEGOTIATE;
63 		t_optreq.opt.maxlen = MAXOPTSIZE;
64 		t_optreq.opt.buf = malloc(MAXOPTSIZE);
65 		if (t_optreq.opt.buf == NULL) {
66 			(void) mutex_unlock(&td_opt_lock);
67 			t_errno = TSYSERR;
68 			return (-1);
69 		}
70 		/* LINTED pointer cast */
71 		opt = (struct opthdr *)(t_optreq.opt.buf);
72 		opt->name = TCP_NODELAY;
73 		opt->len = 4;
74 		opt->level = IPPROTO_TCP;
75 		(void) memcpy((caddr_t)(t_optreq.opt.buf +
76 				sizeof (struct opthdr)), &i, sizeof (int));
77 		t_optreq.opt.len = (int)(sizeof (struct opthdr) +
78 						sizeof (int));
79 
80 		t_optret.opt.maxlen = MAXOPTSIZE;
81 		t_optret.opt.len = 0;
82 		t_optret.opt.buf = malloc(MAXOPTSIZE);
83 		if (t_optret.opt.buf == NULL) {
84 			(void) mutex_unlock(&td_opt_lock);
85 			free(t_optreq.opt.buf);
86 			t_errno = TSYSERR;
87 			return (-1);
88 		}
89 	}
90 
91 	if (state == T_IDLE)
92 		rval = t_optmgmt(fd, &t_optreq, &t_optret);
93 
94 	(void) mutex_unlock(&td_opt_lock);
95 	return (rval);
96 }
97