@@ -63,6 +63,7 @@ struct cloud_struct {
6363 char * ip ;
6464 uint16_t port ;
6565 char * request_sshkey_path ;
66+ char * request_hostname_path ;
6667 char * request_userdata_path ;
6768 char * cloud_config_header ;
6869};
@@ -74,6 +75,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
7475 "169.254.169.254" ,
7576 80 ,
7677 "/latest/meta-data/public-keys/0/openssh-key" ,
78+ "/latest/meta-data/hostname" ,
7779 "/latest/user-data" ,
7880 "#cloud-config\n" \
7981 "users:\n" \
@@ -87,6 +89,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
8789 80 ,
8890 "/opc/v1/instance/metadata/ssh_authorized_keys" ,
8991 NULL ,
92+ NULL ,
9093 "#cloud-config\n" \
9194 "users:\n" \
9295 " - name: opc\n" \
@@ -100,6 +103,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
100103 80 ,
101104 "/latest/meta-data/public-keys/0/openssh-key" ,
102105 NULL ,
106+ NULL ,
103107 "#cloud-config\n" \
104108 "users:\n" \
105109 " - name: tencent\n" \
@@ -112,6 +116,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
112116 80 ,
113117 "/latest/meta-data/public-keys/0/openssh-key" ,
114118 NULL ,
119+ NULL ,
115120 "#cloud-config\n" \
116121 "users:\n" \
117122 " - name: aliyun\n" \
@@ -123,6 +128,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
123128 "metadata.platformequinix.com" ,
124129 80 ,
125130 "/2009-04-04/meta-data/public-keys" ,
131+ "/2009-04-04/meta-data/hostname" ,
126132 "/userdata" ,
127133 "#cloud-config\n" \
128134 "users:\n" \
@@ -135,6 +141,7 @@ static struct cloud_struct config[MAX_CONFIGS] = {
135141 "127.0.0.254" ,
136142 8123 ,
137143 "/public-keys" ,
144+ "/hostname" ,
138145 "/user-data" ,
139146 "#cloud-config\n" \
140147 "users:\n" \
@@ -226,7 +233,7 @@ static int write_lines(int out, FILE *f, size_t cl, const char *prefix)
226233int main (int argc , char * argv []) {
227234 int conf = -1 ;
228235 int sockfd ;
229- char * request , * request2 ;
236+ char * request , * request2 , * request3 ;
230237 char * outpath ;
231238 int n = 0 ;
232239
@@ -407,17 +414,80 @@ int main(int argc, char *argv[]) {
407414 }
408415 }
409416
417+ /* next, get hostname */
418+ if (!config [conf ].request_hostname_path )
419+ goto user_data ;
420+
421+ if (asprintf (& request2 , "GET %s HTTP/1.1\r\nhost: %s \r\nConnection: keep-alive\r\n\r\n" ,
422+ config [conf ].request_hostname_path , config [conf ].ip ) < 0 ) {
423+ FAIL ("asprintf" );
424+ }
425+ len = strlen (request2 );
426+
427+ if (write (sockfd , request2 , len ) < (ssize_t )len ) {
428+ close (sockfd );
429+ FAIL ("write()" );
430+ }
431+
432+ f = fdopen (sockfd , "r" );
433+ if (!f ) {
434+ close (sockfd );
435+ FAIL ("fdopen()" );
436+ }
437+
438+ /* parse/discard the header and body */
439+ result = parse_headers (f , & cl );
440+ if (result == 0 ) {
441+ /* error - exit */
442+ fclose (f );
443+ close (out );
444+ FAIL ("parse_headers()" );
445+ }
446+
447+ /* don't write part #2 if 404 or some non-error */
448+ if ((result != 2 ) && (write_lines (out , f , cl , "hostname: " ) != 0 )) {
449+ close (out );
450+ fclose (f );
451+ unlink (outpath );
452+ FAIL ("write_lines()" );
453+ }
454+
455+ /* cleanup */
456+ fclose (f );
457+
458+ /* reopen socket */
459+ sockfd = socket (AF_INET , SOCK_STREAM , 0 );
460+ if (sockfd < 0 ) {
461+ FAIL ("socket()" );
462+ }
463+
464+ n = 0 ;
465+ for (;;) {
466+ int r = connect (sockfd , (struct sockaddr * )& server , sizeof (server ));
467+ if (r == 0 ) {
468+ break ;
469+ }
470+ if ((errno != EAGAIN ) && (errno != ENETUNREACH ) && (errno != ETIMEDOUT )) {
471+ FAIL ("connect()" );
472+ }
473+ nanosleep (& ts , NULL );
474+ if (++ n > 200 ) { /* 10 secs */
475+ FAIL ("timeout in connect()" );
476+ }
477+ }
478+
479+ user_data :
410480 /* next, get user-data */
411481 if (!config [conf ].request_userdata_path )
412482 goto finish ;
413483
414- if (asprintf (& request2 , "GET %s HTTP/1.1\r\nhost: %s \r\nConnection: keep-alive\r\n\r\n" ,
484+ if (asprintf (& request3 , "GET %s HTTP/1.1\r\nhost: %s \r\nConnection: keep-alive\r\n\r\n" ,
415485 config [conf ].request_userdata_path , config [conf ].ip ) < 0 ) {
416486 FAIL ("asprintf" );
417487 }
418- len = strlen (request2 );
488+ len = strlen (request3 );
419489
420- if (write (sockfd , request2 , len ) < (ssize_t )len ) {
490+ if (write (sockfd , request3 , len ) < (ssize_t )len ) {
421491 close (sockfd );
422492 FAIL ("write()" );
423493 }
0 commit comments