io.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #define _GNU_SOURCE 1 // Needed for getline to avoid implicit function declaration, see `man 3 getline`
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include "io.h"
  6. #include "parse.h"
  7. void getline_wrapper(struct arguments *args) {
  8. size_t bufsize = 0;
  9. char *line = NULL;
  10. errno = 0;
  11. int getline_retval = getline(&line, &bufsize, args->ist);
  12. if(line == NULL || line == 0 || feof(args->ist) || errno != 0 || getline_retval < 0) {
  13. if(errno != 0) {
  14. printf("Error %d (%s) getting line for reading.\n", errno, strerror(errno));
  15. }
  16. free(line);
  17. return;
  18. }
  19. char start[256]; // Make it big enough for ipv6
  20. strncpy(start, line, 256);
  21. char *end = NULL;
  22. int ret;
  23. if((ret = split_range(start, &end)) != 0) {
  24. fprintf(stderr, "Error when attempting to split range %s into start and end parts.\n", line);
  25. exit(1);
  26. }
  27. in_addr_t lower;
  28. if((lower = inet_network(start)) == -1) {
  29. fprintf(stderr, "Lower address invalid\n");
  30. exit(1);
  31. }
  32. in_addr_t upper;
  33. if((upper = inet_network(end)) == -1) {
  34. fprintf(stderr, "Upper address invalid\n");
  35. exit(1);
  36. }
  37. if(upper < lower) {
  38. fprintf(stderr, "Incorrect order in range specification? (%s > %s)\n", start, end);
  39. exit(1);
  40. }
  41. deaggregate(lower, upper);
  42. free(line);
  43. }
  44. void no_read(struct arguments *args) {
  45. printf("Unknown mode. Not reading any lines.\n");
  46. }
  47. void sigint_handler(int sig) {
  48. break_print_loop = true;
  49. }