services.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* $Id: services.c 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/types.h>
  32. #include <regex.h>
  33. #include "config.h"
  34. #include "tcpreplay.h"
  35. #include "err.h"
  36. #include "services.h"
  37. extern char tcpservices[NUM_PORTS], udpservices[NUM_PORTS];
  38. /*
  39. * parses /etc/services so we know which ports are service ports
  40. */
  41. void
  42. parse_services(char *file)
  43. {
  44. FILE *service = NULL;
  45. char service_line[MAXLINE], port[10], proto[10];
  46. regex_t preg;
  47. int portc;
  48. size_t nmatch = 3;
  49. regmatch_t pmatch[3];
  50. char regex[] = "([0-9]+)/(tcp|udp)"; /* matches the port as pmatch[1], service pmatch[2] */
  51. dbg(1, "Parsing %s", file);
  52. memset(service_line, '\0', MAXLINE);
  53. /* mark all ports not a service */
  54. memset(tcpservices, '\0', NUM_PORTS);
  55. memset(udpservices, '\0', NUM_PORTS);
  56. if ((service = fopen(file, "r")) == NULL) {
  57. errx(1, "Unable to open service file: %s\n%s", file, strerror(errno));
  58. }
  59. /* compile our regexes */
  60. if ((regcomp(&preg, regex, REG_ICASE|REG_EXTENDED)) != 0) {
  61. errx(1, "Unable to compile regex: %s", regex);
  62. }
  63. /* parse the entire file */
  64. while ((fgets(service_line, MAXLINE, service)) != NULL) {
  65. /* zero out our vars */
  66. memset(port, '\0', 10);
  67. memset(proto, '\0', 10);
  68. portc = 0;
  69. dbg(4, "Procesing: %s", service_line);
  70. /* look for format of 1234/tcp */
  71. if ((regexec(&preg, service_line, nmatch, pmatch, 0)) == 0) { /* matches */
  72. if (nmatch < 2) {
  73. errx(1, "WTF? I matched the line, but I don't know where!");
  74. }
  75. /* strip out the port & proto from the line */
  76. strncpy(port, &service_line[pmatch[1].rm_so], (pmatch[1].rm_eo - pmatch[1].rm_so));
  77. strncpy(proto, &service_line[pmatch[2].rm_so], (pmatch[2].rm_eo - pmatch[2].rm_so));
  78. /* convert port[] into an integer */
  79. portc = atoi(port);
  80. /* update appropriate service array with the server port */
  81. if (strcmp(proto, "tcp") == 0) {
  82. dbg(3, "Setting TCP/%d as a server port", portc);
  83. tcpservices[portc] = 1; /* mark it as a service port */
  84. } else if (strcmp(proto, "udp") == 0) {
  85. dbg(3, "Setting UDP/%d as a server port", portc);
  86. udpservices[portc] = 1;
  87. } else {
  88. warnx("Skipping unknown protocol service %s/%d", proto, portc);
  89. }
  90. }
  91. }
  92. }