static void doconnect(void) { int fd = accept(sockfd, 0, 0); if (fd < 0) perror("accept"); else addport(fd); }
/* * passive_pcap4: This is the ipv4 pcap looper. It is like most pcap callbacks * requires: all of the standard pcap_loop data */ static void passive_pcap4(u_char * args, const struct pcap_pkthdr *header, const u_char * packet) { u_int sport; u_int dport; eth_hdr *ethernet; /* The ethernet header */ ip4ip *ip; /* The IP header */ struct protoent *proto; const struct tcphdr4 *tcp; /* TCP Header */ /* Extract ethernet, ip and tcp headers */ ethernet = (eth_hdr *) (packet); /* Pointer to ethernet header */ ip = (ip4ip *) (packet + sizeof(eth_hdr)); tcp = (struct tcphdr4 *)(packet + sizeof(struct ether_header) + sizeof(struct ip)); if (ip->ip_v != 4) return; /* don't try to do ipv6 yet */ dport = ntohs(tcp->th_dport); /* We only look at the dest port */ proto = getprotobynumber(ip->ip_p); /* Fetch the protocol string */ if ((dport <= 1024) || (xflag)) addport(inet_ntoa(ip->ip_dst), dport, proto->p_name); }
void makeports(Ruleset *rules[]) { int i; for(i=0; rules[i]; i++) addport(rules[i]->port); }
Ruleset* readruleset(void) { Ruleset *rs; Rule *r; int eof, inrule, i, ncmd; Again: eof = 0; rs = emalloc(sizeof(Ruleset)); rs->pat = emalloc(sizeof(Rule*)); rs->act = emalloc(sizeof(Rule*)); inrule = 0; ncmd = 0; for(;;){ r = readrule(&eof); if(eof) break; if(r==nil){ if(inrule) break; continue; } inrule = 1; switch(r->obj){ case OArg: case OAttr: case OData: case ODst: case OType: case OWdir: case OSrc: rs->npat++; rs->pat = erealloc(rs->pat, (rs->npat+1)*sizeof(Rule*)); rs->pat[rs->npat-1] = r; rs->pat[rs->npat] = nil; break; case OPlumb: rs->nact++; rs->act = erealloc(rs->act, (rs->nact+1)*sizeof(Rule*)); rs->act[rs->nact-1] = r; rs->act[rs->nact] = nil; if(r->verb == VTo){ if(rs->npat>0 && rs->port != nil) /* npat==0 implies port declaration */ parseerror("too many ports"); if(lookup(r->qarg, badports) >= 0) parseerror("illegal port name %s", r->qarg); if(rs->port) free(rs->port); rs->port = estrdup(r->qarg); }else ncmd++; /* start or client rule */ break; } } if(ncmd > 1){ freeruleset(rs); parseerror("ruleset has more than one client or start action"); } if(rs->npat>0 && rs->nact>0) return rs; if(rs->npat==0 && rs->nact==0){ freeruleset(rs); return nil; } if(rs->nact==0 || rs->port==nil){ freeruleset(rs); parseerror("ruleset must have patterns and actions"); return nil; } /* declare ports */ for(i=0; i<rs->nact; i++) if(rs->act[i]->verb != VTo){ freeruleset(rs); parseerror("ruleset must have actions"); return nil; } for(i=0; i<rs->nact; i++) addport(rs->act[i]->qarg); freeruleset(rs); goto Again; }