static void TftpSend (void) { volatile unsigned char * pkt; volatile unsigned char * xp; int len = 0; /* * We will always be sending some sort of packet, so * cobble together the packet headers now. */ pkt = NetTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE; switch (TftpState) { case STATE_RRQ: xp = pkt; *((unsigned short *) pkt) = TFTP_RRQ; pkt += sizeof(unsigned short); strcpy ((char *) pkt, tftp_filename); pkt += strlen(tftp_filename) + 1; strcpy ((char *) pkt, "octet"); pkt += 5 /*strlen("octet")*/ + 1; len = pkt - xp; break; case STATE_DATA: xp = pkt; *((unsigned short *) pkt) = TFTP_ACK; pkt += sizeof(unsigned short); *((unsigned short *) pkt) = TftpBlock; pkt += sizeof(unsigned short); len = pkt - xp; break; case STATE_TOO_LARGE: xp = pkt; *((unsigned short *) pkt) = TFTP_ERROR; pkt += sizeof(unsigned short); *((unsigned short *) pkt) = 3; pkt += sizeof(unsigned short); strcpy ((char *)pkt, "File too large"); pkt += 14 /*strlen("File too large")*/ + 1; len = pkt - xp; break; case STATE_BAD_MAGIC: xp = pkt; *((unsigned short *) pkt) = TFTP_ERROR; pkt += sizeof(unsigned short); *((unsigned short *) pkt) = 2; pkt += sizeof(unsigned short); strcpy ((char *) pkt, "File has bad magic"); pkt += 18 /*strlen("File has bad magic")*/ + 1; len = pkt - xp; break; } NetSetEther(NetTxPacket, NetServerEther, PROT_IP); NetSetIP(NetTxPacket + ETHER_HDR_SIZE, NetServerIP, TftpServerPort, TftpOurPort, len); NetSendPacket(NetTxPacket, ETHER_HDR_SIZE + IP_HDR_SIZE + len); }
bool PhotoQuery(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; int min, max; min = NetGetNumberData(p, "min"); max = NetGetNumberData(p, "max"); PhotoPost *plist; NetPacket *pkt; int len; pkt = NetCreatePacket(); plist = QueryPhotoList(DB(d),NetGetStringData(p, "id") , min, max, &len); if(plist == NULL){ pkt->header.type = PHOTO_QUERY_FAILED; NetAddStringData(pkt, "reason", REASON_MEMORY_ERROR); goto CleanUp; } else pkt->header.type = PHOTO_INFO; NetAddNumberData(pkt, "count", len); for(int i=0;i<len;i++){ char msg[8]; printf("%s %d\n", plist[i].comment, plist[i].lat); sprintf(msg,"i%d", i+1); NetAddStringData(pkt, msg, plist[i].id); sprintf(msg,"c%d", i+1); NetAddStringData(pkt, msg, plist[i].comment); sprintf(msg,"p%d", i+1); NetAddStringData(pkt, msg, plist[i].photo); sprintf(msg,"pi%d", i+1); NetAddNumberData(pkt, msg, plist[i].photo_id); sprintf(msg,"t%d", i+1); NetAddNumberData(pkt, msg, plist[i].time); sprintf(msg,"lt%d", i+1); NetAddNumberData(pkt, msg, plist[i].lat); sprintf(msg,"ln%d", i+1); NetAddNumberData(pkt, msg, plist[i].lon); sprintf(msg,"l%d", i+1); NetAddNumberData(pkt, msg, plist[i].like); } CleanUp: NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); free(plist); return ret; }
void RarpRequest (void) { int i; volatile uchar *pkt; ARP_t * rarp; printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket; pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP); rarp = (ARP_t *)pkt; rarp->ar_hrd = htons (ARP_ETHER); rarp->ar_pro = htons (PROT_IP); rarp->ar_hln = 6; rarp->ar_pln = 4; rarp->ar_op = htons (RARPOP_REQUEST); memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ /* dest. IP addr set to broadcast */ for (i = 0; i <= 3; i++) { rarp->ar_data[16 + i] = 0xff; } NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); NetSetTimeout(TIMEOUT, RarpTimeout); NetSetHandler(RarpHandler); }
bool PhotoPush(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; PhotoPost post; post.lat = NetGetNumberData(p, "lat"); post.lon = NetGetNumberData(p, "lon"); SET(post.photo, NetGetStringData(p, "photo")); SET(post.comment, NetGetStringData(p, "comment")); ret = PushPhoto(DB(d),d.handle->user->id, &post); NetPacket *pkt; pkt = NetCreatePacket(); if(ret){ pkt->header.type = PHOTO_OK; NetAddNumberData(pkt, "photo_id", post.photo_id); } else{ pkt->header.type = PHOTO_FAILED; NetAddStringData(pkt, "reason", REASON_UNKNOWN); } NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
bool PhotoCommentPush(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; PhotoComment com; com.photo_id = NetGetNumberData(p, "photo_id"); SET(com.comment, NetGetStringData(p, "comment")); ret = PushPhotoComment(DB(d),d.handle->user->id, &com); NetPacket *pkt; pkt = NetCreatePacket(); if(ret){ pkt->header.type = PHOTO_COMMENT_OK; NetAddNumberData(pkt, "comment_id", com.photo_id); } else{ pkt->header.type = PHOTO_COMMENT_FAILED; NetAddStringData(pkt, "reason", REASON_UNKNOWN); } NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
bool PhotoCommentDelete(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; ret = DeletePhotoComment( DB(d), d.handle->user->id, NetGetNumberData(p, "comment_id")); NetPacket *pkt; pkt = NetCreatePacket(); if(ret){ pkt->header.type = PHOTO_COMMENT_OK; } else{ pkt->header.type = PHOTO_COMMENT_FAILED; NetAddStringData(pkt, "reason", REASON_UNKNOWN); } NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src; IPaddr_t src_ip; int eth_hdr_size; switch (icmph->type) { case ICMP_ECHO_REPLY: src_ip = NetReadIP((void *)&ip->ip_src); if (src_ip == NetPingIP) net_set_state(NETLOOP_SUCCESS); return; case ICMP_ECHO_REQUEST: eth_hdr_size = net_update_ether(et, et->et_src, PROT_IP); debug_cond(DEBUG_DEV_PKT, "Got ICMP ECHO REQUEST, return " "%d bytes\n", eth_hdr_size + len); ip->ip_sum = 0; ip->ip_off = 0; NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); NetCopyIP((void *)&ip->ip_src, &NetOurIP); ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); icmph->type = ICMP_ECHO_REPLY; icmph->checksum = 0; icmph->checksum = ~NetCksum((uchar *)icmph, (len - IP_HDR_SIZE) >> 1); NetSendPacket((uchar *)et, eth_hdr_size + len); return; /* default: return;*/ } }
void arp_raw_request(IPaddr_t sourceIP, const uchar *targetEther, IPaddr_t targetIP) { uchar *pkt; struct arp_hdr *arp; int eth_hdr_size; debug_cond(DEBUG_DEV_PKT, "ARP broadcast %d\n", NetArpWaitTry); pkt = NetArpTxPacket; eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); pkt += eth_hdr_size; arp = (struct arp_hdr *) pkt; arp->ar_hrd = htons(ARP_ETHER); arp->ar_pro = htons(PROT_IP); arp->ar_hln = ARP_HLEN; arp->ar_pln = ARP_PLEN; arp->ar_op = htons(ARPOP_REQUEST); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source ET addr */ NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */ memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */ NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */ NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE); }
bool FriendQueryCount(PacketHandlerData d){ NetPacket *p; p = d.pkt; int ret = true; ret = QueryFriendCount(DB(d), NetGetStringData(p, "id")); NetPacket *pkt; pkt = NetCreatePacket(); if(ret == -1){ pkt->header.type = FRIEND_QUERY_FAILED; NetAddStringData(pkt, "reason", REASON_UNKNOWN); } else{ pkt->header.type = FRIEND_INFO_COUNT; NetAddNumberData(pkt, "count", ret); } NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
bool FriendFriend(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; ret = Friend(DB(d), d.handle->user->id, NetGetStringData(p, "id")); NetPacket *pkt; pkt = NetCreatePacket(); if(ret == true){ pkt->header.type = FRIEND_ADD_OK; } else{ pkt->header.type = FRIEND_ADD_FAILED; NetAddStringData(pkt, "reason", REASON_UNKNOWN); } NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int payload_len) { uchar *pkt; int eth_hdr_size; int pkt_hdr_size; /* make sure the NetTxPacket is initialized (NetInit() was called) */ assert(NetTxPacket != NULL); if (NetTxPacket == NULL) return -1; /* convert to new style broadcast */ if (dest == 0) dest = 0xFFFFFFFF; /* if broadcast, make the ether address a broadcast and don't do ARP */ if (dest == 0xFFFFFFFF) ether = NetBcastAddr; pkt = (uchar *)NetTxPacket; eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); pkt += eth_hdr_size; net_set_udp_header(pkt, dest, dport, sport, payload_len); pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE; /* if MAC address was not discovered yet, do an ARP request */ if (memcmp(ether, NetEtherNullAddr, 6) == 0) { debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest); /* save the ip and eth addr for the packet to send after arp */ NetArpWaitPacketIP = dest; NetArpWaitPacketMAC = ether; /* size of the waiting packet */ NetArpWaitTxPacketSize = pkt_hdr_size + payload_len; /* and do the ARP request */ NetArpWaitTry = 1; NetArpWaitTimerStart = get_timer(0); ArpRequest(); return 1; /* waiting */ } else { debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n", &dest, ether); NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); return 0; /* transmitted */ } }
bool LocationQuery(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; int min, max; Location *plist; NetPacket *pkt; int len; pkt = NetCreatePacket(); printf("ASDF"); plist = QueryLocation(DB(d) , &len); if(plist == NULL){ pkt->header.type = LOCATION_QUERY_FAILED; NetAddStringData(pkt, "reason", REASON_MEMORY_ERROR); goto CleanUp; } else pkt->header.type = LOCATION_INFO; NetAddNumberData(pkt, "count", len); for(int i=0;i<len;i++){ char msg[8]; sprintf(msg,"lt%d", i+1); NetAddNumberData(pkt, msg, plist[i].lat); sprintf(msg,"ln%d", i+1); NetAddNumberData(pkt, msg, plist[i].lon); sprintf(msg,"i%d", i+1); NetAddStringData(pkt, msg, plist[i].id); } CleanUp: NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); free(plist); return ret; }
bool FriendQueryFriend(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; ret = IsFriended(DB(d), NetGetStringData(p, "id1"), NetGetStringData(p, "id2")); NetPacket *pkt; pkt = NetCreatePacket(); pkt->header.type = FRIEND_INFO_FRIEND; NetAddNumberData(pkt, "result", ret); NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
bool PhotoLike(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret; ret = LikePhoto(DB(d), d.handle->user->id, NetGetNumberData(p, "photo_id")); NetPacket *pkt; pkt = NetCreatePacket(); if(ret == true){ pkt->header.type = PHOTO_OK; } else{ pkt->header.type = PHOTO_FAILED; NetAddStringData(pkt, "reason", REASON_UNKNOWN); } NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
bool FriendQueryList(PacketHandlerData d){ NetPacket *p; p = d.pkt; bool ret = true; FriendList list; ret = QueryFriendList(DB(d), NetGetStringData(p, "id"), &list, NetGetNumberData(p, "min"), NetGetNumberData(p, "max")); NetPacket *pkt; pkt = NetCreatePacket(); if(ret == false){ pkt->header.type = FRIEND_QUERY_FAILED; } else{ pkt->header.type = FRIEND_INFO_LIST; NetAddNumberData(pkt, "count", list.count); for(int i=0;i<list.count;i++){ char m[8]; sprintf(m, "i%d", i+1); NetAddStringData(pkt, m, list.id + i*16); } free(list.id); } NetSendPacket(d.handle,d.io,pkt); NetDisposePacket(pkt, true); return ret; }
void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len) { struct arp_hdr *arp; IPaddr_t reply_ip_addr; uchar *pkt; int eth_hdr_size; /* * We have to deal with two types of ARP packets: * - REQUEST packets will be answered by sending our * IP address - if we know it. * - REPLY packates are expected only after we asked * for the TFTP server's or the gateway's ethernet * address; so if we receive such a packet, we set * the server ethernet address */ debug_cond(DEBUG_NET_PKT, "Got ARP\n"); arp = (struct arp_hdr *)ip; if (len < ARP_HDR_SIZE) { printf("bad length %d < %d\n", len, ARP_HDR_SIZE); return; } if (ntohs(arp->ar_hrd) != ARP_ETHER) return; if (ntohs(arp->ar_pro) != PROT_IP) return; if (arp->ar_hln != ARP_HLEN) return; if (arp->ar_pln != ARP_PLEN) return; if (NetOurIP == 0) return; if (NetReadIP(&arp->ar_tpa) != NetOurIP) return; switch (ntohs(arp->ar_op)) { case ARPOP_REQUEST: /* reply with our IP address */ debug_cond(DEBUG_DEV_PKT, "Got ARP REQUEST, return our IP\n"); pkt = (uchar *)et; eth_hdr_size = net_update_ether(et, et->et_src, PROT_ARP); pkt += eth_hdr_size; arp->ar_op = htons(ARPOP_REPLY); memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN); NetCopyIP(&arp->ar_tpa, &arp->ar_spa); memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); NetCopyIP(&arp->ar_spa, &NetOurIP); #ifdef CONFIG_CMD_LINK_LOCAL /* * Work-around for brain-damaged Cisco equipment with * arp-proxy enabled. * * If the requesting IP is not on our subnet, wait 5ms to * reply to ARP request so that our reply will overwrite * the arp-proxy's instead of the other way around. */ if ((NetReadIP(&arp->ar_tpa) & NetOurSubnetMask) != (NetReadIP(&arp->ar_spa) & NetOurSubnetMask)) udelay(5000); #endif NetSendPacket((uchar *)et, eth_hdr_size + ARP_HDR_SIZE); return; case ARPOP_REPLY: /* arp reply */ /* are we waiting for a reply */ if (!NetArpWaitPacketIP) break; #ifdef CONFIG_KEEP_SERVERADDR if (NetServerIP == NetArpWaitPacketIP) { char buf[20]; sprintf(buf, "%pM", &arp->ar_sha); setenv("serveraddr", buf); } #endif reply_ip_addr = NetReadIP(&arp->ar_spa); /* matched waiting packet's address */ if (reply_ip_addr == NetArpWaitReplyIP) { debug_cond(DEBUG_DEV_PKT, "Got ARP REPLY, set eth addr (%pM)\n", arp->ar_data); /* save address for later use */ if (NetArpWaitPacketMAC != NULL) memcpy(NetArpWaitPacketMAC, &arp->ar_sha, ARP_HLEN); net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, 0, len); /* set the mac address in the waiting packet's header and transmit it */ memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest, &arp->ar_sha, ARP_HLEN); NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize); /* no arp request pending now */ NetArpWaitPacketIP = 0; NetArpWaitTxPacketSize = 0; NetArpWaitPacketMAC = NULL; } return; default: debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op)); return; } }