[dbench @ cvs-1:tridge-20041212143742-r0tjbd9yb1njroen]
[tridge/dbench.git] / sockio.c
1 /* 
2    dbench version 2
3    Copyright (C) Andrew Tridgell 1999
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "dbench.h"
21
22 #define MAX_FILES 1000
23
24 static char buf[70000];
25
26 char *server = "localhost";
27
28 static int sock;
29
30 /* emulate a single SMB packet exchange */
31 static void do_packets(int send_size, int recv_size)
32 {
33         uint32 *ubuf = (uint32 *)buf;
34
35         ubuf[0] = htonl(send_size-4);
36         ubuf[1] = htonl(recv_size-4);
37
38         if (write_sock(sock, buf, send_size) != send_size) {
39                 printf("error writing %d bytes\n", (int)send_size);
40                 exit(1);
41         }
42
43         if (read_sock(sock, buf, 4) != 4) {
44                 printf("error reading header\n");
45                 exit(1);
46         }
47
48         if (ntohl(ubuf[0]) != (unsigned)(recv_size-4)) {
49                 printf("lost sync (%d %d)\n", 
50                        (int)recv_size-4, (int)ntohl(ubuf[0]));
51                 exit(1);
52         }
53
54         if (recv(sock, buf, recv_size-4, MSG_WAITALL|MSG_TRUNC) != 
55             recv_size-4) {
56                 printf("error reading %d bytes\n", (int)recv_size-4);
57                 exit(1);
58         }
59
60         if (ntohl(ubuf[0]) != (unsigned)(recv_size-4)) {
61                 printf("lost sync (%d %d)\n", 
62                        (int)recv_size-4, (int)ntohl(ubuf[0]));
63         }
64 }
65
66
67 void nb_setup(struct child_struct *child)
68 {
69         extern char *tcp_options;
70
71         sock = open_socket_out(server, TCP_PORT);
72         if (sock == -1) {
73                 printf("client %d failed to start\n", child->id);
74                 exit(1);
75         }
76         set_socket_options(sock, tcp_options);
77
78         do_packets(8, 8);
79 }
80
81 void nb_unlink(struct child_struct *child, char *fname, int attr, const char *status)
82 {
83         (void)child;
84         (void)attr;
85         (void)status;
86         do_packets(39+2+strlen(fname)*2+2, 39);
87 }
88
89 void nb_mkdir(struct child_struct *child, char *dname, const char *status)
90 {
91         (void)child;
92         (void)status;
93         do_packets(39+2+strlen(dname)*2+2, 39);
94 }
95
96 void nb_rmdir(struct child_struct *child, char *fname, const char *status)
97 {
98         (void)child;
99         (void)status;
100         do_packets(39+2+strlen(fname)*2+2, 39);
101 }
102
103 void nb_createx(struct child_struct *child, const char *fname, 
104                 uint32_t create_options, uint32_t create_disposition, int fnum,
105                 const char *status)
106 {
107         (void)child;
108         (void)create_options;
109         (void)create_disposition;
110         (void)fnum;
111         (void)status;
112         do_packets(70+2+strlen(fname)*2+2, 39+12*4);
113 }
114
115 void nb_writex(struct child_struct *child, int handle, int offset, 
116                int size, int ret_size, const char *status)
117 {
118         (void)child;
119         (void)handle;
120         (void)offset;
121         (void)ret_size;
122         (void)status;
123         do_packets(39+20+size, 39+16);
124         child->bytes += size;
125 }
126
127 void nb_readx(struct child_struct *child, int handle, int offset, 
128               int size, int ret_size, const char *status)
129 {
130         (void)child;
131         (void)handle;
132         (void)offset;
133         (void)size;
134         (void)status;
135         do_packets(39+20, 39+20+ret_size);
136         child->bytes += ret_size;
137 }
138
139 void nb_close(struct child_struct *child, int handle, const char *status)
140 {
141         (void)child;
142         (void)handle;
143         (void)status;
144         do_packets(39+8, 39);
145 }
146
147 void nb_rename(struct child_struct *child, char *old, char *new, const char *status)
148 {
149         (void)child;
150         (void)status;
151         do_packets(39+8+2*strlen(old)+2*strlen(new), 39);
152 }
153
154 void nb_flush(struct child_struct *child, int handle, const char *status)
155 {
156         (void)child;
157         (void)handle;
158         (void)status;
159         do_packets(39+2, 39);
160 }
161
162 void nb_qpathinfo(struct child_struct *child, const char *fname, int level, 
163                   const char *status)
164 {
165         (void)child;
166         (void)level;
167         (void)status;
168         do_packets(39+16+2*strlen(fname), 39+32);
169 }
170
171 void nb_qfileinfo(struct child_struct *child, int handle, int level, const char *status)
172 {
173         (void)child;
174         (void)level;
175         (void)handle;
176         (void)status;
177         do_packets(39+20, 39+32);
178 }
179
180 void nb_qfsinfo(struct child_struct *child, int level, const char *status)
181 {
182         (void)child;
183         (void)level;
184         (void)status;
185         do_packets(39+20, 39+32);
186 }
187
188 void nb_findfirst(struct child_struct *child, char *fname, int level, int maxcnt, 
189                   int count, const char *status)
190 {
191         (void)child;
192         (void)level;
193         (void)maxcnt;
194         (void)status;
195         do_packets(39+20+strlen(fname)*2, 39+90*count);
196 }
197
198 void nb_cleanup(struct child_struct *child)
199 {
200         (void)child;
201 }
202
203 void nb_deltree(struct child_struct *child, char *dname)
204 {
205         (void)child;
206         (void)dname;
207 }
208
209 void nb_sfileinfo(struct child_struct *child, int handle, int level, const char *status)
210 {
211         (void)child;
212         (void)handle;
213         (void)level;
214         (void)status;
215         do_packets(39+32, 39+8);
216 }
217
218 void nb_lockx(struct child_struct *child, int handle, uint32_t offset, int size, 
219               const char *status)
220 {
221         (void)child;
222         (void)handle;
223         (void)offset;
224         (void)size;
225         (void)status;
226         do_packets(39+12, 39);
227 }
228
229 void nb_unlockx(struct child_struct *child,
230                 int handle, uint32_t offset, int size, const char *status)
231 {
232         (void)child;
233         (void)handle;
234         (void)offset;
235         (void)size;
236         (void)status;
237         do_packets(39+12, 39);
238 }
239
240 void nb_sleep(struct child_struct *child, int usec, const char *status)
241 {
242         (void)child;
243         (void)usec;
244         (void)status;
245         usleep(usec);
246 }