void rcv_encoded(uint16_t *ids, uint16_t len, int sd, char *buf, struct sockaddr_in *client) { int i; int dgram_num; uint16_t id; struct ip_header *iph = (struct ip_header*) buf; struct udp_header *udph = (struct udp_header *) (buf + sizeof(struct ip_header)); uint16_t initid = ntohs(len); printf("Message length: %d\n", len); for (i = 0; i < len * 2; i++) { recv_dgram(sd, buf, client); id = ntohs(iph->id); if (ntohs(udph->srcport) != port_from_date() || IP_FLAGS(iph) == IP_DONTFRAG || !in_range(initid, id)) { /* not one of our packets */ #ifdef DEBUG printf("A message we don't care about...\n"); printf(" ntohs(udph->srcport): %d\n", ntohs(udph->srcport)); printf(" port_from_date(): %d\n", port_from_date()); printf(" IP_FLAGS(iph): %d\n", IP_FLAGS(iph)); printf(" IP_DONTFRAG: %d\n", IP_DONTFRAG); printf(" id: %d\n", id); printf(" in_range(initid, id): %d\n", in_range(initid, id)); #endif i--; continue; } /* len is the initial ID */ dgram_num = (id - initid) / 0x10; ids[dgram_num] = id; printf("#%d\tID: %d\n", dgram_num + 1, ntohs(iph->id)); } }
char *IPPacket2Str(IPHeader *packet) { const int BUFFER_LENGTH = 1024; char *str = (char*)malloc(BUFFER_LENGTH); ZeroMemory(str, BUFFER_LENGTH); char *t1 = Int2Bin(packet->iph_tos, 8); char *t2 = Int2Bin(IP_FLAGS(ntohs(packet->iph_flags)), 3); char *src = Nethost2Str(packet->iph_sourceip); char *dest = Nethost2Str(packet->iph_destip); sprintf(str, "ver=%d hlen=%d tos=%s len=%-4d id=%-5d flags=%s offset=%d ttl=%3dms prot=%-2d xsum=%-5X src=%-15s dest=%-15s\n", BYTE_H(packet->iph_ver), BYTE_L(packet->iph_ver)*4, t1, ntohs(packet->iph_len), ntohs(packet->iph_ident), t2, IP_OFFSET(ntohs(packet->iph_offset)), packet->iph_ttl, packet->iph_protocol, ntohs(packet->iph_xsum), src, dest); free(t1); free(t2); free(src); free(dest); return str; }
void server() { struct sockaddr_in server; struct sockaddr_in client; int sd; char buf[RECV_BUFLEN]; uint16_t len; uint16_t *ids; char *decoded; struct ip_header *iph = (struct ip_header*) buf; struct udp_header *udph = (struct udp_header *) (buf + sizeof(struct ip_header)); if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1) { exit(sock_error("socket()", 0)); } memset((char *) &server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(DST_PORT); server.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sd, (struct sockaddr *) &server, sizeof(server)) == -1) { exit(sock_error("bind()", 0)); } while (1) { recv_dgram(sd, buf, &client); if (ntohs(udph->srcport) == port_from_date() && IP_FLAGS(iph) == IP_DONTFRAG) { printf("Covert message arriving...\n"); len = iph->id; ids = malloc(sizeof(uint16_t) * len * 2); decoded = malloc(sizeof(char) * len + 1); rcv_encoded(ids, len, sd, buf, &client); printf("Message received. Decoding...\n\n"); decode(decoded, ids, len); printf("%s\n", decoded); free(ids); free(decoded); } #ifdef DEBUG else { printf("A message we don't care about...\n"); printf(" ntohs(udph->srcport): %d\n", ntohs(udph->srcport)); printf(" port_from_date(): %d\n", port_from_date()); printf(" IP_FLAGS(iph): %d\n", IP_FLAGS(iph)); printf(" IP_DONTFRAG: %d\n", IP_DONTFRAG); } #endif } }