File: ec_services.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (141 lines) | stat: -rw-r--r-- 2,898 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
    ettercap -- services list module

    Copyright (C) ALoR & NaGA

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


*/

#include <ec.h>
#include <ec_file.h>
#include <ec_proto.h>

/* globals */

static SLIST_HEAD(, entry) serv_head;

struct entry {
   u_int32 serv;
   u_int16 proto;
   char *name;
   SLIST_ENTRY(entry) next;
};

/* protos */

static void discard_servdb(void);
int services_init(void);
char * service_search(u_int32 serv, u_int8 proto);

/*****************************************/


static void discard_servdb(void)
{
   struct entry *l;

   while (SLIST_FIRST(&serv_head) != NULL) {
      l = SLIST_FIRST(&serv_head);
      SLIST_REMOVE_HEAD(&serv_head, next);
      SAFE_FREE(l->name);
      SAFE_FREE(l);
   }

   DEBUG_MSG("ATEXIT: discard_servdb");
   
   return;
}


int services_init(void)
{
   struct entry *s;
   FILE *f;

   char line[128];
   char name[32];
   char type[8];
   u_int serv;
   u_int8 proto;
   int i = 0;

   /* errors are handled by the function */
   f = open_data("share", SERVICES_NAMES, FOPEN_READ_TEXT);
   ON_ERROR(f, NULL, "Cannot open %s", SERVICES_NAMES);

   while (fgets(line, 80, f) != 0) {

      if (sscanf(line, "%31s%u/%7s", name, &serv, type) != 3)
         continue;

      /* only tcp and udp services */
      if (!strcmp(type, "tcp"))
         proto = NL_TYPE_TCP;
      else if (!strcmp(type, "udp"))
         proto = NL_TYPE_UDP;
      else
         continue;
      
      /* skip comments */
      if (strstr(name, "#"))
         continue;

      SAFE_CALLOC(s, 1, sizeof(struct entry));
      
      s->name = strdup(name);
      s->serv = htons(serv);
      s->proto = proto;
      
      SLIST_INSERT_HEAD(&serv_head, s, next);

      i++;

   }

   DEBUG_MSG("serv_init -- %d services loaded", i);
   USER_MSG("%4d known services\n", i);
   
   fclose(f);

   atexit(discard_servdb);

   return i;
}

/*
 * search for a service and 
 * return its name
 */

char * service_search(u_int32 serv, u_int8 proto)
{
   struct entry *s;

   SLIST_FOREACH(s, &serv_head, next) {
      if (s->serv == serv && s->proto == proto)
         return (s->name);
   }

   /* empty name for unknown services */
   return "";
}


/* EOF */

// vim:ts=3:expandtab