Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit e4aad1b

Browse files
committed
support dns search & options
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 1849735 commit e4aad1b

File tree

3 files changed

+90
-20
lines changed

3 files changed

+90
-20
lines changed

src/hyper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ struct hyper_pod {
2323
struct hyper_route *rt;
2424
struct portmapping_white_list *portmap_white_lists;
2525
char **dns;
26+
char **dns_search;
27+
char **dns_option;
2628
struct list_head containers;
2729
struct list_head exec_head;
2830
char *hostname;
@@ -31,6 +33,8 @@ struct hyper_pod {
3133
uint32_t i_num;
3234
uint32_t r_num;
3335
uint32_t d_num;
36+
uint32_t dsearch_num;
37+
uint32_t doption_num;
3438
/* how many containers are running */
3539
uint32_t remains;
3640
int req_destroy;

src/net.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,61 @@ int hyper_cmd_setup_route(char *json, int length) {
677677
return ret;
678678
}
679679

680+
int hyper_write_dns_file(int fd, char *field, char **data, int num)
681+
{
682+
int i = 0, len = 0, ret = -1, size;
683+
char *buf = NULL;
684+
685+
if (num == 0)
686+
return 0;
687+
688+
size = strlen(field);
689+
buf = malloc(size);
690+
if (buf == NULL) {
691+
fprintf(stderr, "fail to malloc buff for %s\n", field);
692+
goto out;
693+
}
694+
memcpy(buf, field, size);
695+
for (i = 0; i < num; i++) {
696+
int new_len = strlen(data[i]) + 1 + 1;
697+
char *format = " %s";
698+
if (i + 1 == num) {
699+
new_len += 1;
700+
format = " %s\n";
701+
}
702+
buf = realloc(buf, size + new_len);
703+
if (buf == NULL) {
704+
fprintf(stderr, "fail to realloc buff for %s\n", field);
705+
goto out;
706+
}
707+
if (snprintf(buf + size, new_len, format, data[i]) < 0) {
708+
fprintf(stderr, "sprintf search entry failed\n");
709+
goto out;
710+
}
711+
fprintf(stdout, "%s: data: %s\n", field, buf);
712+
size += new_len;
713+
}
714+
715+
while (len < size) {
716+
i = write(fd, buf + len, size - len);
717+
if (i < 0) {
718+
perror("fail to write resolv.conf");
719+
goto out;
720+
}
721+
len += i;
722+
}
723+
724+
ret = 0;
725+
out:
726+
free(buf);
727+
return ret;
728+
}
729+
680730
int hyper_setup_dns(struct hyper_pod *pod)
681731
{
682732
int i, fd, ret = -1;
683733
char buf[28];
734+
int len = 0, size = 0;
684735

685736
if (pod->dns == NULL)
686737
return 0;
@@ -693,9 +744,9 @@ int hyper_setup_dns(struct hyper_pod *pod)
693744
}
694745

695746
for (i = 0; i < pod->d_num; i++) {
696-
int size = snprintf(buf, sizeof(buf), "nameserver %s\n", pod->dns[i]);
697-
int len = 0, l;
698-
747+
int l;
748+
len = 0;
749+
size = snprintf(buf, sizeof(buf), "nameserver %s\n", pod->dns[i]);
699750
if (size < 0) {
700751
fprintf(stderr, "sprintf resolv.conf entry failed\n");
701752
goto out;
@@ -711,6 +762,11 @@ int hyper_setup_dns(struct hyper_pod *pod)
711762
}
712763
}
713764

765+
if (hyper_write_dns_file(fd, "search", pod->dns_search, pod->dsearch_num) < 0 ||
766+
hyper_write_dns_file(fd, "options", pod->dns_option, pod->doption_num) < 0) {
767+
goto out;
768+
}
769+
714770
ret = 0;
715771
out:
716772
close(fd);

src/parse.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,30 +1060,33 @@ int hyper_parse_setup_routes(struct hyper_route **routes, uint32_t *r_num, char
10601060
return ret;
10611061
}
10621062

1063-
static int hyper_parse_dns(struct hyper_pod *pod, char *json, jsmntok_t *toks)
1063+
static int hyper_parse_string_array(char *json, jsmntok_t *toks, char *field,
1064+
char ***data, unsigned int *num)
10641065
{
10651066
int i = 0, j;
1067+
char **values;
10661068

10671069
if (toks[i].type != JSMN_ARRAY) {
1068-
dbg_pr(stdout, "Dns format incorrect\n");
1070+
dbg_pr(stdout, "%s format incorrect\n", field);
10691071
return -1;
10701072
}
10711073

1072-
pod->d_num = toks[i].size;
1073-
dbg_pr(stdout, "dns count %d\n", pod->d_num);
1074+
*num = toks[i].size;
1075+
dbg_pr(stdout, "%s count %d\n", field, *num);
10741076

1075-
pod->dns = calloc(pod->d_num, sizeof(*pod->dns));
1076-
if (pod->dns == NULL) {
1077-
dbg_pr(stdout, "alloc memory for dns failed\n");
1077+
values = calloc(*num, sizeof(*values));
1078+
if (data == NULL) {
1079+
dbg_pr(stdout, "alloc memory for %s failed\n", field);
10781080
return -1;
10791081
}
10801082

10811083
i++;
1082-
for (j = 0; j < pod->d_num; j++, i++) {
1083-
pod->dns[j] = json_token_str(json, &toks[i]);
1084-
dbg_pr(stdout, "pod dns %d: %s\n", j, pod->dns[j]);
1084+
for (j = 0; j < *num; j++, i++) {
1085+
values[j] = json_token_str(json, &toks[i]);
1086+
dbg_pr(stdout, "%s option %d: %s\n", field, j, values[j]);
10851087
}
10861088

1089+
*data = values;
10871090
return i;
10881091
}
10891092

@@ -1247,25 +1250,34 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
12471250
next = hyper_parse_containers(pod, json, &toks[++i]);
12481251
if (next < 0)
12491252
goto out;
1250-
12511253
i += next;
12521254
} else if (json_token_streq(json, t, "interfaces") && t->size == 1) {
12531255
next = hyper_parse_interfaces(pod, json, &toks[++i]);
12541256
if (next < 0)
12551257
goto out;
1256-
12571258
i += next;
12581259
} else if (json_token_streq(json, t, "routes") && t->size == 1) {
12591260
next = hyper_parse_routes(&pod->rt, &pod->r_num, json, &toks[++i]);
12601261
if (next < 0)
12611262
goto out;
1262-
12631263
i += next;
12641264
} else if (json_token_streq(json, t, "dns") && t->size == 1) {
1265-
next = hyper_parse_dns(pod, json, &toks[++i]);
1265+
next = hyper_parse_string_array(json, &toks[++i], "dns",
1266+
&pod->dns, &pod->d_num);
1267+
if (next < 0)
1268+
goto out;
1269+
i += next;
1270+
} else if (json_token_streq(json, t, "dnsSearch") && t->size == 1) {
1271+
next = hyper_parse_string_array(json, &toks[++i], "dns search",
1272+
&pod->dns_search, &pod->dsearch_num);
1273+
if (next < 0)
1274+
goto out;
1275+
i += next;
1276+
} else if (json_token_streq(json, t, "dnsOptions") && t->size == 1) {
1277+
next = hyper_parse_string_array(json, &toks[++i], "dns option",
1278+
&pod->dns_option, &pod->doption_num);
12661279
if (next < 0)
12671280
goto out;
1268-
12691281
i += next;
12701282
} else if (json_token_streq(json, t, "shareDir") && t->size == 1) {
12711283
pod->share_tag = (json_token_str(json, &toks[++i]));
@@ -1282,15 +1294,13 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
12821294
next = hyper_parse_portmapping_whitelist(pod, json, &toks[++i]);
12831295
if (next < 0)
12841296
goto out;
1285-
12861297
i += next;
12871298
} else {
12881299
hyper_print_unknown_key(json, &toks[i]);
12891300
next = -1;
12901301
break;
12911302
}
12921303
}
1293-
12941304
out:
12951305
free(toks);
12961306
return next;

0 commit comments

Comments
 (0)