8 #include <sys/socket.h>
9 #include <netinet/in.h>
12 #include <sys/select.h>
16 #define MODEM_DEV "/dev/modem"
17 #define PIDFILE "/var/run/cidd.pid"
22 unsigned char ibuf[1024];
23 unsigned char tmpbuf[1024];
27 void write2(int fd, unsigned char *buf, int len)
33 ret = write(fd, buf, len);
44 void sendcmd(unsigned char *cmd)
48 lbuf = strcpy(malloc(strlen(cmd) + 3), cmd);
50 write2(mdm, lbuf, strlen(cmd) + 2);
54 unsigned char *getline(int block)
59 static unsigned char retbuf[1024];
64 while((p = memchr(ibuf, '\r', ibuflen)) != NULL)
66 memcpy(retbuf, ibuf, len = (p - ibuf));
68 memmove(ibuf, p + 1, ibuflen -= (len + 1));
69 for(p = retbuf; *p; p++)
72 memmove(p, p + 1, len--);
74 if(len && (memcmp(retbuf, "AT", 2)))
82 if(poll(&pf, 1, 0) == 0)
85 ret = read(mdm, ibuf + ibuflen, 1024 - ibuflen);
95 void broadcast(unsigned char *buf, ...)
101 vsprintf(tmpbuf, buf, args);
103 strcat(tmpbuf, "\n");
104 for(i = 0; i < FD_SETSIZE; i++)
106 if(FD_ISSET(i, &cfds) && (i != mdm) && (i != sock))
107 write2(i, tmpbuf, strlen(tmpbuf));
114 if(strcmp(getline(1), "OK"))
117 syslog(LOG_CRIT, "Could not reset modem");
119 fprintf(stderr, "Could not reset modem\n");
123 if(strcmp(getline(1), "OK"))
126 syslog(LOG_CRIT, "Could not activate called ID\n");
128 fprintf(stderr, "Could not activate caller ID\n");
133 void sighandler(int sig)
141 int main(int argc, unsigned char **argv)
147 struct sockaddr_in name;
153 if((mdm = open(MODEM_DEV, O_RDWR)) < 0)
158 if(tcgetattr(mdm, &tio) < 0)
164 cfsetispeed(&tio, B0);
165 cfsetospeed(&tio, B38400);
166 if(tcsetattr(mdm, TCSANOW, &tio) < 0)
171 if((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
176 name.sin_family = AF_INET;
177 name.sin_port = htons(PORT);
178 name.sin_addr.s_addr = 0;
179 if(bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0)
184 if(listen(sock, 16) < 0)
190 signal(SIGHUP, sighandler);
191 if((pidst = fopen(PIDFILE, "w")) == NULL)
193 perror("open " PIDFILE);
197 fprintf(pidst, "%i\n", getpid());
199 openlog("cidd", LOG_PID, LOG_DAEMON);
207 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
208 if((ret < 0) && (errno != EINTR))
210 syslog(LOG_CRIT, "select return %m");
215 for(i = 0; i < FD_SETSIZE; i++)
217 if(FD_ISSET(i, &rfds))
221 while((p = getline(0)) != NULL)
223 if(!strcmp(p, "RING"))
225 if(!memcmp(p, "NMBR = ", 7))
231 } else if(i == sock) {
232 if((nsock = accept(sock, NULL, 0)) >= 0)
233 FD_SET(nsock, &cfds);
235 if(read(i, tmpbuf, 1024) == 0)