+int oversize_test(gchar* hostname, int port, char* name, int sock, char sock_is_open, char close_sock) {
+ int retval=0;
+ struct nbd_request req;
+ struct nbd_reply rep;
+ int request=0;
+ int i=0;
+ pid_t mypid = getpid();
+ char buf[((1024*1024)+sizeof(struct nbd_request)/2)<<1];
+ bool got_err;
+
+ /* This should work */
+ if(!sock_is_open) {
+ if((sock=setup_connection(hostname, port, name, CONNECTION_TYPE_FULL))<0) {
+ g_warning("Could not open socket: %s", errstr);
+ retval=-1;
+ goto err;
+ }
+ }
+ req.magic=htonl(NBD_REQUEST_MAGIC);
+ req.type=htonl(NBD_CMD_READ);
+ req.len=htonl(1024*1024);
+ memcpy(&(req.handle),&i,sizeof(i));
+ req.from=htonll(i);
+ write(sock, &req, sizeof(req));
+ printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
+ read_all(sock, &rep, sizeof(struct nbd_reply));
+ read_all(sock, &buf, ntohl(req.len));
+ if(rep.error) {
+ printf("Received unexpected error\n");
+ retval=-1;
+ goto err;
+ } else {
+ printf("OK\n");
+ }
+ /* This probably should not work */
+ i++; req.from=htonll(i);
+ req.len = htonl(ntohl(req.len) + sizeof(struct nbd_request) / 2);
+ write(sock, &req, sizeof(req));
+ printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
+ read_all(sock, &rep, sizeof(struct nbd_reply));
+ read_all(sock, &buf, ntohl(req.len));
+ if(rep.error) {
+ printf("Received expected error\n");
+ got_err=true;
+ } else {
+ printf("OK\n");
+ got_err=false;
+ }
+ /* ... unless this works, too */
+ i++; req.from=htonll(i);
+ req.len = htonl(ntohl(req.len) << 1);
+ write(sock, &req, sizeof(req));
+ printf("%d: testing oversized request: %d: ", getpid(), ntohl(req.len));
+ read_all(sock, &rep, sizeof(struct nbd_reply));
+ read_all(sock, &buf, ntohl(req.len));
+ if(rep.error) {
+ printf("error\n");
+ } else {
+ printf("OK\n");
+ }
+ if((rep.error && !got_err) || (!rep.error && got_err)) {
+ printf("Received unexpected error\n");
+ retval=-1;
+ }
+ err:
+ return retval;
+}
+