| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /* vcc/main.c
- *
- * This file is part of vcc.
- *
- * vcc 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 3 of the License, or
- * (at your option) any later version.
- *
- * vcc 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 vcc. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <poll.h>
- #include <signal.h>
- #include <vcc/vcc.h>
- #include <vcc/version.h>
- int fd;
- static unsigned char empty_package[REQ_SIZE];
- static int usage(void) {
- fprintf(stderr, "Usage: vcc [server]\n");
- fprintf(stderr, "As default, server is 124.223.105.230. \n");
- return 1;
- }
- static int init_socket(const char *server_addr) {
- struct sockaddr_in addr, server;
- if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
- perror("socket()");
- exit(1);
- }
- memset(&addr, 0, sizeof(struct sockaddr_in));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addr.sin_port = htons(0);
- if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
- perror("bind()");
- exit(1);
- }
- memset(&server, 0, sizeof(struct sockaddr_in));
- server.sin_family = AF_INET;
- server.sin_port = htons(VCC_PORT);
- if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
- fprintf(stderr, "invalid ip address. "
- "leave blank if you want to connect the default server");
- exit(1);
- }
- if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
- perror("connect()");
- exit(1);
- }
- return fd;
- }
- static int send_msg(const char *msg) {
- char *buf;
-
- if (unlikely(!(buf = malloc(REQ_SIZE)))) {
- fprintf(stderr, "*** malloc() failed\n");
- exit(1);
- }
- strcpy(buf, msg);
- write(fd, buf, REQ_SIZE);
- free(buf);
- return 0;
- }
- static int read_msg(char *buf) {
- return read(fd, buf, REQ_SIZE);
- }
- void alarm_handler(int nr) {
- alarm(1);
- memset(empty_package, 0xff, REQ_SIZE);
- write(fd, empty_package, REQ_SIZE);
- return;
- }
- static int setup_alarm(void) {
- struct sigaction act;
- act.sa_handler = alarm_handler;
- act.sa_flags = SA_RESTART;
- sigaction(SIGALRM, &act, NULL);
- alarm(1);
- return 0;
- }
- int main(int argc, const char **argv) {
- char buf[REQ_SIZE];
- int pid;
- if (unlikely(argc != 1 && argc != 2))
- return usage();
- printf(VCCD_VERSION "\n");
- init_socket(argv[1] ? argv[1] : "124.223.105.230");
- printf("Connected to server. \n");
- if (!(pid = fork())) {
- /* receive messages */
- setup_alarm();
- for (;;) {
- memset(buf, 0, REQ_SIZE);
- if (read_msg(buf) == -1) {
- printf("Server closed\n");
- break;
- }
- if (!*buf || *(unsigned char *) buf == 0xffu)
- continue;
- printf("New message: %s\n", buf);
- }
- }
- else
- for (;;) {
- memset(buf, 0, REQ_SIZE);
- if (read(0, buf, REQ_SIZE) <= 0) {
- fprintf(stderr, "--- read() failed. \n");
- continue;
- }
- if (!strcmp(buf, "quit"))
- break;
- send_msg(buf);
- }
- close(fd);
- kill(pid, 9);
- return 0;
- }
|