services.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include <sys/types.h>
  23. #include <regex.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. /**
  28. * parses /etc/services so we know which ports are service ports
  29. */
  30. void
  31. parse_services(const char *file, tcpr_services_t *services)
  32. {
  33. FILE *service = NULL;
  34. char service_line[MAXLINE], port[10], proto[10];
  35. regex_t preg;
  36. uint16_t portc;
  37. size_t nmatch = 3;
  38. regmatch_t pmatch[3];
  39. static const char regex[] = "([0-9]+)/(tcp|udp)"; /* matches the port as pmatch[1], service pmatch[2] */
  40. assert(file);
  41. assert(services);
  42. dbgx(1, "Parsing %s", file);
  43. memset(service_line, '\0', MAXLINE);
  44. /* mark all ports not a service */
  45. memset(&services->tcp[0], '\0', sizeof(services->tcp));
  46. memset(&services->udp[0], '\0', sizeof(services->udp));
  47. if ((service = fopen(file, "r")) == NULL) {
  48. errx(-1, "Unable to open service file: %s\n%s", file, strerror(errno));
  49. }
  50. /* compile our regexes */
  51. if ((regcomp(&preg, regex, REG_ICASE|REG_EXTENDED)) != 0) {
  52. errx(-1, "Unable to compile regex: %s", regex);
  53. }
  54. /* parse the entire file */
  55. while ((fgets(service_line, MAXLINE, service)) != NULL) {
  56. /* zero out our vars */
  57. memset(port, '\0', 10);
  58. memset(proto, '\0', 10);
  59. portc = 0;
  60. dbgx(4, "Processing: %s", service_line);
  61. /* look for format of 1234/tcp */
  62. if ((regexec(&preg, service_line, nmatch, pmatch, 0)) == 0) { /* matches */
  63. if (nmatch < 2) {
  64. err(-1, "WTF? I matched the line, but I don't know where!");
  65. }
  66. /* strip out the port & proto from the line */
  67. strncpy(port, &service_line[pmatch[1].rm_so], (pmatch[1].rm_eo - pmatch[1].rm_so));
  68. strncpy(proto, &service_line[pmatch[2].rm_so], (pmatch[2].rm_eo - pmatch[2].rm_so));
  69. /* convert port[] into an integer */
  70. portc = (uint16_t)atoi(port);
  71. /* update appropriate service array with the server port */
  72. if (strcmp(proto, "tcp") == 0) {
  73. dbgx(3, "Setting TCP/%d as a server port", portc);
  74. services->tcp[portc] = 1; /* mark it as a service port */
  75. } else if (strcmp(proto, "udp") == 0) {
  76. dbgx(3, "Setting UDP/%d as a server port", portc);
  77. services->udp[portc] = 1;
  78. } else {
  79. warnx("Skipping unknown protocol service %s/%d", proto, portc);
  80. }
  81. }
  82. }
  83. fclose(service);
  84. }