X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=listati%2FTCP_echod_first_init.c;fp=listati%2FTCP_echod_first_init.c;h=02e99adfe78082052ca778dad4c8d291eb54f46e;hp=0000000000000000000000000000000000000000;hb=541dec587ab21a1e01342784a803ab6404b98d1e;hpb=5077cb02e6a65499d7fddcd01d68e8800c73a6fa diff --git a/listati/TCP_echod_first_init.c b/listati/TCP_echod_first_init.c new file mode 100644 index 0000000..02e99ad --- /dev/null +++ b/listati/TCP_echod_first_init.c @@ -0,0 +1,36 @@ +int main(int argc, char *argv[]) +{ + int list_fd, conn_fd; + pid_t pid; + struct sockaddr_in serv_add; + ... + /* create and init socket */ + if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("Socket creation error"); + exit(1); + } + memset((void *)&serv_add, 0, sizeof(serv_add)); /* clear server address */ + serv_add.sin_family = AF_INET; /* address type is INET */ + serv_add.sin_port = htons(7); /* echo port is 7 */ + serv_add.sin_addr.s_addr = htonl(INADDR_ANY); /* connect from anywhere */ + if (bind(list_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) { + perror("bind error"); + exit(1); + } + /* give away privileges and go daemon */ + if (setgid(65534) !=0) { /* first give away group privileges */ + perror("cannot give away group privileges"); + exit(1); + } + if (setuid(65534) !=0) { /* and only after user ... */ + perror("cannot give away user privileges"); + exit(1); + } + if (demonize) { /* go daemon */ + openlog(argv[0], 0, LOG_DAEMON); /* open logging */ + if (daemon(0, 0) != 0) { + perror("cannot start as daemon"); + exit(1); + } + } + ...