MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
autopilot.c File Reference
#include "mbconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include "ac/getopt.h"
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/un.h>
#include <string.h>
#include "sock.h"
Include dependency graph for autopilot.c:

Go to the source code of this file.

Functions

static void keys (FILE *fh)
 
static void usage (void)
 
void reset_input_mode (void)
 
void set_input_mode (void)
 
int send_message (const char *message)
 
int main (int argc, char *argv[])
 

Variables

const unsigned short int PORT = 5555
 
const char * SERVERHOST = "localhost"
 
const char * SERVERPATH = "/var/mbdyn/mbdyn.sock"
 
struct termios saved_attributes
 
static int sasl = 0
 
const char * path = ((void*)0)
 
const char * host = ((void*)0)
 
unsigned short int port = 0
 

Function Documentation

static void keys ( FILE *  fh)
static

Definition at line 67 of file autopilot.c.

Referenced by main(), and usage().

68 {
69  fprintf(fh,
70  "\tkeys:\n"
71  "\t\t'i':\tswitches the incremental mode on\n"
72  "\t\t'p':\tincrements the drive\n"
73  "\t\t'm':\tdecrements the drive\n"
74  "\t\t'^D':\tquits\n\n");
75 }
int main ( int  argc,
char *  argv[] 
)

Definition at line 184 of file autopilot.c.

References c, getopt(), host, keys(), msg, optarg, optind, path, PORT, port, read(), send_message(), SERVERHOST, set_input_mode(), usage(), and verbose.

185 {
186  char *user = NULL;
187  char *cred = NULL;
188 
189  char *increment = "1.";
190  char *label = NULL;
191 
192  char c;
193 
194  char *auth = NULL;
195  char *inc = NULL;
196  char *plus = NULL;
197  char *minus = NULL;
198 
199  char *mech = NULL;
200 
201  int verbose = 0;
202 
203  host = SERVERHOST;
204  port = PORT;
205 
206  while (1) {
207  int opt;
208 
209  opt = getopt(argc, argv, "D:h:m:p:P:Svw:Wx:");
210 
211  if (opt == EOF) {
212  break;
213  }
214 
215  switch (opt) {
216  case 'D':
217  user = optarg;
218  break;
219 
220  case 'h':
221  host = optarg;
222  break;
223 
224  case 'm':
225  mech = optarg;
226 #ifndef HAVE_SASL2
227  fprintf(stderr, "SASL not supported\n");
228 #endif /* ! HAVE_SASL2 */
229  break;
230 
231  case 'p':
232  port = atoi(optarg);
233  break;
234 
235  case 'P':
236  path = optarg;
237  break;
238 
239  case 'S':
240  sasl++;
241 #ifndef HAVE_SASL2
242  fprintf(stderr, "SASL not supported\n");
243  exit(EXIT_FAILURE);
244 #endif /* ! HAVE_SASL2 */
245  break;
246 
247  case 'v':
248  verbose++;
249  break;
250 
251  case 'w':
252  cred = strdup(optarg);
253  break;
254 
255  case 'W': {
256  char *tmp = getpass("password: ");
257 
258  if (tmp) {
259  cred = strdup(tmp);
260  memset(tmp, '\0', strlen(tmp));
261  }
262  break;
263  }
264 
265  case 'x':
266  increment = optarg;
267  break;
268  }
269  }
270 
271  if (argc - optind < 1) {
272  usage();
273  exit(EXIT_SUCCESS);
274  }
275 
276  label = argv[optind];
277 
278  /* messaggi: */
279  if (!sasl) {
280  if (user) {
281  size_t l = strlen(user);
282  if (cred) {
283  l += strlen(cred) + 7 + 11;
284 
285  auth = (char *)calloc(sizeof(char), l + 1);
286  snprintf(auth, l + 1, "user: %s\npassword: %s\n", user, cred);
287  } else {
288  l += 7;
289 
290  auth = (char *)calloc(sizeof(char), l + 1);
291  snprintf(auth, l + 1, "user: %s\n", user);
292  }
293  }
294  } else {
295 #ifdef HAVE_SASL2
296  if (verbose) {
297  printf("initializing SASL data...\n");
298  }
299 
300  mbdyn_sasl.use_sasl = MBDYN_SASL_CLIENT;
301  mbdyn_sasl.sasl_flags = MBDYN_SASL_FLAG_CRITICAL | MBDYN_SASL_FLAG_USERAUTHZ;
302  mbdyn_sasl.sasl_mech = mech;
303  mbdyn_sasl.sasl_user = user;
304  mbdyn_sasl.sasl_cred = cred;
305  mbdyn_sasl.sasl_hostname = host;
306 
307  if (mbdyn_sasl_client_init(&mbdyn_sasl) != SASL_OK) {
308  fprintf(stderr, "SASL init failed\n");
309  exit(EXIT_FAILURE);
310  }
311 #endif /* HAVE_SASL2 */
312  }
313 
314  if (auth) {
315  size_t l = strlen(auth) + 8 + strlen(label) + 9 + 2;
316 
317  inc = (char *)calloc(sizeof(char), l + 1);
318  snprintf(inc, l, "%slabel: %s\ninc: yes\n.\n", auth, label);
319 
320  l = strlen(auth) + 8 + strlen(label) + 8 + strlen(increment) + 2;
321  plus = (char *)calloc(sizeof(char), l + 1);
322  minus = (char *)calloc(sizeof(char), l + 1 + 1);
323  snprintf(plus, l + 1, "%slabel: %s\nvalue: %s\n.\n", auth, label, increment);
324  snprintf(minus, l + 2, "%slabel: %s\nvalue: -%s\n.\n", auth, label, increment);
325 
326  } else {
327  size_t l = 8 + strlen(label) + 9 + 2;
328 
329  inc = (char *)calloc(sizeof(char), l + 1);
330  snprintf(inc, l + 1, "label: %s\ninc: yes\n.\n", label);
331 
332  l = 8 + strlen(label) + 8 + strlen(increment) + 2;
333  plus = (char *)calloc(sizeof(char), l + 1);
334  minus = (char *)calloc(sizeof(char), l + 1 + 1);
335  snprintf(plus, l + 1, "label: %s\nvalue: %s\n.\n", label, increment);
336  snprintf(minus, l + 2, "label: %s\nvalue: -%s\n.\n", label, increment);
337  }
338 
339  set_input_mode();
340 
341  if (verbose) {
342  fprintf(stdout, "Connecting to host %s:%d\n", host, port);
343  if (user) {
344  if (cred) {
345  fprintf(stdout, "Accounting as \"%s\" (with creds)\n", user);
346 
347  } else {
348  fprintf(stdout, "Accounting as \"%s\"\n", user);
349  }
350  }
351  fprintf(stdout, "Incrementing drive %s by %s\n", label, increment);
352  keys(stdout);
353  }
354 
355  while (1) {
356  size_t i;
357 
358  i = read(STDIN_FILENO, &c, 1);
359 
360  if (i > 0) {
361  char *msg = NULL;
362 
363  if (c == '\004') { /* `C-d' */
364  break;
365  }
366 
367  switch (c) {
368  case 'i':
369  msg = inc;
370  break;
371 
372  break;
373 
374  case 'p':
375  msg = plus;
376  break;
377 
378  case 'm':
379  msg = minus;
380  break;
381 
382  }
383 
384  if (send_message(msg) == -1) {
385  fprintf(stderr, "unable to connect to host %s:%d\n",
386  host, port);
387  }
388  }
389  }
390 
391  if (cred) {
392  free(cred);
393  }
394 
395  exit(EXIT_SUCCESS);
396 }
int optind
Definition: getopt.c:72
static int sasl
Definition: autopilot.c:136
const unsigned short int PORT
Definition: autopilot.c:62
static void usage(void)
Definition: autopilot.c:78
const char * SERVERHOST
Definition: autopilot.c:63
static void keys(FILE *fh)
Definition: autopilot.c:67
int send_message(const char *message)
Definition: autopilot.c:146
const char * host
Definition: autopilot.c:142
void set_input_mode(void)
Definition: autopilot.c:111
static std::stack< cleanup * > c
Definition: cleanup.cc:59
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
static int verbose
Definition: ann_tr.c:51
struct mbrtai_msg_t msg
char * optarg
Definition: getopt.c:74
unsigned short int port
Definition: autopilot.c:143
static void * read(LoadableElem *pEl, DataManager *pDM, MBDynParser &HP)
const char * path
Definition: autopilot.c:141

Here is the call graph for this function:

void reset_input_mode ( void  )

Definition at line 105 of file autopilot.c.

References saved_attributes.

Referenced by set_input_mode().

106 {
107  tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
108 }
struct termios saved_attributes
Definition: autopilot.c:102
int send_message ( const char *  message)

Definition at line 146 of file autopilot.c.

References host, mbdyn_make_inet_socket(), mbdyn_make_named_socket(), path, and port.

Referenced by main().

147 {
148  int sock;
149  struct sockaddr_in peer_name;
150  FILE *fd;
151 
152  /* Create the socket. */
153  if (path) {
154  sock = mbdyn_make_named_socket(0, path, 0, NULL);
155  } else {
156  sock = mbdyn_make_inet_socket(0, host, port, 0, NULL);
157  }
158  if (sock < 0) {
159  return -1;
160  }
161 
162  /* Connect to the server. */
163  if (0 > connect(sock, (struct sockaddr *) &peer_name,
164  sizeof(peer_name))) {
165  return -1;
166  }
167 
168  if (sasl) {
169 #ifdef HAVE_SASL2
170  if (mbdyn_sasl_client_auth(sock, NULL, &mbdyn_sasl) != SASL_OK) {
171  return -1;
172  }
173 #endif /* HAVE_SASL2 */
174  }
175 
176  fd = fdopen(sock, "w");
177  fputs(message, fd);
178  fclose(fd);
179 
180  return 0;
181 }
static int sasl
Definition: autopilot.c:136
int mbdyn_make_inet_socket(struct sockaddr_in *name, const char *hostname, unsigned short int port, int dobind, int *perror)
const char * host
Definition: autopilot.c:142
int mbdyn_make_named_socket(struct sockaddr_un *name, const char *path, int dobind, int *perror)
unsigned short int port
Definition: autopilot.c:143
const char * path
Definition: autopilot.c:141

Here is the call graph for this function:

void set_input_mode ( void  )

Definition at line 111 of file autopilot.c.

References reset_input_mode(), and saved_attributes.

Referenced by main().

112 {
113  struct termios tattr;
114 
115  /* Make sure stdin is a terminal. */
116  if (!isatty(STDIN_FILENO)) {
117  fprintf(stderr, "Not a terminal.\n");
118  exit(EXIT_FAILURE);
119  }
120 
121  /* Save the terminal attributes so we can restore them later. */
122  tcgetattr(STDIN_FILENO, &saved_attributes);
123  atexit(reset_input_mode);
124 
125  /* Set the funny terminal modes. */
126  tcgetattr(STDIN_FILENO, &tattr);
127  tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
128  tattr.c_cc[VMIN] = 0;
129  tattr.c_cc[VTIME] = 0;
130  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr) < 0) {
131  perror("tcsetattr");
132  exit(EXIT_FAILURE);
133  }
134 }
void reset_input_mode(void)
Definition: autopilot.c:105
struct termios saved_attributes
Definition: autopilot.c:102

Here is the call graph for this function:

static void usage ( void  )
static

Definition at line 78 of file autopilot.c.

References keys().

Referenced by main().

79 {
80  fprintf(stderr,
81  "\n\tusage: autopilot [h:p:D:vw:Wx:] <label>\n\n"
82  "\t\t-D <user>\tuser name\n"
83  "\t\t-h <host>\thost name\n"
84  "\t\t-m <mech>\tSASL mechanism(s)\n"
85  "\t\t-p <port>\tport number\n"
86  "\t\t-P <path>\tpath for named socked\n"
87  "\t\t-S\t\tuse SASL"
88 #ifndef HAVE_SASL2
89  " (not supported)"
90 #endif /* ! HAVE_SASL2 */
91  "\n"
92  "\t\t-v\t\tverbose\n"
93  "\t\t-w <cred>\tuser credentials\n"
94  "\t\t-W\t\tprompt for user credentials\n"
95  "\t\t-x <value>\tincrement\n"
96  "\n"
97  "\t\t<label>:\tfile drive (base 1) index to modify\n\n");
98  keys(stderr);
99 }
static void keys(FILE *fh)
Definition: autopilot.c:67

Here is the call graph for this function:

Variable Documentation

const char* path = ((void*)0)
const unsigned short int PORT = 5555

Definition at line 62 of file autopilot.c.

Referenced by main().

unsigned short int port = 0

Definition at line 143 of file autopilot.c.

Referenced by main(), ReadExtSocketHandler(), and send_message().

int sasl = 0
static

Definition at line 136 of file autopilot.c.

struct termios saved_attributes

Definition at line 102 of file autopilot.c.

Referenced by reset_input_mode(), and set_input_mode().

const char* SERVERHOST = "localhost"

Definition at line 63 of file autopilot.c.

Referenced by main().

const char* SERVERPATH = "/var/mbdyn/mbdyn.sock"

Definition at line 64 of file autopilot.c.