ctrl k
Changes for deployment (#13)
Merged
Leonard Iklé opened 2 years ago
No description
Commits were merged into target branch
  • src/constants.h
    ■ ■ ■ ■
    1 1  #define ZIIT_VERSION "0.0.1"
    2 2  #define ZIIT_DEFAULT_ADDR "127.0.0.1"
    3  -#define ZIIT_DEFAULT_PORT 6784
     3 +#define ZIIT_DEFAULT_PORT "6784"
    4 4   
  • src/init.c
    ■ ■ ■ ■ ■
    1 1  #include "init.h"
    2 2  #include "constants.h"
    3 3  #include <cargs.h>
     4 +#include <ctype.h>
    4 5  #include <errno.h>
    5 6  #include <sodium.h>
    6 7  #include <stdlib.h>
    skipped 81 lines
    88 89  {
    89 90   cag_option_context ctx;
    90 91   cfg_t *repository_cfg;
    91  - const char *repository, *addr;
    92  - char *serverpk, *userpk, *usersk, *answer;
    93  - int port;
     92 + const char *repository;
     93 + char *addr, *port, *serverpk, *userpk, *usersk, *answer;
    94 94   FILE *cfg_file;
    95 95   
    96 96   repository = "default";
    97  - addr = ZIIT_DEFAULT_ADDR;
    98  - port = ZIIT_DEFAULT_PORT;
     97 + addr = NULL;
     98 + port = NULL;
    99 99   serverpk = NULL;
    100 100   userpk = NULL;
    101 101   usersk = NULL;
    skipped 8 lines
    110 110   repository = cag_option_get_value(&ctx);
    111 111   break;
    112 112   case 'a':
    113  - addr = cag_option_get_value(&ctx);
     113 + addr = strdup(cag_option_get_value(&ctx));
    114 114   break;
    115 115   case 'p':
    116  - port = (int)strtol(cag_option_get_value(&ctx), NULL, 10);
     116 + port = strdup(cag_option_get_value(&ctx));
    117 117   break;
    118 118   case 'o':
    119 119   serverpk = strdup(cag_option_get_value(&ctx));
    skipped 10 lines
    130 130   }
    131 131   }
    132 132   
     133 + if (addr == NULL) {
     134 + printf("server address (%s): ", ZIIT_DEFAULT_ADDR);
     135 + readline(&addr);
     136 + if (*addr == '\0') {
     137 + addr = strdup(ZIIT_DEFAULT_ADDR);
     138 + }
     139 + }
     140 + 
     141 + if (port == NULL) {
     142 + printf("server port (%s): ", ZIIT_DEFAULT_PORT);
     143 + readline(&port);
     144 + if (!isalnum(*port)) {
     145 + port = strdup(ZIIT_DEFAULT_PORT);
     146 + }
     147 + }
     148 + 
    133 149   if (serverpk == NULL) {
    134 150   printf("server public key: ");
    135 151   readline(&serverpk);
    skipped 23 lines
    159 175   repository_cfg = cfg_addtsec(cfg, "repository", repository);
    160 176   }
    161 177   cfg_setstr(repository_cfg, "addr", addr);
    162  - cfg_setint(repository_cfg, "port", port);
     178 + cfg_setstr(repository_cfg, "port", port);
    163 179   cfg_setstr(repository_cfg, "server_public_key", serverpk);
    164 180   cfg_setstr(repository_cfg, "user_public_key", userpk);
    165 181   cfg_setstr(repository_cfg, "user_private_key", usersk);
    skipped 13 lines
    179 195   free(serverpk);
    180 196   free(userpk);
    181 197   free(usersk);
     198 + free(port);
     199 + free(addr);
    182 200   
    183 201   return 0;
    184 202  }
    skipped 1 lines
  • src/main.c
    ■ ■ ■ ■
    skipped 15 lines
    16 16  #include <syslog.h>
    17 17   
    18 18  cfg_opt_t repository_opts[] = {CFG_STR("addr", ZIIT_DEFAULT_ADDR, CFGF_NONE),
    19  - CFG_INT("port", ZIIT_DEFAULT_PORT, CFGF_NONE),
     19 + CFG_STR("port", ZIIT_DEFAULT_PORT, CFGF_NONE),
    20 20   CFG_STR("server_public_key", NULL, CFGF_NONE),
    21 21   CFG_STR("user_public_key", NULL, CFGF_NONE),
    22 22   CFG_STR("user_private_key", NULL, CFGF_NONE), CFG_END()};
    skipped 61 lines
  • src/net.c
    ■ ■ ■ ■ ■ ■
    1 1  #include "net.h"
    2 2  #include <arpa/inet.h>
    3 3  #include <confuse.h>
     4 +#include <netdb.h>
    4 5  #include <sodium.h>
    5 6  #include <stdint.h>
    6 7  #include <stdlib.h>
    skipped 140 lines
    147 148   const char *repository)
    148 149  {
    149 150   int result;
    150  - struct sockaddr_in servaddr;
     151 + 
     152 + struct addrinfo hints, *res, *p;
    151 153   cfg_t *server_cfg;
    152  - const char *addr;
    153  - int port;
     154 + const char *addr, *port;
    154 155   const char *serverpkstr, *pkstr, *skstr;
    155 156   uint32_t msgsize;
    156 157   
    skipped 32 lines
    189 190   return -1;
    190 191   }
    191 192   
    192  - // socket create and verification
    193  - con->sockfd = socket(AF_INET, SOCK_STREAM, 0);
    194  - if (con->sockfd == -1) {
    195  - fprintf(stderr, "Could not create socket to connect to server.\n");
     193 + // assign IP, PORT
     194 + addr = cfg_getstr(server_cfg, "addr");
     195 + port = cfg_getstr(server_cfg, "port");
     196 + 
     197 + // resolve address
     198 + memset(&hints, 0, sizeof hints); // make sure the struct is empty
     199 + hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
     200 + hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
     201 + hints.ai_flags = AI_PASSIVE; // fill in my IP for me
     202 + if ((result = getaddrinfo(addr, port, &hints, &res)) != 0) {
     203 + fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(result));
    196 204   return -1;
    197 205   }
    198 206   
    199  - bzero(&servaddr, sizeof(servaddr));
     207 + // connect the client socket to server socket
     208 + // loop through all the results and connect to the first we can
     209 + for (p = res; p != NULL; p = p->ai_next) {
     210 + if ((con->sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) ==
     211 + -1) {
     212 + perror("socket");
     213 + continue;
     214 + }
    200 215   
    201  - // assign IP, PORT
    202  - addr = cfg_getstr(server_cfg, "addr");
    203  - port = (int)cfg_getint(server_cfg, "port");
    204  - servaddr.sin_family = AF_INET;
    205  - servaddr.sin_addr.s_addr = inet_addr(addr);
    206  - servaddr.sin_port = htons(port);
     216 + if (connect(con->sockfd, p->ai_addr, p->ai_addrlen) == -1) {
     217 + perror("connect");
     218 + close(con->sockfd);
     219 + continue;
     220 + }
    207 221   
    208  - // connect the client socket to server socket
    209  - if (connect(con->sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) !=
    210  - 0) {
    211  - fprintf(stderr, "connection with the server failed...\n");
     222 + break; // if we get here, we must have connected successfully
     223 + }
     224 + 
     225 + if (p == NULL) {
    212 226   goto err_connect;
    213 227   }
    214 228   
    skipped 13 lines
    228 242   goto err_init_type;
    229 243   }
    230 244   
     245 + freeaddrinfo(res);
     246 + 
    231 247   return 0;
    232 248   
    233 249  err_init_type:
    skipped 1 lines
    235 251  err_init_recv:
    236 252   close(con->sockfd);
    237 253  err_connect:
     254 + freeaddrinfo(res);
    238 255   return -1;
    239 256  }
    240 257   
    skipped 35 lines
pull request 1 of 1
Submitter Leonard Iklé
Target master
Source changes-for-deployment
Reviewers
Assignees
Merge Strategy
Create Merge Commit
Watchers (2)
Reference
pull request acosom/ziit/ziit-cli#13
Please wait...
Page is in error, reload to recover