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-2018 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. size_t nmatch = 3;
  37. regmatch_t pmatch[3];
  38. static const char regex[] = "([0-9]+)/(tcp|udp)"; /* matches the port as pmatch[1], service pmatch[2] */
  39. assert(file);
  40. assert(services);
  41. dbgx(1, "Parsing %s", file);
  42. memset(service_line, '\0', MAXLINE);
  43. /* mark all ports not a service */
  44. memset(&services->tcp[0], '\0', sizeof(services->tcp));
  45. memset(&services->udp[0], '\0', sizeof(services->udp));
  46. if ((service = fopen(file, "r")) == NULL) {
  47. errx(-1, "Unable to open service file: %s\n%s", file, strerror(errno));
  48. }
  49. /* compile our regexes */
  50. if ((regcomp(&preg, regex, REG_ICASE|REG_EXTENDED)) != 0) {
  51. errx(-1, "Unable to compile regex: %s", regex);
  52. }
  53. /* parse the entire file */
  54. while ((fgets(service_line, MAXLINE, service)) != NULL) {
  55. /* zero out our vars */
  56. memset(port, '\0', 10);
  57. memset(proto, '\0', 10);
  58. dbgx(4, "Processing: %s", service_line);
  59. /* look for format of 1234/tcp */
  60. if ((regexec(&preg, service_line, nmatch, pmatch, 0)) == 0) { /* matches */
  61. uint16_t portc;
  62. /* strip out the port & proto from the line */
  63. strncpy(port, &service_line[pmatch[1].rm_so], (pmatch[1].rm_eo - pmatch[1].rm_so));
  64. strncpy(proto, &service_line[pmatch[2].rm_so], (pmatch[2].rm_eo - pmatch[2].rm_so));
  65. /* convert port[] into an integer */
  66. portc = (uint16_t)atoi(port);
  67. /* update appropriate service array with the server port */
  68. if (strcmp(proto, "tcp") == 0) {
  69. dbgx(3, "Setting TCP/%d as a server port", portc);
  70. services->tcp[portc] = 1; /* mark it as a service port */
  71. } else if (strcmp(proto, "udp") == 0) {
  72. dbgx(3, "Setting UDP/%d as a server port", portc);
  73. services->udp[portc] = 1;
  74. } else {
  75. warnx("Skipping unknown protocol service %s/%d", proto, portc);
  76. }
  77. }
  78. }
  79. fclose(service);
  80. }