|
| 1 | +/* |
| 2 | + * Copyright 2013 10gen Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +/* |
| 19 | + * This example can only be used internally to the library as it uses |
| 20 | + * private features that are not exported in the public ABI. It does, |
| 21 | + * however, illustrate some of the internals of the system. |
| 22 | + */ |
| 23 | + |
| 24 | + |
| 25 | +#include <fcntl.h> |
| 26 | +#include <mongoc.h> |
| 27 | +#include <mongoc-array-private.h> |
| 28 | +#include <mongoc-rpc-private.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <stdlib.h> |
| 31 | +#include <unistd.h> |
| 32 | + |
| 33 | + |
| 34 | +static void |
| 35 | +validate (const char *name, |
| 36 | + int fd) |
| 37 | +{ |
| 38 | + bson_uint8_t *buf; |
| 39 | + mongoc_rpc_t rpc; |
| 40 | + bson_int32_t len; |
| 41 | + struct stat st; |
| 42 | + |
| 43 | + if (fstat (fd, &st) != 0) { |
| 44 | + fprintf (stderr, "%s: Failed to fstat.\n", name); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (st.st_size > (100 * 1024 * 1024)) { |
| 49 | + fprintf (stderr, "%s: unreasonable message size\n", name); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + buf = malloc (st.st_size); |
| 54 | + if (buf == NULL) { |
| 55 | + fprintf (stderr, "%s: Failed to malloc %d bytes.\n", |
| 56 | + name, (int)st.st_size); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (st.st_size != read (fd, buf, st.st_size)) { |
| 61 | + fprintf (stderr, "%s: Failed to read %d bytes into buffer.\n", |
| 62 | + name, (int)st.st_size); |
| 63 | + goto cleanup; |
| 64 | + } |
| 65 | + |
| 66 | + memcpy (&len, buf, 4); |
| 67 | + len = BSON_UINT32_FROM_LE (len); |
| 68 | + if (len != st.st_size) { |
| 69 | + fprintf (stderr, "%s is invalid. Invalid Length.\n", name); |
| 70 | + goto cleanup; |
| 71 | + } |
| 72 | + |
| 73 | + if (!_mongoc_rpc_scatter (&rpc, buf, st.st_size)) { |
| 74 | + fprintf (stderr, "%s is invalid. Invalid Format.\n", name); |
| 75 | + goto cleanup; |
| 76 | + } |
| 77 | + |
| 78 | + fprintf (stdout, "%s is valid.\n", name); |
| 79 | + |
| 80 | +cleanup: |
| 81 | + free (buf); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +int |
| 86 | +main (int argc, |
| 87 | + char *argv[]) |
| 88 | +{ |
| 89 | + int fd; |
| 90 | + int i; |
| 91 | + |
| 92 | + if (argc < 2) { |
| 93 | + fprintf (stderr, "usage: %s FILE...\n", argv[0]); |
| 94 | + return EXIT_FAILURE; |
| 95 | + } |
| 96 | + |
| 97 | + for (i = 1; i < argc; i++) { |
| 98 | + fd = open (argv[i], O_RDONLY); |
| 99 | + if (fd == -1) { |
| 100 | + fprintf (stderr, "Failed to open \"%s\"\n", argv[i]); |
| 101 | + continue; |
| 102 | + } |
| 103 | + validate (argv[i], fd); |
| 104 | + close (fd); |
| 105 | + } |
| 106 | + |
| 107 | + return EXIT_SUCCESS; |
| 108 | +} |
0 commit comments