1 /*
2  * ncidd.h - This file is part of ncidd.
3  *
4  * Copyright (c) 2005-2019
5  * by John L. Chmielewski <jlc@users.sourceforge.net>
6  *
7  * ncidd is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * ncidd is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with ncidd.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _NCIDD_H
22 #define _NCIDD_H
23 
24 #include "nciddalias.h"
25 #include "nciddhangup.h"
26 #include "version.h"
27 #include "libncid.h"
28 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <getopt.h>
32 #include <fcntl.h>
33 #include <termios.h>
34 #include <string.h>
35 #include <sys/utsname.h>
36 
37 #include <sys/socket.h>
38 #include <netdb.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <netinet/in.h> /* needed for FreeBSD */
42 #include <arpa/inet.h>
43 #include <signal.h>
44 
45 #ifndef CONFIG
46 #define CONFIG     "/etc/ncid/ncidd.conf"
47 #endif
48 
49 #ifndef HOST_NAME_MAX
50 #define HOST_NAME_MAX 64 /* HOST_NAME_MAX is POSIX.1, Linux defines it as 64 */
51 #endif
52 
53 #if (!defined(O_SYNC))
54 # define O_SYNC 0
55 #endif
56 
57 #define UNUSED(var) (void) var
58 
59 #define SHOWVER     "%s [%s:%d] %s\n%s\n"
60 #define DESC        "%s - Network CallerID Server\n"
61 #define NOOPT       "%s: not a option: %s\n"
62 #define USAGE       "\
63 Usage:   %s [options]\n\
64 Options: [-A <file>       | --alias <file>]\n\
65          [-a <file>       | --announce <file>]\n\
66          [-B <file>       | --blacklist <file>]\n\
67          [-C <file>       | --config <file>]\n\
68          [-c <file>       | --cidlog <file>]\n\
69          [-D              | --debug]\n\
70          [-d <file>       | --datalog <file>]\n\
71          [-e <identifier> | --lineid <identifier>]\n\
72          [-f <command>    | --audiofmt <command>]\n\
73          [-g <0|1>        | --gencid <0|1>]\n\
74          [-H <0|1|2|3>    | --hangup <0|1|2|3>]\n\
75          [-h              | --help]\n\
76          [-I <string>     | --initstr <string>]\n\
77          [-i <string>     | --initcid <string>]\n\
78          [-j <0|1|2|3>    | --cidinput <0|1|2|3>]\n\
79          [-L <file>       | --logfile <file>]\n\
80          [-l <file>       | --lockfile <file>]\n\
81          [-M <MaxBytes>   | --cidlogmax <MaxBytes>]\n\
82          [-m <0|1|2|3>    | --hupmode <0|1|2|3>]\n\
83          [-P <file>       | --pidfile <file>]\n\
84          [-p <portnumber> | --port <portnumber>]\n\
85          [-Q <dotted-ip>  | --ifaddr <dotted-ip>]\n\
86          [-r <0|1|2>      | --regex <0|1|2>]\n\
87          [-S <ttyspeed>   | --ttyspeed <ttyspeed>]\n\
88          [-s <datatype>   | --send <datatype>]\n\
89          [-T <0|1>        | --ttyclocal <0|1>]\n\
90          [-t <ttyport>    | --ttyport <ttyport>]\n\
91          [-V              | --version]\n\
92          [-v <1-9>        | --verbose <1-9>]\n\
93          [-W <file>       | --whitelist <file>]\n\
94          [--osx-launchd]\n\
95 "
96 
97 #ifndef TTYPORT
98 #define TTYPORT     "/dev/ttyACM0"
99 #endif
100 #ifndef CIDLOG
101 #define CIDLOG      "/var/log/cidcall.log"
102 #endif
103 #ifndef DATALOG
104 #define DATALOG     "/var/log/ciddata.log"
105 #endif
106 #ifndef LOGFILE
107 #define LOGFILE     "/var/log/ncidd.log"
108 #endif
109 #ifndef PIDFILE
110 #define PIDFILE     "/var/run/ncidd.pid"
111 #endif
112 #ifndef LOCKFILE
113 #define LOCKFILE    "/var/lock/LCK.."
114 #endif
115 #ifndef NCIDUPDATE
116 #define NCIDUPDATE  "/usr/local/bin/cidupdate"
117 #endif
118 #ifndef NCIDUTIL
119 #define NCIDUTIL    "/usr/local/bin/ncidutil"
120 #endif
121 #ifndef HUPEXTENSION
122 #define HUPEXTENSION   "hangup-nohangup"
123 #endif
124 #ifndef EXTDIR
125 #define EXTDIR      "/usr/local/share/ncid/extensions"
126 #endif
127 #ifndef RECORDING
128 #define RECORDING   "DisconnectedNotInService.rmd"
129 #endif
130 #ifndef ANNDIR
131 #define ANNDIR      "/usr/local/share/ncid/recordings"
132 #endif
133 
134 #define STDOUT      1
135 #define CHARWAIT    2       /* deciseconds */
136 #define READWAIT    100     /* milliseconds */
137 #define READTRY     60      /* number of times to EEADWAIT for a character 6 seconds */
138 #define TTYSPEED    B115200
139 #define RAW         1
140 #define NOTRAW      0
141 
142 #define SEND_TIMEOUT 15     /* seconds */
143 
144 /* server messages */
145 #define ANNOUNCE    "200 Server:"
146 #define APIANNOUNCE "210 "
147 #define LOGEND      "250 End of call log"
148 #define NOLOGSENT   "251 Call log not sent"
149 #define EMPTYLOG    "252 Call log empty"
150 #define NOLOG       "253 No Call log"
151 #define LOGSTART    "254 Start of call log"
152 #define ENDSTARTUP  "300 End of connection startup"
153 #define BEGIN_DATA  "400 Start of data requiring OK"
154 #define BEGIN_DATA1 "401 Start of data requiring ACCEPT or REJECT"
155 #define BEGIN_DATA2 "402 Start of data showing status of handled request"
156 #define BEGIN_DATA3 "403 Start of data defining permitted requests"
157 #define END_DATA    "410 End of data"
158 #define END_RESP    "411 End of response"
159 
160 #define NOCHANGES   "no changes"
161 #define DENIED      "denied"
162 #define DOUPDATE    NCIDUPDATE " -C %s -a %s -c %s -W %s -B %s "
163 #define DOUTIL      NCIDUTIL " %s %s \"%s\" %s %s 2>&1"
164 #define DOEXTEND    "%s \"*DATE*%s*TIME*%s*LINE*%s*NMBR*%s*NAME*%s*MODE*%d*\" 2>&1"
165 #define HUPNAME      "hangup"
166 #define RECFILE      "Recording:"
167 #define HUPREASON    "HangupReason:"
168 
169 /* server warning messages */
170 #define LOG2BIG      "MSG: Call Log too big: (%lu > %lu) bytes %s%s"
171 #define TOOMANY      "MSG: Too many clients connected (%d) %s%s"
172 #define SYSMSG       "MSG: %s ***DATE*%s*TIME*%s*NAME*%s*NMBR*%s*LINE*%s*MTYPE*%s*"
173 
174 #define INITSTR       "AT Z S0=0 E1 V1 Q0"
175 #define INITCID1      "AT+VCID=1"
176 #define INITCID2      "AT#CID=1"
177 #define QUERYATI3     "ATI3"
178 #define QUERYATGCI    "AT+GCI?"
179 #define QUERYATFCLASS "AT+FCLASS=?"
180 #define QUERYATVSM    "AT+VSM=?"
181 #define QUERYATFMI    "AT+FMI"
182 #define QUERYV        "AT&V"
183 
184 #define PORT         3333
185 #define MAXCLIENTS   50      /* maximun number of clients that can connect */
186 #define MAXCONNECT   MAXCLIENTS + 2 /* clients + mainsocket + ttyfd */
187 #define MAXIPBUF     500     /* max number of characters in address or name */
188 #define POLL_TIMEOUT 200     /* poll() timeout in milliseconds */
189 #define RINGWAIT     6       /* number of seconds to wait for another RING */
190 #define GCISIZE      50      /* array size used for countryGCI[] */
191 #define MODEMWAIT    45      /* number of seconds to wait before trying init again */
192 
193 #define CRLF         "\r\n"
194 #define NL           "\n"
195 #define CR           "\r"
196 #define PRETTYINDENT "       "
197 
198 #define NONAME       "NO NAME"
199 #define NONMBR       "NO-NUMBER"
200 #define NOLINE       "NO-LINE"
201 #define NOTYPE       "-"
202 #define NOMESG       "NONE"
203 #define NOCID        "No Caller ID"
204 
205 #define LOGMAX       110000
206 #define LOGMAXNUM    100000000
207 
208 #define BLMSG       "Calls in the blacklist file will be terminated"
209 #define WLMSG       "Calls in the whitelist file will not be terminated"
210 #define IGNORE1     "Leading 1 ignored in call & alias/blacklist/whitelist"
211 #define NOIGNORE1   "Leading 1 in call & alias/blacklist/whitelist not ignored"
212 #define RELOADED    "Alias, blacklist and whitelist files have been read"
213 
214 /* Call and message line labels sent to clients */
215 /* sorted alphabetically here for readability */
216 
217 #define BLKLINE     "BLK: "
218 #define CIDLINE     "CID: "
219 #define ENDLINE     "END: "
220 #define HUPLINE     "HUP: "
221 #define MSGLINE     "MSG: "
222 #define MWILINE     "MWI: "
223 #define NOTLINE     "NOT: "
224 #define RLYLINE     "RLY: "
225 #define OUTLINE     "OUT: "
226 #define PIDLINE     "PID: "
227 #define PUTLINE     "PUT: "
228 #define RIDLINE     "RID: "
229 #define WIDLINE     "WID: "
230 
231 /* Other line labels sent to clients */
232 /* sorted alphabetically here for readability */
233 
234 #define ACKLINE    "ACK: "
235 #define INFOLINE   "INFO: "
236 #define LOGLINE    "LOG: "
237 #define OPTLINE    "OPT: "
238 #define REQLINE    "REQ: "
239 #define RESPLINE   "RESP: "
240 #define WRKLINE    "WRK: "
241 
242 #define MESSAGE    "%s ***DATE*%s*TIME*%s*NAME*%s*NMBR*%s*LINE*%s*MTYPE*%s*"
243 
244 #define RELOAD     "REQ: RELOAD"  /* reload alias, blacklist, whitelist files */
245 #define UPDATE     "REQ: UPDATE"  /* update current CID call log file */
246 #define UPDATES    "REQ: UPDATES" /* update all of the CID call log files */
247 #define REREAD     "REQ: REREAD"  /* reread current CID call log file */
248 #define REQ_INFO   "REQ: INFO"
249 #define ACPT_LOG   "ACCEPT LOG"
250 #define ACPT_LOGS  "ACCEPT LOGS"
251 #define RJCT_LOG   "REJECT LOG"
252 #define RJCT_LOGS  "REJECT LOGS"
253 #define BLK_LST    "black"
254 #define ALIAS_LST  "alias"
255 #define WHT_LST    "white"
256 #define ACK        "ACK"
257 #define YO         "YO"
258 #define REQ_ACK    "REQ: ACK"
259 #define REQ_YO     "REQ: YO"
260 #define HELLO      "HELLO: "
261 #define IDENT      "HELLO: IDENT: "
262 #define COMMAND    "HELLO: CMD: "
263 #define GWOUT      "[OUT]"
264 
265 #define GOODBYE        "GOODBYE"
266 #define CLIENT_GOODBYE "Client %d pos %d disconnected - received GOODBYE %s\n"
267 
268 #define DIAL           "REQ: DIAL"    /* dial caller back */
269 #define DIAL_WORKING   "RESP: Working ..."
270 #define DIALOK         "RESP: Pickup phone within %d seconds"
271 #define DIAL_TIMEOUT   5
272 #define DIALED_NMBR   "RESP: Dialed %s on line \"%s\""
273 #define DIAL_LINE_ERR "RPLY: dial - format error: %s"
274 #define DIAL_FAIL     "RPLY: dial - dial failed, modem returned %s"
275 #define DIAL_HANGUP   "RPLY: dial - hungup %s on line \"%s\""
276 
277 #define LINETYPE   25
278 #define ONELINE    "POTS"
279 
280 /* Call types received only from gateways (list 1 of 2) */
281 #define IN         30
282 #define CID        30
283 #define OUT        31
284 #define HUP        32
285 #define BLK        33
286 #define PID        34
287 #define PUT        35
288 #define WID        36
289 #define RID        37
290 #define MWI        38
291 
292 /* Call types received only from gateways (list 2 of 2) */
293 /* kept in same sequence as (list 1 of 2) for consistency */
294 #define CALLIN     "CALLIN"
295 #define CALLCID    "CALLCID"
296 #define CALLOUT    "CALLOUT"
297 #define CALLHUP    "CALLHUP"
298 #define CALLBLK    "CALLBLK"
299 #define CALLPID    "CALLPID"
300 #define CALLPUT    "CALLPUT"
301 #define CALLWID    "CALLWID"
302 #define CALLRID    "CALLRID"
303 #define CALLMWI    "CALLMWI"
304 
305 #define DATE       "*DATE*"
306 #define TIME       "*TIME*"
307 #define NMBR       "*NMBR*"
308 #define MESG       "*MESG*"
309 #define NAME       "*NAME*"
310 #define LINE       "*LINE*"
311 #define HTYPE      "*HTYPE*"
312 #define SCALL      "*SCALL*"
313 #define ECALL      "*ECALL*"
314 #define CTYPE      "*CTYPE*"
315 #define STAR       "*"
316 
317 #define CIDINFO    "CIDINFO: "
318 #define RING       "*RING*"
319 
320 #define CALL       "CALL: "
321 #define CALLINFO   "CALLINFO: "
322 #define CANCEL     "CANCEL"
323 #define BYE        "BYE"
324 #define BUSY       "BUSY"
325 
326 #define OUT_OF_AREA "OUT-OF-AREA"
327 #define ANONYMOUS   "ANONYMOUS"
328 #define PRIVATE     "PRIVATE"
329 
330 /*
331  * Start of XDMF message definitions
332  *
333  * An XDMF message starts with one of the following bytes:
334  *     SDMF_MESG_TYPE
335  *     MDMF_MESG_TYPE
336  *     MDMF_MSG_WAITING
337  *
338  * MDMF_MSG_WAITING is seldom used, it indicates voicemail
339  * https://en.wikipedia.org/wiki/Message-waiting_indicator
340  */
341 
342 #define SDMF_MESG_TYPE    0x04
343 #define MDMF_MESG_TYPE    0x80
344 #define MDMF_MSG_WAITING  0x82
345 
346 #define MDMF_VISUAL_IND   0x0B
347 
348 #define MDMF_DATETIME     0x01
349 #define MDMF_CALLER_NMBR  0x02
350 #define MDMF_CALLER_NAME  0x07
351 
352 #define MDMF_WHY_NO_NMBR  0x04
353 #define MDMF_WHY_NO_NAME  0x08
354 
355 /*
356  * Two reasons defined for (SDMF, MDMF) WHY_NO_NUMBER
357  * and WHY_NO_NAME (MDMF)
358  * WITHHELD    = 0x50 = 'P'RIVATE
359  * UNAVAILABLE = 0x4F = 'O'UT OF AREA
360  */
361 #define LETTER_P          0x50
362 #define LETTER_O          0x4F
363 
364 /*
365  * CALL_TYPE_UK is only sent for ETSI FSK and BT FSK
366  * In general it can be ignored.
367  */
368 #define CALL_TYPE_UK      0x11
369 /*
370  * There are three call subtypes defined:
371  */
372 #define VOICE_UK          0x01
373 #define RINGBACK_UK       0x02
374 #define MSG_WAITING_UK    0x81
375 
376 #define MDMF_NMSS_UK      0x13  /* Network Message System Status */
377 
378 /* End of XDMF message definitions */
379 
380 
381 #define CIDDATE     0x01
382 #define CIDTIME     0x02
383 #define CIDNMBR     0x04
384 #define CIDNAME     0x08
385 #define CIDMESG     0x10
386 #define IGNHIGH     0x0F
387 #define CIDALL3     (CIDDATE | CIDTIME | CIDNMBR)
388 #define CIDALT3     (CIDDATE | CIDTIME | CIDNAME)
389 #define CIDALL4     (CIDDATE | CIDTIME | CIDNMBR | CIDNAME)
390 #define CIDNODT     (CIDNMBR | CIDNAME)
391 
392 extern const char * ttyport;
393 extern const char * TTYspeed;
394 extern const char * initstr;
395 extern const char * initcid;
396 extern const char * hupname;
397 extern const char * huprmd;
398 extern const char * cidlog;
399 extern const char * datalog;
400 extern const char * initstr;
401 extern const char * initcid;
402 extern const char * logfile;
403 extern const char * pidfile;
404 extern const char * lockfile; /* the modem lock file name */
405 extern const char * announce;
406 extern int setcid, port, clocal, ttyspeed, ttyfd, hangup, hupmode;
407 extern int sendlog, sendinfo, ignore1, cidnoname, cidinput;
408 extern int nomodem, noserial, gencid, verbose, dialdelay;
409 extern long unsigned int cidlogmax;
410 extern struct termios rtty, ntty;
411 
412 void errorExit(int error, const char * msg, const char * arg);
413 void sysMesg(const char * message, const char * sysline);
414 void reservePoll(int pos);
415 void restorePoll(int pos, int fd);
416 int CheckForLockfile(void);
417 int makeLockfile(void);
418 int removeLockfile(void);
419 int setModem(void);
420 int initModem(const char * ptr, int maxwait);
421 int setTTY(void);
422 
423 #endif /* ncidd.h */
424