/* open our outfile */ int open_output_file(void) { int fd = 0; if (!opts.outfile) return STDOUT_FILENO; if (!strncmp(opts.outfile, "-", 1)) return STDOUT_FILENO; umask(0); fd = open(opts.outfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP); if (fd == -1) { struct stat s; if (errno != EEXIST) err_sys_die(EXIT_FAILOPT, "Can't create outputfile: %s", opts.outfile); fd = open(opts.outfile, O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP); if (fd == -1) err_sys_die(EXIT_FAILOPT, "Can't open outputfile: %s", opts.outfile); xfstat(fd, &s, opts.outfile); if (S_ISREG(s.st_mode)) /* symblic link that pointed to regular file */ err_sys_die(EXIT_FAILOPT, "Can't create outputfile: %s", opts.outfile); /* else file is a named pipe, socket, etc. */ } return fd; }
/* Creates our server socket and initialize ** options */ static int init_tipc_trans(void) { int fd; assert(opts.family == AF_TIPC); fd = tipc_socket_connect(); if (fd < 0) err_sys_die(EXIT_FAILNET, "tipc_socket_connect"); return fd; }
int open_input_file(void) { int fd, ret; struct stat stat_buf; if (!strncmp(opts.infile, "-", 1)) { return STDIN_FILENO; } /* We don't want to read from a regular file ** rather we want to execute a program and take ** this output as our data source. */ if (opts.execstring) { pid_t pid; int pipefd[2]; xpipe(pipefd); switch (pid = fork()) { case -1: err_sys_die(EXIT_FAILMISC, "Can't fork"); case 0: close(STDOUT_FILENO); close(STDERR_FILENO); close(pipefd[0]); dup(pipefd[1]); dup(pipefd[1]); system(opts.execstring); exit(0); break; default: close(pipefd[1]); return pipefd[0]; break; } } /* Thats the normal case: we open a regular file and take ** the content as our source. */ ret = stat(opts.infile, &stat_buf); if (ret == -1) err_sys_die(EXIT_FAILMISC, "Can't stat file %s", opts.infile); #if 0 if (!(stat_buf.st_mode & S_IFREG)) { err_sys("Not an regular file %s", opts.infile); exit(EXIT_FAILOPT); } #endif #ifdef O_NOATIME fd = open(opts.infile, O_RDONLY|O_NOATIME); #else fd = open(opts.infile, O_RDONLY); #endif if (fd == -1) err_msg_die(EXIT_FAILMISC, "Can't open input file: %s", opts.infile); return fd; }
/* * performs all socketopts specified, except * for some highly protocol dependant options (e.g. TCP_MD5SIG). */ void set_socketopts(int fd) { int i, ret; const void *optval; socklen_t optlen; /* loop over all selectable socket options */ for (i = 0; socket_options[i].sockopt_name; i++) { if (!socket_options[i].user_issue) continue; /* * this switch statement checks that the particular * socket option matches our selected socket-type */ switch (socket_options[i].level) { case SOL_SOCKET: break; /* works on every socket */ /* fall-through begins here ... */ case IPPROTO_IP: case IPPROTO_IPV6: case IPPROTO_TCP: if (opts.protocol == IPPROTO_TCP) break; case IPPROTO_UDP: if (opts.protocol == IPPROTO_UDP) break; case IPPROTO_UDPLITE: if (opts.protocol == IPPROTO_UDPLITE) break; case IPPROTO_SCTP: if (opts.protocol == IPPROTO_SCTP) break; case SOL_DCCP: if (opts.protocol == IPPROTO_DCCP) break; default: /* and exit if socketoption and sockettype did not match */ err_msg_die(EXIT_FAILMISC, "You selected an socket option which isn't " "compatible with this particular socket option"); } /* ... and do the dirty: set the socket options */ switch (socket_options[i].sockopt_type) { case SVT_BOOL: case SVT_INT: case SVT_TOINT: optlen = sizeof(socket_options[i].value); optval = &socket_options[i].value; break; case SVT_TIMEVAL: optlen = sizeof(socket_options[i].tv); optval = &socket_options[i].tv; break; case SVT_STR: optlen = strlen(socket_options[i].value_ptr) + 1; optval = socket_options[i].value_ptr; break; default: err_msg_die(EXIT_FAILNET, "Unknown sockopt_type %d\n", socket_options[i].sockopt_type); } ret = setsockopt(fd, socket_options[i].level, socket_options[i].option, optval, optlen); if (ret) err_sys_die(EXIT_FAILMISC, "setsockopt option %d (name %s) failed", socket_options[i].sockopt_type, socket_options[i].sockopt_name); } }