Watt-32 tcp/ip  2.2 dev-rel.10
fcntl.c
Go to the documentation of this file.
1 
5 /*
6  * BSD sockets functionality for Watt-32 TCP/IP
7  *
8  * Copyright (c) 1997-2002 Gisle Vanem <gvanem@yahoo.no>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  * must display the following acknowledgement:
20  * This product includes software developed by Gisle Vanem
21  * Bergen, Norway.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ME (Gisle Vanem) AND CONTRIBUTORS ``AS IS''
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL I OR CONTRIBUTORS BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * Version
35  *
36  * 0.5 : 19 Aug 1999 : by Claus Oberste-Brandenburg
37  * <Claus.OBERSTE-BRANDENBURG@jacala.ensmp.fr>
38  *
39  * 0.6 : 19 Aug 1999 : Changed function prototype, added fd_duped (GV)
40  */
41 
42 #include "socket.h"
43 
44 #if defined(USE_BSD_API)
45 
46 #ifndef O_NONBLOCK
47 #define O_NONBLOCK 0x2000
48 #endif
49 
50 #if defined(USE_DEBUG)
51 /*
52  * Return string for fcntlsocket() command
53  */
54 static const struct search_list commands[] = {
55  { F_DUPFD, "F_DUPFD" },
56  { F_GETFL, "F_GETFL" },
57  { F_SETFL, "F_SETFL" },
58  { F_GETFD, "F_GETFD" },
59  { F_SETFD, "F_SETFD" },
60  { F_GETLK, "F_GETLK" },
61  { F_SETLK, "F_SETLK" },
62  { F_SETLKW, "F_SETLKW" },
63  { F_GETOWN, "F_GETOWN" },
64  { F_SETOWN, "F_SETOWN" }
65  };
66 #endif /* USE_DEBUG */
67 
68 
69 int MS_CDECL fcntlsocket (int s, int cmd, ...)
70 {
72  size_t arg; /* same size as a pointer */
73  int rc = 0;
74  va_list va;
75 
76  va_start (va, cmd);
77  arg = va_arg (va, size_t);
78 
79  SOCK_PROLOGUE (socket, "\nfcntlsocket:%d, ", s);
80 
81  SOCK_DEBUGF ((", %s: ", list_lookup(cmd,commands,DIM(commands))));
82 
83  /* fcntl is always file-ioctrl
84  */
85  switch (cmd)
86  {
87  case F_DUPFD:
88  socket->fd_duped++;
89  SOCK_DEBUGF (("dup %d", socket->fd_duped));
90  return (0);
91 
92  case F_GETFL:
95  if (socket->so_state & SS_NBIO)
96  {
97  SOCK_DEBUGF (("non-blocking"));
98  return (O_NONBLOCK);
99  }
100  SOCK_DEBUGF (("blocking"));
101  return (0);
102 
103  case F_SETFL:
104  if (arg & O_NONBLOCK) /* O_APPEND will not be served */
105  {
106  SOCK_DEBUGF (("non-blocking"));
107  socket->so_state |= SS_NBIO;
108  socket->timeout = 0;
109  if (socket->tcp_sock)
110  socket->tcp_sock->timeout = 0;
111  }
112  else
113  {
114  SOCK_DEBUGF (("blocking"));
115  socket->so_state &= ~SS_NBIO;
116  socket->timeout = sock_delay;
117  }
118  return (0);
119 
120  case F_GETFD:
121  return (0); /* socket remains open across exec() */
122 
123  case F_SETFD:
124  case F_GETLK:
125  case F_SETLK:
126  case F_SETLKW:
127  case F_GETOWN:
128  case F_SETOWN:
129  SOCK_DEBUGF (("not supported"));
130  SOCK_ERRNO (ESOCKTNOSUPPORT);
131  rc = -1;
132  break;
133 
134  default:
135  SOCK_ENTER_SCOPE();
136  if (ioctlsocket(s, cmd, (char*)arg) < 0)
137  {
138  SOCK_DEBUGF ((", unknown cmd %d", cmd));
139  SOCK_ERRNO (ESOCKTNOSUPPORT);
140  rc = -1;
141  }
142  SOCK_LEAVE_SCOPE();
143  }
144  return (rc);
145 }
146 #endif /* USE_BSD_API */
147 
Definition: socket.h:137
Definition: if.h:84
const char * list_lookup(DWORD type, const struct search_list *list, int num)
Search 'list' for 'type' and return it's name.
Definition: misc.c:929
Socket * _socklist_find(int s)
Returns a pointer to the Socket structure associated with socket 's'.
Definition: socket.c:1534
int MS_CDECL fcntlsocket(int s, int cmd,...)
Definition: fcntl.c:69
DWORD timeout
timer for retrans etc.
Definition: wattcp.h:631