ip_t * ip_open(void) { BOOL on; ip_t *ip; if ((ip = calloc(1, sizeof(*ip))) != NULL) { if (WSAStartup(MAKEWORD(2, 2), &ip->wsdata) != 0) { free(ip); return (NULL); } if ((ip->fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == INVALID_SOCKET) return (ip_close(ip)); on = TRUE; if (setsockopt(ip->fd, IPPROTO_IP, IP_HDRINCL, (const char *)&on, sizeof(on)) == SOCKET_ERROR) { SetLastError(ERROR_NETWORK_ACCESS_DENIED); return (ip_close(ip)); } ip->sin.sin_family = AF_INET; ip->sin.sin_port = htons(666); } return (ip); }
static void nfq_daq_shutdown (void* handle) { NfqImpl *impl = (NfqImpl*)handle; impl->state = DAQ_STATE_UNINITIALIZED; if (impl->nf_queue) nfq_destroy_queue(impl->nf_queue); // note that we don't unbind here because // we will unbind other programs too if(impl->nf_handle) nfq_close(impl->nf_handle); if ( impl->link ) eth_close(impl->link); if ( impl->net ) ip_close(impl->net); if ( impl->filter ) free(impl->filter); if ( impl->buf ) free(impl->buf); free(impl); }
static void __producer_stop(void) { if (producer_status == PS_PLAYING || producer_status == PS_PAUSED) { ip_close(ip); __producer_status_update(PS_STOPPED); reset_buffer(); } }
int main(int argc, char **argv) { ip_t *sock; intf_t *intf; struct addr dst; struct ip_hdr *ip; struct udp_hdr *udp; struct intf_entry entry; int len = IP_HDR_LEN + UDP_HDR_LEN; char buf[len]; if (argc < 2 || addr_aton(argv[1], &dst)) { printf("error: please specify a target ip address\n"); return 1; } memset(buf, 0, sizeof(buf)); ip = (struct ip_hdr *) buf; ip->ip_v = 4; ip->ip_hl = 5; ip->ip_tos = 0; ip->ip_off = 0; ip->ip_sum = 0; ip->ip_ttl = IP_TTL_MAX; ip->ip_p = IP_PROTO_UDP; ip->ip_id = htons(0xdead); ip->ip_len = htons(len); udp = (struct udp_hdr *) (buf + IP_HDR_LEN); udp->uh_sum = 0; udp->uh_sport = htons(0); udp->uh_dport = htons(5353); udp->uh_ulen = htons(UDP_HDR_LEN); intf = intf_open(); intf_get_dst(intf, &entry, &dst); intf_close(intf); ip->ip_src = entry.intf_addr.addr_ip; ip->ip_dst = dst.addr_ip; ip_checksum(buf, len); sock = ip_open(); if (!sock) { printf("error: root privileges needed for raw socket\n"); return 1; } ip_send(sock, buf, len); ip_close(sock); return 0; }
static int Active_Close (void) { if ( s_link ) eth_close(s_link); if ( s_ipnet ) ip_close(s_ipnet); s_link = NULL; s_ipnet = NULL; return 0; }
int ip_init(anysin_t *addresses, int addresses_count) { int i; global_ip_sockets = (struct ip_socket_t *) calloc(addresses_count * 2, sizeof(struct ip_socket_t)); if (!global_ip_sockets) goto wrong; global_ip_sockets_count = addresses_count * 2; for (i = 0; i < global_ip_sockets_count; i++) global_ip_sockets[i].fd = -1; for (i = 0; i < addresses_count; i++) { int sid = i * 2; // Do UDP bindings: global_ip_sockets[sid].address = &addresses[i]; global_ip_sockets[sid].protocol = IP_PROTOCOL_UDP; if (!ip_udp_open(&global_ip_sockets[sid].fd, &addresses[i])) { debug_log(DEBUG_FATAL, "ip_init(): unable to open UDP socket (%s)\n", strerror(errno)); goto wrong; } if (!ip_reuse(global_ip_sockets[sid].fd)) debug_log(DEBUG_WARN, "ip_init(): unable to set UDP socket to reuse address (%s)\n", strerror(errno)); if (!ip_bind(global_ip_sockets[sid].fd, &addresses[i])) { debug_log(DEBUG_FATAL, "ip_init(): unable to bind UDP socket (%s)\n", strerror(errno)); goto wrong; } // Do TCP bindings: global_ip_sockets[sid+1].address = &addresses[i]; global_ip_sockets[sid+1].protocol = IP_PROTOCOL_TCP; if (!ip_tcp_open(&global_ip_sockets[sid+1].fd, &addresses[i])) { debug_log(DEBUG_FATAL, "ip_init(): unable to open TCP socket (%s)\n", strerror(errno)); goto wrong; } if (!ip_reuse(global_ip_sockets[sid+1].fd)) debug_log(DEBUG_WARN, "ip_init(): unable to set TCP socket to reuse address (%s)\n", strerror(errno)); if (!ip_bind(global_ip_sockets[sid+1].fd, &addresses[i])) { debug_log(DEBUG_FATAL, "ip_init(): unable to bind TCP socket (%s)\n", strerror(errno)); goto wrong; } if (!ip_tcp_listen(global_ip_sockets[sid+1].fd)) { debug_log(DEBUG_FATAL, "ip_init(): unable to listen on TCP socket (%s)\n", strerror(errno)); } } return 1; wrong: ip_close(); return 0; }
ip_t * ip_open(void) { ip_t *i; int n, len; if ((i = calloc(1, sizeof(*i))) == NULL) return (NULL); if ((i->fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return (ip_close(i)); #ifdef IP_HDRINCL n = 1; if (setsockopt(i->fd, IPPROTO_IP, IP_HDRINCL, &n, sizeof(n)) < 0) return (ip_close(i)); #endif #ifdef SO_SNDBUF len = sizeof(n); if (getsockopt(i->fd, SOL_SOCKET, SO_SNDBUF, &n, &len) < 0) return (ip_close(i)); for (n += 128; n < 1048576; n += 128) { if (setsockopt(i->fd, SOL_SOCKET, SO_SNDBUF, &n, len) < 0) { if (errno == ENOBUFS) break; return (ip_close(i)); } } #endif #ifdef SO_BROADCAST n = 1; if (setsockopt(i->fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) < 0) return (ip_close(i)); #endif return (i); }
static void ipq_daq_shutdown (void* handle) { IpqImpl* impl = (IpqImpl*)handle; if ( impl->ipqh ) ipq_destroy_handle(impl->ipqh); if ( impl->device ) free(impl->device); if ( impl->link ) eth_close(impl->link); if ( impl->net ) ip_close(impl->net); if ( impl->filter ) free(impl->filter); if ( impl->buf ) free(impl->buf); free(impl); }
int main(int argc, char *argv[]) { struct intf_entry ifent; intf_t *intf; int i, tests; char *cmd; if (argc < 3) usage(); for (tests = 0, i = 1; i < argc - 1; i++) { cmd = argv[i]; if (strcmp(cmd, "all") == 0) tests = ~0; else if (strcmp(cmd, "ping") == 0) tests |= TEST_PING; else if (strcmp(cmd, "ip-opt") == 0) tests |= TEST_IP_OPT; else if (strcmp(cmd, "ip-tracert") == 0) tests |= TEST_IP_TRACERT; else if (strcmp(cmd, "frag") == 0) tests |= TEST_FRAG; else if (strcmp(cmd, "frag-new") == 0) tests |= TEST_FRAG_NEW; else if (strcmp(cmd, "frag-old") == 0) tests |= TEST_FRAG_OLD; else if (strcmp(cmd, "frag-timeout") == 0) tests |= TEST_FRAG_TIMEOUT; else usage(); } if (addr_aton(argv[i], &ctx.dst) < 0) err(1, "invalid host %s", argv[i]); if ((intf = intf_open()) == NULL) err(1, "couldn't open interface handle"); ifent.intf_len = sizeof(ifent); if (intf_get_dst(intf, &ifent, &ctx.dst) < 0) err(1, "couldn't find interface for %s", addr_ntoa(&ctx.dst)); memcpy(&ctx.src, &ifent.intf_addr, sizeof(ctx.src)); ctx.src.addr_bits = IP_ADDR_BITS; intf_close(intf); if ((ctx.ip = ip_open()) == NULL) err(1, "couldn't open raw IP interface"); if ((ctx.pcap = pcap_open(ifent.intf_name)) == NULL) err(1, "couldn't open %s for sniffing", ifent.intf_name); if ((ctx.dloff = pcap_dloff(ctx.pcap)) < 0) err(1, "couldn't determine link layer offset"); ctx.rnd = rand_open(); pkt_init(16); TAILQ_INIT(&ctx.pktq); ping = pkt_new(); ip_pack_hdr(ping->pkt_ip, 0, IP_HDR_LEN + 8 + 24, 666, 0, IP_TTL_DEFAULT, IP_PROTO_ICMP, ctx.src.addr_ip, ctx.dst.addr_ip); icmp_pack_hdr_echo(ping->pkt_icmp, ICMP_ECHO, ICMP_CODE_NONE, 666, 1, "AAAAAAAABBBBBBBBCCCCCCCC", 24); ping->pkt_end = ping->pkt_eth_data + IP_HDR_LEN + 8 + 24; pkt_decorate(ping); if ((tests & TEST_PING) != 0) test_ping(); if ((tests & TEST_IP_OPT) != 0) test_ip_opt(); if ((tests & TEST_IP_TRACERT) != 0) test_ip_tracert(); if ((tests & TEST_FRAG) != 0) test_frag(NULL, 0); if ((tests & TEST_FRAG_NEW) != 0) test_frag("new", 0); if ((tests & TEST_FRAG_OLD) != 0) test_frag("old", 0); if ((tests & TEST_FRAG_TIMEOUT) != 0) test_frag(NULL, 1); rand_close(ctx.rnd); pcap_close(ctx.pcap); ip_close(ctx.ip); exit(0); }