/////////////////////////////////////////////////////////////////////////////
// Prints current IP settings
/////////////////////////////////////////////////////////////////////////////
static s32 UIP_TASK_SendDebugMessage_IP(void)
{
  uip_ipaddr_t ipaddr;
  uip_gethostaddr(&ipaddr);

#if DEBUG_VERBOSE_LEVEL >= 1
  UIP_TASK_MUTEX_MIDIOUT_TAKE;
  DEBUG_MSG("[UIP_TASK] IP address: %d.%d.%d.%d\n",
	    uip_ipaddr1(ipaddr), uip_ipaddr2(ipaddr),
	    uip_ipaddr3(ipaddr), uip_ipaddr4(ipaddr));

  uip_ipaddr_t netmask;
  uip_getnetmask(&netmask);
  DEBUG_MSG("[UIP_TASK] Netmask: %d.%d.%d.%d\n",
	    uip_ipaddr1(netmask), uip_ipaddr2(netmask),
	    uip_ipaddr3(netmask), uip_ipaddr4(netmask));

  uip_ipaddr_t draddr;
  uip_getdraddr(&draddr);
  DEBUG_MSG("[UIP_TASK] Default Router (Gateway): %d.%d.%d.%d\n",
	    uip_ipaddr1(draddr), uip_ipaddr2(draddr),
	    uip_ipaddr3(draddr), uip_ipaddr4(draddr));
  UIP_TASK_MUTEX_MIDIOUT_GIVE;
#endif

  return 0; // no error
}
void handle_udp_srv_app2(void)
{
    UIP_UDP_CONN *udp_conn = uip_udp_conn;
    u16_t lport, rport;
    u8_t addr[16] = {0};
    uip_ipaddr_t ip;

    static u16_t rporttest=0;
    static u16_t j=0;
    u16_t i=0;
    u8_t testbuf[24] = {0};
    uip_ipaddr_t testip;

    lport=HTONS(udp_conn->lport);
    rport=HTONS(udp_conn->rport);
    uip_gethostaddr(&ip);

    printf_high("%s,%d, lport[%d], rport[%d] \n",__FUNCTION__,__LINE__, lport,rport);

    if (uip_newdata()) {
        rporttest = rport;

        /* Here shows how to get the peer info of the received data. */
        sprintf((char *)addr, "%d.%d.%d.%d",
                uip_ipaddr1(udp_conn->ripaddr), uip_ipaddr2(udp_conn->ripaddr),
                uip_ipaddr3(udp_conn->ripaddr), uip_ipaddr4(udp_conn->ripaddr));

        printf_high("UDP Sample server RX:  LocalPort[%d],RemoteMACAddress[%s], RemotePort[%d], Data[%s]\n",
                    HTONS(udp_conn->lport), addr, HTONS(udp_conn->rport), uip_appdata);

        printf_high("HostIP: %d.%d.%d.%d \n",
                    uip_ipaddr1(ip), uip_ipaddr2(ip), uip_ipaddr3(ip), uip_ipaddr4(ip));
    }

    {
        sprintf((char *)testbuf, "[ID=%d] UDP Test!!! \n", j++);

        /*send UDP packet to all stations connected to MT7681(AP mode),
            Notice: the rporttest is determined by the last received UDP packet*/
        for (i=0; i<UIP_DHCPD_CONNS; i++) {
            if (uip_dhpcd_conns[i].flag == 1) {
                uip_ipaddr(testip,
                           (uip_dhpcd_conns[i].yiaddr & 0x000000ff),
                           (uip_dhpcd_conns[i].yiaddr & 0x0000ff00)>>8,
                           (uip_dhpcd_conns[i].yiaddr & 0x00ff0000)>>16,
                           (uip_dhpcd_conns[i].yiaddr & 0xff000000)>>24);

                iot_udp_pkt_send_direct(testip,
                                        uip_dhpcd_conns[i].chaddr,
                                        UDP_SRV_APP2_LOCAL_PORT,
                                        rporttest,
                                        testbuf,
                                        sizeof(testbuf));
                msecDelay(10);
            }
        }
    }
Exemple #3
0
s32 SEQ_TERMINAL_PrintNetworkInfo(void *_output_function)
{
  void (*out)(char *format, ...) = _output_function;

  MUTEX_MIDIOUT_TAKE;

#if defined(MIOS32_FAMILY_EMULATION)
  out("No network informations available in emulation!");
#else

  out("MBHP_ETH module connected: %s", UIP_TASK_NetworkDeviceAvailable() ? "yes" : "no");
  if( !UIP_TASK_NetworkDeviceAvailable() ) {
    out("Please reboot your MIDIbox SEQ to restart module detection! (or just type \"reset\")");
  } else {
    out("Ethernet services running: %s", UIP_TASK_ServicesRunning() ? "yes" : "no");
    out("DHCP: %s", UIP_TASK_DHCP_EnableGet() ? "enabled" : "disabled");

    if( UIP_TASK_DHCP_EnableGet() && !UIP_TASK_ServicesRunning() ) {
      out("IP address: not available yet");
      out("Netmask: not available yet");
      out("Default Router (Gateway): not available yet");
    } else {
      uip_ipaddr_t ipaddr;
      uip_gethostaddr(&ipaddr);
      out("IP address: %d.%d.%d.%d",
	  uip_ipaddr1(ipaddr), uip_ipaddr2(ipaddr),
	  uip_ipaddr3(ipaddr), uip_ipaddr4(ipaddr));

      uip_ipaddr_t netmask;
      uip_getnetmask(&netmask);
      out("Netmask: %d.%d.%d.%d",
	  uip_ipaddr1(netmask), uip_ipaddr2(netmask),
	  uip_ipaddr3(netmask), uip_ipaddr4(netmask));

      uip_ipaddr_t draddr;
      uip_getdraddr(&draddr);
      out("Default Router (Gateway): %d.%d.%d.%d",
	  uip_ipaddr1(draddr), uip_ipaddr2(draddr),
	  uip_ipaddr3(draddr), uip_ipaddr4(draddr));
    }

    int con;
    for(con=0; con<OSC_SERVER_NUM_CONNECTIONS; ++con) {
      u32 osc_remote_ip = OSC_SERVER_RemoteIP_Get(con);
      out("OSC%d Remote address: %d.%d.%d.%d",
	  con+1,
	  (osc_remote_ip>>24)&0xff, (osc_remote_ip>>16)&0xff,
	  (osc_remote_ip>>8)&0xff, (osc_remote_ip>>0)&0xff);
      out("OSC%d Remote port: %d", con+1, OSC_SERVER_RemotePortGet(con));
      out("OSC%d Local port: %d", con+1, OSC_SERVER_LocalPortGet(con));
    }
  }
#endif
  MUTEX_MIDIOUT_GIVE;

  return 0; // no error
}
Exemple #4
0
void dhcpc_configured(const struct dhcpc_state *s)
{
	#ifdef MY_DEBUG
	rprintf("IP : %d.%d.%d.%d\n", uip_ipaddr1(s->ipaddr), uip_ipaddr2(s->ipaddr), uip_ipaddr3(s->ipaddr), uip_ipaddr4(s->ipaddr));
	rprintf("NM : %d.%d.%d.%d\n", uip_ipaddr1(s->netmask), uip_ipaddr2(s->netmask), uip_ipaddr3(s->netmask), uip_ipaddr4(s->netmask));
	rprintf("GW : %d.%d.%d.%d\n", uip_ipaddr1(s->default_router), uip_ipaddr2(s->default_router), uip_ipaddr3(s->default_router), uip_ipaddr4(s->default_router));
	#endif

    uip_sethostaddr(s->ipaddr);
    uip_setnetmask(s->netmask);
    uip_setdraddr(s->default_router);
}
Exemple #5
0
void xtcpd_get_ipconfig(xtcp_ipconfig_t *ipconfig)
{
    ipconfig->ipaddr[0] = uip_ipaddr1(uip_hostaddr);
    ipconfig->ipaddr[1] = uip_ipaddr2(uip_hostaddr);
    ipconfig->ipaddr[2] = uip_ipaddr3(uip_hostaddr);
    ipconfig->ipaddr[3] = uip_ipaddr4(uip_hostaddr);
    ipconfig->netmask[0] = uip_ipaddr1(uip_netmask);
    ipconfig->netmask[1] = uip_ipaddr2(uip_netmask);
    ipconfig->netmask[2] = uip_ipaddr3(uip_netmask);
    ipconfig->netmask[3] = uip_ipaddr4(uip_netmask);
    ipconfig->gateway[0] = uip_ipaddr1(uip_draddr);
    ipconfig->gateway[1] = uip_ipaddr2(uip_draddr);
    ipconfig->gateway[2] = uip_ipaddr3(uip_draddr);
    ipconfig->gateway[3] = uip_ipaddr4(uip_draddr);
}
void in_out_packet() {
    bool incoming = false;
    if (uip_len > 0) {
        NABTO_LOG_INFO(("incoming nabto traffic"));

        ipacket.socket = uip_udp_conn;
        ipacket.addr = 
            ((uint32_t)uip_ipaddr1(BUF->srcipaddr) << 24) +
            ((uint32_t)uip_ipaddr2(BUF->srcipaddr) << 16) +
            ((uint32_t)uip_ipaddr3(BUF->srcipaddr) << 8) +
            (uint32_t)uip_ipaddr4(BUF->srcipaddr);
        ipacket.port = htons(BUF->srcport);
        ipacket.len = uip_len;
        memcpy(ipacket.buffer, uip_appdata, uip_len);
        incoming = true;
    }

    if (opacket.socket == uip_udp_conn) { // we send on the current connection
        NABTO_LOG_INFO(("outgoing nabto traffic"));
        opacket.socket=NULL;
        uip_ipaddr(&uip_udp_conn->ripaddr, 
                   opacket.addr>>24 & 255, 
                   opacket.addr>>16 & 255, 
                   opacket.addr>>8 & 255, 
                   opacket.addr & 255);
        uip_udp_conn->rport = htons(opacket.port);
        memcpy(uip_appdata, opacket.buffer, opacket.len);
        uip_udp_send(opacket.len);
    }
Exemple #7
0
extern "C" void dhcpc_configured(const struct dhcpc_state *s)
{
    printf("Got IP address %d.%d.%d.%d\n",
           uip_ipaddr1(&s->ipaddr), uip_ipaddr2(&s->ipaddr),
           uip_ipaddr3(&s->ipaddr), uip_ipaddr4(&s->ipaddr));
    printf("Got netmask %d.%d.%d.%d\n",
           uip_ipaddr1(&s->netmask), uip_ipaddr2(&s->netmask),
           uip_ipaddr3(&s->netmask), uip_ipaddr4(&s->netmask));
    printf("Got DNS server %d.%d.%d.%d\n",
           uip_ipaddr1(&s->dnsaddr), uip_ipaddr2(&s->dnsaddr),
           uip_ipaddr3(&s->dnsaddr), uip_ipaddr4(&s->dnsaddr));
    printf("Got default router %d.%d.%d.%d\n",
           uip_ipaddr1(&s->default_router), uip_ipaddr2(&s->default_router),
           uip_ipaddr3(&s->default_router), uip_ipaddr4(&s->default_router));
    printf("Lease expires in %ld seconds\n", ntohl(s->lease_time));

    theNetwork->dhcpc_configured(s->ipaddr, s->netmask, s->default_router);
}
__attribute__ ((noinline)) void uip_printip4(const uip_ipaddr_t ip4) {
	printint(uip_ipaddr1(ip4));
	printstr(".");
	printint(uip_ipaddr2(ip4));
	printstr(".");
	printint(uip_ipaddr3(ip4));
	printstr(".");
	printint(uip_ipaddr4(ip4));
}
void handle_udp_srv_app1()
{
    u8_t addr[48] = {0};
    u8_t ipStrMaxLen = 16,strTotLen = 0;
    uip_ipaddr_t ip;
    UIP_UDP_CONN *udp_conn = uip_udp_conn;

    if (uip_newdata()) {
        /* Here shows how to get the peer info of the received data. */
        sprintf((char *)addr, "%d.%d.%d.%d",
                uip_ipaddr1(udp_conn->ripaddr),
                uip_ipaddr2(udp_conn->ripaddr),
                uip_ipaddr3(udp_conn->ripaddr),
                uip_ipaddr4(udp_conn->ripaddr));

        printf("UDP Sample server RX: lp:%d,ra:%s,rp:%d,  got:%s\n",
               HTONS(udp_conn->lport), addr, HTONS(udp_conn->rport), uip_appdata);

        /* uip_appdata is a pointer pointed the received data. */
        if (!memcmp(uip_appdata, "ip", 2)) {
            uip_gethostaddr(&ip);
#if UIP_CLOUD_SERVER_SUPPORT
            strTotLen = ipStrMaxLen + 2 + strlen(productID);
            if(sizeof(addr) >= strTotLen){
              sprintf((char *)addr, "%d.%d.%d.%d.%d.%s", 
              uip_ipaddr1(ip), uip_ipaddr2(ip), 
              uip_ipaddr3(ip), uip_ipaddr4(ip), mt76xx_Activation.ActivedFlag, productID);
            }
#else
            strTotLen = ipStrMaxLen;
            if(sizeof(addr) >= strTotLen){
              sprintf((char *)addr, "%d.%d.%d.%d", 
              uip_ipaddr1(ip), uip_ipaddr2(ip), 
              uip_ipaddr3(ip), uip_ipaddr4(ip));
            }
#endif
            uip_send(addr, sizeof(addr));
        }
    }

    if (uip_poll()) {
    }
}
s32 UIP_TASK_EffectiveGatewayGet(void)
{
  uip_ipaddr_t ipaddr;
  uip_getdraddr(&ipaddr);
  return
    (uip_ipaddr1(ipaddr) << 24) |
    (uip_ipaddr2(ipaddr) << 16) |
    (uip_ipaddr3(ipaddr) <<  8) |
    (uip_ipaddr4(ipaddr) <<  0);
}
s32 UIP_TASK_EffectiveNetmaskGet(void)
{
  uip_ipaddr_t ipaddr;
  uip_getnetmask(&ipaddr);
  return
    (uip_ipaddr1(ipaddr) << 24) |
    (uip_ipaddr2(ipaddr) << 16) |
    (uip_ipaddr3(ipaddr) <<  8) |
    (uip_ipaddr4(ipaddr) <<  0);
}
s32 UIP_TASK_IP_EffectiveAddressGet(void)
{
  uip_ipaddr_t ipaddr;
  uip_gethostaddr(&ipaddr);
  return
    (uip_ipaddr1(ipaddr) << 24) |
    (uip_ipaddr2(ipaddr) << 16) |
    (uip_ipaddr3(ipaddr) <<  8) |
    (uip_ipaddr4(ipaddr) <<  0);
}
/////////////////////////////////////////////////////////////////////////////
// Prints current IP settings
/////////////////////////////////////////////////////////////////////////////
static s32 UIP_TASK_SendDebugMessage_IP(void)
{
  uip_ipaddr_t ipaddr;
  uip_gethostaddr(&ipaddr);

  MIOS32_MIDI_SendDebugMessage("[UIP_TASK] IP address: %d.%d.%d.%d\n",
			       uip_ipaddr1(ipaddr), uip_ipaddr2(ipaddr),
			       uip_ipaddr3(ipaddr), uip_ipaddr4(ipaddr));

  uip_ipaddr_t netmask;
  uip_getnetmask(&netmask);
  MIOS32_MIDI_SendDebugMessage("[UIP_TASK] Netmask: %d.%d.%d.%d\n",
			       uip_ipaddr1(netmask), uip_ipaddr2(netmask),
			       uip_ipaddr3(netmask), uip_ipaddr4(netmask));

  uip_ipaddr_t draddr;
  uip_getdraddr(&draddr);
  MIOS32_MIDI_SendDebugMessage("[UIP_TASK] Default Router (Gateway): %d.%d.%d.%d\n",
			       uip_ipaddr1(draddr), uip_ipaddr2(draddr),
			       uip_ipaddr3(draddr), uip_ipaddr4(draddr));

  return 0; // no error
}
/////////////////////////////////////////////////////////////////////////////
// Called by DHCP client once it got IP addresses
/////////////////////////////////////////////////////////////////////////////
void dhcpc_configured(const struct dhcpc_state *s)
{
  // set IP settings
  uip_sethostaddr(s->ipaddr);
  uip_setnetmask(s->netmask);
  uip_setdraddr(s->default_router);

  // start services
  UIP_TASK_StartServices();

  // print unused settings
  MIOS32_MIDI_SendDebugMessage("[UIP_TASK] Got DNS server %d.%d.%d.%d\n",
			       uip_ipaddr1(s->dnsaddr), uip_ipaddr2(s->dnsaddr),
			       uip_ipaddr3(s->dnsaddr), uip_ipaddr4(s->dnsaddr));
  MIOS32_MIDI_SendDebugMessage("[UIP_TASK] Lease expires in %d hours\n",
			       (ntohs(s->lease_time[0])*65536ul + ntohs(s->lease_time[1]))/3600);
}
Exemple #15
0
void uip_appcall_gateway()
{
//    printf("GW : %u %u %u\r\n",ntohs(uip_conn->lport), ntohs(uip_conn->rport),uip_ipaddr1(uip_conn->ripaddr));

    if (uip_conn->lport == HTONS(80))
    {
      httpd_appcall();
    }
    else if (uip_conn->rport == HTONS(80) || uip_conn->rport == HTONS(26600))
    {
      webclient_app();
   } else if (uip_conn->lport == HTONS(1000))
   {
     // not implemented lcd_appcall();
   } else {
       printf("Unknown packet ... \r\n");
       printf("rport : %u\r\n",ntohs(uip_conn->rport));
       printf("lport : %u\r\n",ntohs(uip_conn->lport));
       printf("LEN : %u\r\n",uip_conn->len);
       printf("IP : %u.%u.%u.%u\r\n",uip_ipaddr1(uip_conn->ripaddr),uip_ipaddr2(uip_conn->ripaddr),uip_ipaddr3(uip_conn->ripaddr),uip_ipaddr4(uip_conn->ripaddr));
   }
}
Exemple #16
0
static void connections(char *str, Shell *sh)
{
    char istr[128];
    struct uip_conn *connr;
    snprintf(istr, sizeof(istr), "Initial MSS: %d, MSS: %d\n", uip_initialmss(), uip_mss());
    sh->output(istr);
    sh->output("Current connections: \n");

    for (connr = &uip_conns[0]; connr <= &uip_conns[UIP_CONNS - 1]; ++connr) {
        if(connr->tcpstateflags != UIP_CLOSED) {
            snprintf(istr, sizeof(istr), "%d, %u.%u.%u.%u:%u, %s, %u, %u, %c %c\n",
                     HTONS(connr->lport),
                     uip_ipaddr1(connr->ripaddr), uip_ipaddr2(connr->ripaddr),  uip_ipaddr3(connr->ripaddr), uip_ipaddr4(connr->ripaddr),
                     HTONS(connr->rport),
                     states[connr->tcpstateflags & UIP_TS_MASK],
                     connr->nrtx,
                     connr->timer,
                     (uip_outstanding(connr)) ? '*' : ' ',
                     (uip_stopped(connr)) ? '!' : ' ');

            sh->output(istr);
        }
    }
}
Exemple #17
0
int dhcp_release(uip_ipaddr_t server_ip)
{
	DhcpPacket release;

	// Set up the UDP connection.
	struct uip_udp_conn *conn =
		uip_udp_new(&server_ip, htonw(DhcpServerPort));
	if (!conn) {
		printf("Failed to set up UDP connection.\n");
		return 1;
	}
	uip_udp_bind(conn, htonw(DhcpClientPort));

	// Prepare the DHCP release packet.
	dhcp_prep_packet(&release, rand());
	uip_ipaddr_t my_ip;
	uip_gethostaddr(&my_ip);
	release.client_ip = (uip_ipaddr1(&my_ip) << 0) |
			    (uip_ipaddr2(&my_ip) << 8) |
			    (uip_ipaddr3(&my_ip) << 16) |
			    (uip_ipaddr4(&my_ip) << 24);
	uint8_t *options = release.options;
	int remaining = sizeof(release.options);
	uint8_t byte = DhcpRelease;
	dhcp_add_option(&options, DhcpTagMessageType, &byte, sizeof(byte),
			&remaining);
	dhcp_add_option(&options, DhcpTagEndOfList, NULL, 0, &remaining);

	// Call uip_udp_packet_send directly since we won't get a reply.
	uip_udp_packet_send(conn, &release, sizeof(release));

	dhcp_state = DhcpInit;
	uip_udp_remove(conn);

	return 0;
}
Exemple #18
0
static void print_ip_addr(const uip_ipaddr_t *ip)
{
	printf("%d.%d.%d.%d", uip_ipaddr1(ip), uip_ipaddr2(ip),
		uip_ipaddr3(ip), uip_ipaddr4(ip));
}
Exemple #19
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_dhcp(void))
{
  PT_BEGIN(&s.pt);
  
  /* try_again:*/
  s.state = STATE_SENDING;
  s.ticks = CLOCK_SECOND;

  do {
    send_discover();
    timer_set(&s.timer, s.ticks);
    PT_YIELD(&s.pt);
    PT_WAIT_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer));

    if(uip_newdata() && parse_msg() == DHCPOFFER) {
      s.state = STATE_OFFER_RECEIVED;
      break;
    }

    if(s.ticks < CLOCK_SECOND * 60) {
      s.ticks *= 2;
    }
  } while(s.state != STATE_OFFER_RECEIVED);
  
  s.ticks = CLOCK_SECOND;

  do {
    send_request();
    timer_set(&s.timer, s.ticks);
    PT_YIELD(&s.pt);
    PT_WAIT_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer));

    if(uip_newdata() && parse_msg() == DHCPACK) {
      s.state = STATE_CONFIG_RECEIVED;
      break;
    }

    if(s.ticks <= CLOCK_SECOND * 10) {
      s.ticks += CLOCK_SECOND;
    } else {
      PT_RESTART(&s.pt);
    }
  } while(s.state != STATE_CONFIG_RECEIVED);
  
#if 0
  printf("Got IP address %d.%d.%d.%d\n",
	 uip_ipaddr1(s.ipaddr), uip_ipaddr2(s.ipaddr),
	 uip_ipaddr3(s.ipaddr), uip_ipaddr4(s.ipaddr));
  printf("Got netmask %d.%d.%d.%d\n",
	 uip_ipaddr1(s.netmask), uip_ipaddr2(s.netmask),
	 uip_ipaddr3(s.netmask), uip_ipaddr4(s.netmask));
  printf("Got DNS server %d.%d.%d.%d\n",
	 uip_ipaddr1(s.dnsaddr), uip_ipaddr2(s.dnsaddr),
	 uip_ipaddr3(s.dnsaddr), uip_ipaddr4(s.dnsaddr));
  printf("Got default router %d.%d.%d.%d\n",
	 uip_ipaddr1(s.default_router), uip_ipaddr2(s.default_router),
	 uip_ipaddr3(s.default_router), uip_ipaddr4(s.default_router));
  printf("Lease expires in %ld seconds\n",
	 ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1]));
#endif

  dhcpc_configured(&s);
  
  /*  timer_stop(&s.timer);*/

  /*
   * PT_END restarts the thread so we do this instead. Eventually we
   * should reacquire expired leases here.
   */
  while(1) {
    PT_YIELD(&s.pt);
  }

  PT_END(&s.pt);
}
Exemple #20
0
int main(void)
{
	NetbootParam *param;

	// Initialize some consoles.
	serial_console_init();
	cbmem_console_init();
	video_console_init();
	input_init();

	printf("\n\nStarting netboot on " CONFIG_BOARD "...\n");

	timestamp_init();

	if (run_init_funcs())
		halt();

	// Make sure graphics are available if they aren't already.
	enable_graphics();

	dc_usb_initialize();

	srand(timer_raw_value());

	printf("Looking for network device... ");
	while (!net_get_device())
		usb_poll();
	printf("done.\n");

	printf("Waiting for link... ");
	int ready = 0;
	while (!ready) {
		if (net_ready(&ready))
			halt();
	}
	mdelay(200);	// some dongles need more time than they think
	printf("done.\n");

	// Start up the network stack.
	uip_init();

	// Plug in the MAC address.
	const uip_eth_addr *mac_addr = net_get_mac();
	if (!mac_addr)
		halt();
	printf("MAC: ");
	print_mac_addr(mac_addr);
	printf("\n");
	uip_setethaddr(*mac_addr);

	// Find out who we are.
	uip_ipaddr_t my_ip, next_ip, server_ip;
	const char *dhcp_bootfile;
	while (dhcp_request(&next_ip, &server_ip, &dhcp_bootfile))
		printf("Dhcp failed, retrying.\n");

	printf("My ip is ");
	uip_gethostaddr(&my_ip);
	print_ip_addr(&my_ip);
	printf("\nThe DHCP server ip is ");
	print_ip_addr(&server_ip);
	printf("\n");

	// Retrieve settings from the shared data area.
	FmapArea shared_data;
	if (fmap_find_area("SHARED_DATA", &shared_data)) {
		printf("Couldn't find the shared data area.\n");
		halt();
	}
	void *data = flash_read(shared_data.offset, shared_data.size);
	netboot_params_init(data, shared_data.size);

	// Get TFTP server IP and file name from params with DHCP as fallback
	uip_ipaddr_t *tftp_ip = NULL;
	param = netboot_params_val(NetbootParamIdTftpServerIp);
	if (param->data && param->size >= sizeof(uip_ipaddr_t)) {
		tftp_ip = (uip_ipaddr_t *)param->data;
		printf("TFTP server IP set from firmware parameters: ");
	} else {
		tftp_ip = &next_ip;
		printf("TFTP server IP supplied by DHCP server: ");
	}
	print_ip_addr(tftp_ip);
	printf("\n");

	const char *bootfile = NULL;
	param = netboot_params_val(NetbootParamIdBootfile);
	if (param->data && param->size > 0 && strnlen((const char *)param->data,
			param->size) < param->size) {
		bootfile = (const char *)param->data;
		printf("Bootfile set from firmware parameters: %s\n", bootfile);
	} else {
		bootfile = dhcp_bootfile;
		printf("Bootfile supplied by DHCP server: %s\n", bootfile);
	}

	// Download the bootfile.
	uint32_t size;
	if (tftp_read(payload, tftp_ip, bootfile, &size, MaxPayloadSize)) {
		printf("Tftp failed.\n");
		if (dhcp_release(server_ip))
			printf("Dhcp release failed.\n");
		halt();
	}
	printf("The bootfile was %d bytes long.\n", size);

	// Use command line from params when present (added to the default).
	param = netboot_params_val(NetbootParamIdKernelArgs);
	if (param->data && param->size > 0 && *(char *)param->data != '\0') {
		cmd_line[sizeof(def_cmd_line) - 1] = ' ';
		strncpy(&cmd_line[sizeof(def_cmd_line)], param->data,
			sizeof(cmd_line) - sizeof(def_cmd_line));
		printf("Command line set from firmware parameters.\n");
	// Otherwise, try to fetch it dynamically as a TFTP file.
	} else if (!(tftp_read(cmd_line, tftp_ip, "cmdline." CONFIG_BOARD,
			       &size, sizeof(cmd_line) - 1))) {
		while (cmd_line[size - 1] <= ' ')  // strip trailing whitespace
			if (!--size) break;	   // and control chars (\n, \r)
		cmd_line[size] = '\0';
		while (size--)			   // replace inline control
			if (cmd_line[size] < ' ')  // chars with spaces
				cmd_line[size] = ' ';
		printf("Command line loaded dynamically from TFTP server.\n");
	// If the file doesn't exist, finally fall back to built-in default.
	} else {
		printf("No command line from TFTP, falling back to default.\n");
	}
	cmd_line[sizeof(cmd_line) - 1] = '\0';

	// We're done on the network, so release our IP.
	if (dhcp_release(server_ip)) {
		printf("Dhcp release failed.\n");
		halt();
	}

	// Add tftp server IP into command line.
	static const char def_tftp_cmdline[] = " tftpserverip=xxx.xxx.xxx.xxx";
	const int tftp_cmdline_def_size = sizeof(def_tftp_cmdline) - 1;
	int cmd_line_size = strlen(cmd_line);
	if (cmd_line_size + tftp_cmdline_def_size >= sizeof(cmd_line)) {
		printf("Out of space adding TFTP server IP to the command line.\n");
		return 1;
	}
	sprintf(&cmd_line[cmd_line_size], " tftpserverip=%d.%d.%d.%d",
		uip_ipaddr1(tftp_ip), uip_ipaddr2(tftp_ip),
		uip_ipaddr3(tftp_ip), uip_ipaddr4(tftp_ip));
	printf("The command line is: %s\n", cmd_line);

	// Boot.
	boot(payload, cmd_line, NULL, NULL);

	// We should never get here.
	printf("Got to the end!\n");
	halt();
	return 0;
}
__attribute__ ((noinline)) void uip_printip4(const uip_ipaddr_t ip4) {
	printf("%i.%i.%i.%i\n",uip_ipaddr1(ip4), uip_ipaddr2(ip4), uip_ipaddr3(ip4), uip_ipaddr4(ip4));
}
Exemple #22
0
/*
 * This function is called whenever an uIP event occurs (e.g. when
 * a new connection is established, new data arrives, sent data is
 * acknowledged, data needs to be retransmitted, etc.), from the 
 * uip_tcp_appcall() callback.
 */
void ftpd_appcall( void )
{


    u16_t i;

    //PRINT_Log("\r\nftpd_appcall\r\n");

    /*
     * First we must check that it is the port we are listening on as
     * this function will be called for all events on all connections.
     */
    if (uip_conn->lport == HTONS(FTPD_CONTROL_PORT))
    {

    	ftpd_appstate_t *s = (ftpd_appstate_t *)uip_conn->appdata;
        /*
         * If a new connection we need to check for a few possibilities.
         */
        if (uip_connected())
        {
        	PRINT_Log("\r\nNew command connection from IP Address %d.%d.%d.%d:%d",
                   uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr),
                   uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr),
                   ntohs(uip_conn->rport));


            if (!driveAttached)
            {
            	PRINT_Log("\r\nNo drive attached!");
                // Initialise the ProtoSocket for the 421 message
                PSOCK_INIT(&s->ps, NULL, 0);
                // Set the flag to cause it to be sent
                s->resp421_flag = TRUE;
            }
            else
            {
                // find a new FTP server structure
                for (i = 0; i < NUM_FTP_DAEMONS; i++)
                {
                    if (FTP_UNUSED == ftpd_state[i].state)
                    {
                        break;
                    }
                }
                
                // Check if we found a free ftpd_state
                if (NUM_FTP_DAEMONS == i)
                {
                	PRINT_Log("\r\nNo daemons available!");
                    // Initialise the ProtoSocket for the 421 message
                    PSOCK_INIT(&s->ps, NULL, 0);
                    // Set the flag to cause it to be sent
                    s->resp421_flag = TRUE;
                }
                else
                {
                	PRINT_Log("\r\nFound unused deamon %d", i);
                    s->ftpd_state_ptr = &ftpd_state[i];
                    // Initialise the FTP Daemon state
                    ftpd_state[i].state = LOGGED_OUT;
                    ftpd_state[i].data_conn = NULL;
                    //ftpd_state[i].clientip = uip_conn->ripaddr;
                    memcpy(ftpd_state[i].clientip,uip_conn->ripaddr,sizeof(uip_ipaddr_t));
                    ftpd_state[i].clientport = FTPC_DATA_PORT;
                    // Clear the command index down
                    ftpd_state[i].cmd_index = CMD_INDEX_IDLE;
                    // Reset the directory pointer
                    //ftpd_state[i].pCurrent_dir = NULL;
                    // Clear the current path
                    strcpy(ftpd_state[i].Current_path, "/");
                    // Setup the PASV information
                    ftpd_state[i].pasvflag = FALSE;
                    
                    // Initialise the ProtoSockets and ProtoThread used by the command parser
                    PSOCK_INIT(&ftpd_state[i].cmdpsin, ftpd_state[i].cmdinputbuffer, CMD_BUFFER_SIZE);
                    PT_INIT(&ftpd_state[i].cmdpt);
                    // Clear the 421 response flag
                    s->resp421_flag = FALSE;
                }
            }
        }
        else if (uip_closed() || uip_aborted() || uip_timedout())
        {
        	PRINT_Log("\r\nCommand connection closing");
            if (s->ftpd_state_ptr)
            {
            	PRINT_Log(" for daemon %d", s->ftpd_state_ptr->pasvport - PASV_PORT_OFFSET);
                // Free up the FTP daemon
                s->ftpd_state_ptr->state = FTP_UNUSED;
                s->ftpd_state_ptr = NULL;
            }
        }
      
        if (s->resp421_flag)
        {
            send_421_response(s);
        }
        else if (s->ftpd_state_ptr)
        {
            handle_cmd_input(s->ftpd_state_ptr);
            handle_cmd_output(s->ftpd_state_ptr);
        }
    }
    else
    {
    	//PRINT_Log("\r\nSearch though all the FTPD Structures and see if this one of our data connections.");
        /* 
         * Search though all the FTPD Structures and see if this one of our data connections.
         */
        u16_t i;
        for (i = 0; i < NUM_FTP_DAEMONS; i++)
        {
            if (ftpd_state[i].data_conn == uip_conn)
            {
                if (uip_connected())
                {
                	PRINT_Log("\r\nNew active data connection for daemon %d to IP Address %d.%d.%d.%d:%d", i,
                           uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr),
                           uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr),
                           ntohs(uip_conn->rport));
                    PSOCK_INIT(&ftpd_state[i].dataps, ftpd_state[i].datainputbuffer, DATA_BUFFER_SIZE);
                    // Move the state on as required
                    if (WAITING_FOR_CONNECT == ftpd_state[i].state)
                    {
                        ftpd_state[i].state = CONNECT_OK;
                    }
                }
                else if (uip_closed() || uip_aborted() || uip_timedout())
                {
                	PRINT_Log("\r\nData connection closing for daemon %d", i);
                    if (uip_closed() && (TRANSFER_RECV == ftpd_state[i].state))
                    {
                        // There may be data in this last packet
                        handle_data_connection(&ftpd_state[i]);
                        // Closing of the connection in a RECV state indicates end of file
#if 1
                        CloseFile(&ftpd_state[i]);

#endif
                        //close the file in here

                        PRINT_Log("\r\nFinished file receive for daemon %d", i);
                        ftpd_state[i].state = TRANSFER_DONE;
                    }
                    else if((TRANSFER_LIST == ftpd_state[i].state) || (TRANSFER_SEND == ftpd_state[i].state) || (TRANSFER_RECV == ftpd_state[i].state))
                    {
                    	PRINT_Log("\r\nAborting data transfer for daemon %d", i);
                        ftpd_state[i].state = TRANSFER_ABORT;
                    }
                    ftpd_state[i].data_conn = NULL;
                }
                
                if (ftpd_state[i].data_conn)
                {
                    handle_data_connection(&ftpd_state[i]);
                }
                // Leave the loop early
                break;
            }
            else
            {
                // Check for a new connection in Passive mode
                if(ftpd_state[i].pasvflag && uip_connected() && (ntohs(uip_conn->lport) == ftpd_state[i].pasvport))
                {
                	PRINT_Log("\r\nNew passive data connection for daemon %d from IP Address %d.%d.%d.%d:%d", i,
                           uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr),
                           uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr),
                           ntohs(uip_conn->rport));
                    ftpd_state[i].data_conn = uip_conn;
                    PSOCK_INIT(&ftpd_state[i].dataps, ftpd_state[i].datainputbuffer, DATA_BUFFER_SIZE);
                    // Move the state on as required
                    if (WAITING_FOR_CONNECT == ftpd_state[i].state)
                    {
                        ftpd_state[i].state = CONNECT_OK;
                    }
                    handle_data_connection(&ftpd_state[i]);
                    // Leave the loop early
                    break;
                }
            }
        }
    }
}
Exemple #23
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_dhcp(void))
{
  PT_BEGIN(&s.pt);

#if defined PORT_APP_MAPPER
  dhcpc_running = 1; 
#endif


  if (s.state == STATE_RENEW)
      goto send_request_section;

  /* try_again:*/
  s.state = STATE_SENDING;
  s.ticks = CLOCK_SECOND;

//sendString("\r\ndhcpc handle dhcp passed: STATE_SENDING");
  do {
    send_discover();
    timer_set(&s.timer, s.ticks);
    // NOTE: fixed as per http://www.mail-archive.com/[email protected]/msg00003.html
    PT_YIELD_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer));
//sendString("Just got something\n\r");

    if(uip_newdata())
    {
//sendString("Data\n\r");
        if (parse_msg() == DHCPOFFER)
        {
            s.state = STATE_OFFER_RECEIVED;
            break;
        }
    }
    else
    {
//sendString("Timeout\n\r");
        if(s.ticks < CLOCK_SECOND * 60) {
            s.ticks *= 2;
        }
        else
        {
            s.ticks = CLOCK_SECOND;
        }
    }

  } while(s.state != STATE_OFFER_RECEIVED);
  
//sendString("\r\ndhcpc handle dhcp passed: STATE_OFFER_RECEIVED");
  s.ticks = CLOCK_SECOND;

send_request_section:
  do {
    send_request();
    timer_set(&s.timer, s.ticks);
    // NOTE: fixed as per http://www.mail-archive.com/[email protected]/msg00003.html
    PT_YIELD_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer));

    if(uip_newdata())
    {
        msg_type = parse_msg();
        if (msg_type == DHCPACK)
        {
            s.state = STATE_CONFIG_RECEIVED;
            break;
        }
        else if (msg_type == DHCPNAK)
        {
            s.state = STATE_FAIL;
            goto close_and_clean_up;
        }
    }
    else
    {
        if(s.ticks <= CLOCK_SECOND * 10) {
          s.ticks += CLOCK_SECOND;
        } else {
          PT_RESTART(&s.pt);
//sendString("\r\ndhcpc handle RESTARTING!");
        }
    }
  } while(s.state != STATE_CONFIG_RECEIVED);
//sendString("\r\ndhcpc handle dhcp passed: STATE_CONFIG_RECEIVED");
  
#if DEBUG_SERIAL
  printf_P(PSTR("Got IP address %d.%d.%d.%d\r\n"),
	 uip_ipaddr1(s.ipaddr), uip_ipaddr2(s.ipaddr),
	 uip_ipaddr3(s.ipaddr), uip_ipaddr4(s.ipaddr));
  printf_P(PSTR("Got netmask %d.%d.%d.%d\r\n"),
	 uip_ipaddr1(s.netmask), uip_ipaddr2(s.netmask),
	 uip_ipaddr3(s.netmask), uip_ipaddr4(s.netmask));
  printf_P(PSTR("Got DNS server %d.%d.%d.%d\r\n"),
	 uip_ipaddr1(s.dnsaddr), uip_ipaddr2(s.dnsaddr),
	 uip_ipaddr3(s.dnsaddr), uip_ipaddr4(s.dnsaddr));
  printf_P(PSTR("Got default router %d.%d.%d.%d\r\n"),
	 uip_ipaddr1(s.default_router), uip_ipaddr2(s.default_router),
	 uip_ipaddr3(s.default_router), uip_ipaddr4(s.default_router));
  printf_P(PSTR("Lease expires in %ld seconds\r\n"),
	 ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1]));
#endif

  dhcpc_configured(&s);
  
  /*  timer_stop(&s.timer);*/

  /*
   * PT_END restarts the thread so we do this instead. Eventually we
   * should reacquire expired leases here.
   */
/*  while(1) {
    PT_YIELD(&s.pt);
  }
*/
close_and_clean_up:
#if defined PORT_APP_MAPPER
  dhcpc_running = 0; 
#endif

  // all done with the connection, clean up
  uip_udp_remove(s.conn);
  s.conn = NULL;
//sendString("\r\ndhcpc handle dhcp passed: END");
  PT_END(&s.pt);
}
void handle_tcp_app(void)
{
    /*
    * The uip_conn structure has a field called "appstate" that holds
    * the application state of the connection. We make a pointer to
    * this to access it easier.
    */
    struct iot_tcp_app_state *s = &(uip_conn->appstate);
    u16_t lport = HTONS(uip_conn->lport);

    if (uip_aborted() || uip_timedout() || uip_closed()) {
        switch (lport) {
            case 7682: //IoT as clent.
                cli_fd = -1;
        }
        printf("fd %d uip_aborted.%d\n", uip_conn->fd, HTONS(uip_conn->lport));
#if ENABLE_DATAPARSING_SEQUENCE_MGMT
        IoT_cp_app_connection_closed(uip_conn->fd);
#endif
        s->state = IOT_APP_S_CLOSED;
        s->buf = NULL;
        s->len = 0;
        uip_abort();
    }

    if (uip_connected()) {
        u8_t raddr[16];
        u8_t logon_msg[16] = "userlogon:";

        sprintf((char *)raddr, "%d.%d.%d.%d",
                uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr),
                uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr));

        printf_high("Connected fd:%d,lp:%d,ra:%s,rp:%d\n",
                    uip_conn->fd, HTONS(uip_conn->lport), raddr, HTONS(uip_conn->rport));

#if ENABLE_DATAPARSING_SEQUENCE_MGMT
        IoT_cp_app_connection_connected(uip_conn->fd
#if (NO_USED_CODE_REMOVE==0)
                                        ,HTONS(uip_conn->lport),
                                        raddr,
                                        HTONS(uip_conn->rport)
#endif
                                       );
#endif

        s->state = IOT_APP_S_CONNECTED;
        switch (lport) {
            case 7682:
                memcpy(logon_msg+10, gCurrentAddress, 6);
                uip_send(logon_msg, 16);
                break;
        }
    }

    if (uip_acked()) {
        printf("uip_acked.\n");
        s->state = IOT_APP_S_DATA_ACKED;
        s->buf = NULL;
        s->len = 0;
    }

    if (uip_newdata()) {
        printf("RX fd : %d\n", uip_conn->fd);
        if (lport == IoTpAd.ComCfg.Local_TCP_Srv_Port || lport==7682) {
#if ENABLE_DATAPARSING_SEQUENCE_MGMT
            iot_app_proc_pkt(uip_conn->fd, uip_appdata,uip_datalen());
#else
            iot_app_proc_pkt(uip_appdata,uip_datalen());
#endif
#if CFG_SUPPORT_TCPIP_ROBUST_TEST
        } else if (lport==7684) {
            uip_send(uip_appdata, uip_datalen());
#endif
        } else {
#if ATCMD_TCPIP_SUPPORT
            iot_uart_output(uip_appdata, (int16)uip_datalen());
#endif
        }
    }

    /* check if we have data to xmit for this connection.*/
    if (uip_poll()) {
#if ATCMD_TCPIP_SUPPORT
        if (s->state == IOT_APP_S_CLOSED) {
            uip_close();
        } else if (s->len > 0) {
            uip_send(s->buf, s->len);
            s->state = IOT_APP_S_DATA_SEND;
        }
#endif
    }
}