Rev 3 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3 | Rev 14 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | // ************************************************** |
1 | // ************************************************** |
2 | // ** OID CSV Lookup Server 1.1 ** |
2 | // ** OID CSV Lookup Server 1.2 ** |
3 | // ** (c) 2016-2019 ViaThinkSoft, Daniel Marschall ** |
3 | // ** (c) 2016-2020 ViaThinkSoft, Daniel Marschall ** |
4 | // ************************************************** |
4 | // ************************************************** |
5 | 5 | ||
6 | // todo: log verbosity + datetime |
6 | // todo: log verbosity + datetime |
7 | // todo: publish at codelib |
7 | // todo: publish at codelib |
8 | // todo: server hat sich einfach so beendet... "read: connection reset by peer" |
8 | // todo: server hat sich einfach so beendet... "read: connection reset by peer" |
9 | // todo: 2019-02-24 service was booted together with the system, and i got "0 OIDs loaded". why??? |
9 | // todo: 2019-02-24 service was booted together with the system, and i got "0 OIDs loaded". why??? |
10 | // todo: create vnag monitor that checks if this service is OK |
10 | // todo: create vnag monitor that checks if this service is OK |
- | 11 | // todo: unexplained crash with version 1.1 : Signal "pipefail" (141) received after someone connected?! |
|
11 | 12 | ||
12 | #include "oid_lookup_srv.h" |
13 | #include "oid_lookup_srv.h" |
13 | 14 | ||
14 | unordered_set<string> lines; |
15 | unordered_set<string> lines; |
15 | 16 | ||
Line 47... | Line 48... | ||
47 | 48 | ||
48 | int read_from_client(int filedes) { |
49 | int read_from_client(int filedes) { |
49 | char buffer[MAXMSG]; |
50 | char buffer[MAXMSG]; |
50 | int nbytes; |
51 | int nbytes; |
51 | 52 | ||
52 | nbytes = read(filedes, buffer, MAXMSG); |
53 | nbytes = read(filedes, buffer, sizeof(buffer)); |
- | 54 | buffer[sizeof(buffer)-1] = 0; // Terminator |
|
- | 55 | ||
53 | if (nbytes < 0) { |
56 | if (nbytes < 0) { |
54 | /* Read error. */ |
57 | /* Read error. */ |
55 | //perror("read"); |
58 | //perror("read"); |
56 | //exit(EXIT_FAILURE); |
59 | //exit(EXIT_FAILURE); |
57 | return -1; |
60 | return -1; |
Line 59... | Line 62... | ||
59 | /* End-of-file. */ |
62 | /* End-of-file. */ |
60 | return -1; |
63 | return -1; |
61 | } else { |
64 | } else { |
62 | /* Data read. */ |
65 | /* Data read. */ |
63 | 66 | ||
64 | for (int i=0; i<MAXMSG; ++i) { |
67 | for (uint i=0; i<sizeof(buffer); ++i) { |
65 | if ((buffer[i] == 13) || (buffer[i] == 10)) buffer[i] = 0; |
68 | if ((buffer[i] == 13) || (buffer[i] == 10)) { |
- | 69 | buffer[i] = 0; // Terminator |
|
- | 70 | break; |
|
- | 71 | } |
|
66 | } |
72 | } |
67 | 73 | ||
68 | if (strcmp(buffer, "bye") == 0) { |
74 | if (strcmp(buffer, "bye") == 0) { |
69 | fprintf(stdout, "%s:%d[%d] Client said good bye.\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes); |
75 | fprintf(stdout, "%s:%d[%d] Client said good bye.\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes); |
70 | return -1; |
76 | return -1; |
71 | } else { |
77 | } else { |
72 | cons[filedes].queries++; |
78 | cons[filedes].queries++; |
73 | 79 | ||
- | 80 | for (uint i=0; i<sizeof(buffer); ++i) { |
|
- | 81 | if (buffer[i] == 0) break; |
|
- | 82 | if (!((buffer[i] >= '0') && (buffer[i] <= '9')) && !(buffer[i] == '.')) { |
|
- | 83 | fprintf(stdout, "%s:%d[%d] Client sent an invalid request.\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes); |
|
- | 84 | return -1; |
|
- | 85 | } |
|
- | 86 | } |
|
- | 87 | ||
74 | // fprintf(stdout, "%s:%d[%d] Query #%d: %s\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes, cons[filedes].queries, buffer); |
88 | // fprintf(stdout, "%s:%d[%d] Query #%d: %s\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes, cons[filedes].queries, buffer); |
75 | 89 | ||
76 | if (stringAvailable(buffer)) { |
90 | if (stringAvailable(buffer)) { |
77 | write(filedes, "1\n", 2); |
91 | write(filedes, "1\n", 2); |
78 | } else { |
92 | } else { |
Line 137... | Line 151... | ||
137 | int main(void) { |
151 | int main(void) { |
138 | // extern int make_socket(uint16_t port); |
152 | // extern int make_socket(uint16_t port); |
139 | int sock; |
153 | int sock; |
140 | fd_set active_fd_set, read_fd_set; |
154 | fd_set active_fd_set, read_fd_set; |
141 | 155 | ||
142 | fprintf(stdout, "OID CSV Lookup Server 1.0 (c)2016-2019 ViaThinkSoft\n"); |
156 | fprintf(stdout, "OID CSV Lookup Server 1.2 (c)2016-2020 ViaThinkSoft\n"); |
143 | fprintf(stdout, "Listening at port: %d\n", PORT); |
157 | fprintf(stdout, "Listening at port: %d\n", PORT); |
144 | fprintf(stdout, "Max connections: %d\n", FD_SETSIZE); |
158 | fprintf(stdout, "Max connections: %d\n", FD_SETSIZE); |
145 | 159 | ||
146 | initConsArray(); |
160 | initConsArray(); |
147 | 161 | ||
Line 208... | Line 222... | ||
208 | FD_CLR(new_fd, &active_fd_set); |
222 | FD_CLR(new_fd, &active_fd_set); |
209 | } else { |
223 | } else { |
210 | fprintf(stdout, "%s:%d[%d] Connected\n", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port), new_fd); |
224 | fprintf(stdout, "%s:%d[%d] Connected\n", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port), new_fd); |
211 | } |
225 | } |
212 | 226 | ||
- | 227 | if (new_fd >= FD_SETSIZE) { |
|
- | 228 | fprintf(stderr, "%s:%d[%d] new_fd reached cons[FD_SETSIZE] limit\n", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port), new_fd); |
|
- | 229 | close(new_fd); |
|
- | 230 | FD_CLR(new_fd, &active_fd_set); |
|
- | 231 | } |
|
- | 232 | ||
213 | cons[new_fd].last_activity = time(NULL); |
233 | cons[new_fd].last_activity = time(NULL); |
214 | cons[new_fd].queries = 0; |
234 | cons[new_fd].queries = 0; |
215 | } else { |
235 | } else { |
216 | /* Data arriving on an already-connected socket. */ |
236 | /* Data arriving on an already-connected socket. */ |
217 | cons[i].last_activity = time(NULL); |
237 | cons[i].last_activity = time(NULL); |