services.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 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 <regex.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. /**
  27. * parses /etc/services so we know which ports are service ports
  28. */
  29. void
  30. parse_services(const char *file, tcpr_services_t *services)
  31. {
  32. FILE *service = NULL;
  33. char service_line[MAXLINE], port[10], proto[10];
  34. regex_t preg;
  35. size_t nmatch = 3;
  36. regmatch_t pmatch[3];
  37. static const char regex[] = "([0-9]+)/(tcp|udp)"; /* matches the port as pmatch[1], service pmatch[2] */
  38. assert(file);
  39. assert(services);
  40. dbgx(1, "Parsing %s", file);
  41. memset(service_line, '\0', MAXLINE);
  42. /* mark all ports not a service */
  43. memset(&services->tcp[0], '\0', sizeof(services->tcp));
  44. memset(&services->udp[0], '\0', sizeof(services->udp));
  45. if ((service = fopen(file, "r")) == NULL) {
  46. errx(-1, "Unable to open service file: %s\n%s", file, strerror(errno));
  47. }
  48. /* compile our regexes */
  49. if ((regcomp(&preg, regex, REG_ICASE|REG_EXTENDED)) != 0) {
  50. errx(-1, "Unable to compile regex: %s", regex);
  51. }
  52. /* parse the entire file */
  53. while ((fgets(service_line, MAXLINE, service)) != NULL) {
  54. /* zero out our vars */
  55. memset(port, '\0', 10);
  56. memset(proto, '\0', 10);
  57. dbgx(4, "Processing: %s", service_line);
  58. /* look for format of 1234/tcp */
  59. if ((regexec(&preg, service_line, nmatch, pmatch, 0)) == 0) { /* matches */
  60. uint16_t portc;
  61. /* strip out the port & proto from the line */
  62. strncpy(port, &service_line[pmatch[1].rm_so], (pmatch[1].rm_eo - pmatch[1].rm_so));
  63. strncpy(proto, &service_line[pmatch[2].rm_so], (pmatch[2].rm_eo - pmatch[2].rm_so));
  64. /* convert port[] into an integer */
  65. portc = (uint16_t)strtol(port, NULL, 10);
  66. /* update appropriate service array with the server port */
  67. if (strcmp(proto, "tcp") == 0) {
  68. dbgx(3, "Setting TCP/%d as a server port", portc);
  69. services->tcp[portc] = 1; /* mark it as a service port */
  70. } else if (strcmp(proto, "udp") == 0) {
  71. dbgx(3, "Setting UDP/%d as a server port", portc);
  72. services->udp[portc] = 1;
  73. } else {
  74. warnx("Skipping unknown protocol service %s/%d", proto, portc);
  75. }
  76. }
  77. }
  78. regfree(&preg);
  79. fclose(service);
  80. }