Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <signal.h>
- uint32_t one = 1;
- volatile int flag1 = 0;
- volatile int flag2 = 0;
- volatile int counter = 0;
- uint32_t mask_host = ((uint32_t) 1 << 16) - 1;
- uint32_t mask_net = ((uint32_t) 1 << 30) - ((uint32_t) 1 << 16);
- int is_ipv4B(uint32_t addr) {
- if ((addr & (one << 31)) && !(addr & (one << 30))) {
- return 1;
- }
- return 0;
- }
- uint32_t get_elem(uint32_t addr) {
- uint32_t res;
- if (flag1) {
- res = addr & mask_host;
- } else {
- res = (addr & mask_net) >> 16;
- }
- return res;
- }
- void handler(int signo) {
- if (signo == SIGUSR1) {
- flag1 = !flag1;
- ++counter;
- if (counter == 5) {
- _exit(0);
- }
- } else if (signo == SIGUSR2) {
- flag2 = 1;
- }
- }
- int main() {
- struct sigaction sa = { .sa_handler = handler, .sa_flags = SA_RESTART };
- sigaction(SIGUSR1, &sa, NULL);
- sigaction(SIGUSR2, &sa, NULL);
- sigset_t mask1, mask2;
- sigemptyset(&mask1);
- sigaddset(&mask1, SIGUSR1);
- sigaddset(&mask1, SIGUSR2);
- sigprocmask(SIG_BLOCK, &mask1, &mask2);
- printf("%d\n", getpid());
- fflush(stdout);
- while (1) {
- while (!flag2) {
- sigsuspend(&mask2);
- }
- if (flag2) {
- uint32_t num;
- scanf("%o", &num);
- if (is_ipv4B(num)) {
- printf("%u\n", get_elem(num));
- } else {
- printf("0\n");
- }
- fflush(stdout);
- }
- flag2 = 0;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement