2 Unix SMB/Netbios implementation.
4 SMB agent/socket plugin
5 Copyright (C) Andrew Tridgell 1999
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
28 /****************************************************************************
29 terminate socket connection
30 ****************************************************************************/
31 static void sock_redir_free(struct vagent_ops *va, struct sock_redir *sock)
40 va->free_sock(sock->n);
46 /****************************************************************************
48 ****************************************************************************/
49 static void free_sock_array(struct vagent_ops*va)
51 void(*fn)(void*) = (void(*)(void*))&va->free_sock;
52 free_void_array(va->num_socks, (void**)va->socks, *fn);
55 /****************************************************************************
56 add a sockent state to the array
57 ****************************************************************************/
58 static struct sock_redir* add_sock_to_array(uint32 *len,
59 struct sock_redir ***array,
60 struct sock_redir *sock)
63 for (i = 0; i < (*len); i++)
65 if ((*array)[i] == NULL)
72 return (struct sock_redir*)add_item_to_array(len,
73 (void***)array, (void*)sock);
77 /****************************************************************************
78 initiate sockent array
79 ****************************************************************************/
80 void init_sock_redir(struct vagent_ops*va)
86 /****************************************************************************
87 terminate sockent array
88 ****************************************************************************/
89 void free_sock_redir(struct vagent_ops*va)
95 /****************************************************************************
96 create a new sockent state from user credentials
97 ****************************************************************************/
98 static struct sock_redir *sock_redir_get(struct vagent_ops *va, int fd)
100 struct sock_redir *sock = (struct sock_redir*)malloc(sizeof(*sock));
112 DEBUG(10,("sock_redir_get:\tfd:\t%d\n", fd));
116 /****************************************************************************
118 ****************************************************************************/
119 static void sock_add(struct vagent_ops *va, int fd)
121 struct sock_redir *sock;
122 sock = sock_redir_get(va, fd);
125 add_sock_to_array(&va->num_socks, &va->socks, sock);
129 /****************************************************************************
130 delete a sockent state
131 ****************************************************************************/
132 static BOOL sock_del(struct vagent_ops *va, int fd)
136 for (i = 0; i < va->num_socks; i++)
138 if (va->socks[i] == NULL) continue;
139 if (va->socks[i]->c == fd)
141 sock_redir_free(va, va->socks[i]);
150 void start_agent(struct vagent_ops *va)
154 s = va->get_agent_sock(va->id);
161 struct sockaddr_un addr;
162 int in_addrlen = sizeof(addr);
168 for (i = 0; i < va->num_socks; i++)
170 if (va->socks[i] != NULL)
172 int fd = va->socks[i]->c;
174 maxfd = MAX(maxfd, fd);
176 if (va->socks[i]->n != NULL)
178 fd = va->socks[i]->s;
180 maxfd = MAX(fd, maxfd);
186 num = sys_select(maxfd+1,&fds,NULL, NULL);
193 if (FD_ISSET(s, &fds))
196 c = accept(s, (struct sockaddr*)&addr, &in_addrlen);
203 for (i = 0; i < va->num_socks; i++)
205 if (va->socks[i] == NULL)
209 if (FD_ISSET(va->socks[i]->c, &fds))
211 FD_CLR(va->socks[i]->c, &fds);
212 if (!va->process_cli_sock(va->socks,
216 sock_redir_free(va, va->socks[i]);
220 if (va->socks[i] == NULL)
224 if (va->socks[i]->n == NULL)
228 if (FD_ISSET(va->socks[i]->s, &fds))
230 FD_CLR(va->socks[i]->s, &fds);
231 if (!va->process_srv_sock(va->socks,
235 sock_redir_free(va, va->socks[i]);