Skip to content

Commit 2e05df2

Browse files
author
Christian Hergert
committed
examples: add example of internal API to validate an RPC.
1 parent b6898f0 commit 2e05df2

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ config.*
88
configure
99
depcomp
1010
.deps
11+
example-client
1112
INSTALL
1213
tags
1314
index-source
@@ -21,13 +22,13 @@ Makefile
2122
Makefile.in
2223
missing
2324
mongoc-ping
25+
mongoc-rpc-validate
2426
mongoc-stat
2527
mongoc-tail
2628
*.o
2729
*.pc
2830
stamp-h1
2931
*.swp
30-
example-client
3132
repltest1
3233
shardtest1
3334
shardtest2

examples/Makefile.include

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ mongoc_ping_CPPFLAGS = -I$(top_srcdir)/mongoc $(BSON_CFLAGS)
1414
mongoc_ping_LDADD = libmongoc-1.0.la $(BSON_LIBS)
1515

1616

17+
noinst_PROGRAMS += mongoc-rpc-validate
18+
mongoc_rpc_validate_SOURCES = $(top_srcdir)/examples/mongoc-rpc-validate.c
19+
mongoc_rpc_validate_CPPFLAGS = -I$(top_srcdir)/mongoc $(BSON_CFLAGS)
20+
mongoc_rpc_validate_LDADD = libmongoc-priv.la $(BSON_LIBS)
21+
22+
1723
noinst_PROGRAMS += mongoc-tail
1824
mongoc_tail_SOURCES = $(top_srcdir)/examples/mongoc-tail.c
1925
mongoc_tail_CPPFLAGS = -I$(top_srcdir)/mongoc $(BSON_CFLAGS)

examples/mongoc-rpc-validate.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)