nbd-client: Add support for setting /sys/block/nbdN/backend
[nbd/ericb.git] / tests / code / clientacl.c
blobda614fd02fd0255acb3ee16419518a9b7d115f29
1 #include <nbdsrv.h>
2 #include <arpa/inet.h>
3 #include <assert.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netdb.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdbool.h>
11 #include <netinet/in.h>
13 bool do_test(char* address, char* netmask) {
14 struct addrinfo hints;
15 struct addrinfo *res, *tmp;
16 char buf[1024];
17 int err;
19 printf("Doing test for %s, netmask %s\n", address, netmask);
20 memset(&hints, 0, sizeof(hints));
21 hints.ai_family = AF_UNSPEC;
22 hints.ai_flags = AI_NUMERICHOST;
24 if((err = getaddrinfo(address, NULL, &hints, &res))) {
25 fprintf(stderr, "E: %s\n", gai_strerror(err));
26 exit(EXIT_FAILURE);
28 tmp = res;
29 while(res) {
30 if((err = getnameinfo((struct sockaddr*)res->ai_addr, res->ai_addrlen, buf,
31 sizeof (buf), NULL, 0, NI_NUMERICHOST))) {
32 fprintf(stderr, "E: %s\n", gai_strerror(err));
33 exit(EXIT_FAILURE);
36 printf("Found %s\n", buf);
38 if(address_matches(netmask, (struct sockaddr*)res->ai_addr, NULL)) {
39 printf("Yes!\n");
40 freeaddrinfo(tmp);
41 return true;
42 } else {
43 printf("oh noes!\n");
45 res = res->ai_next;
47 freeaddrinfo(tmp);
48 return false;
51 int main(void) {
52 if(!do_test("192.168.0.1", "192.168.1.1/23")) {
53 return 1;
55 if(!do_test("192.168.0.1", "192.168.0.1/24")) {
56 return 1;
58 if(!do_test("::ffff:192.168.200.1", "192.168.200.1/24")) {
59 return 1;
61 if(do_test("::ffff:192.168.200.1", "192.168.100.1/24")) {
62 return 1;
64 if(do_test("192.168.200.1", "192.168.0.0/24")) {
65 return 1;
67 if(do_test("192.168.200.1", "192.168.0.0/23")) {
68 return 1;
71 return 0;