Exemplo n.º 1
0
static int udp_sendmsg(struct sock *sk, struct msghdr *msg, int len, int noblock, 
	int flags)
{
	if(msg->msg_iovlen==1)
		return udp_sendto(sk,msg->msg_iov[0].iov_base,len, noblock, flags, msg->msg_name, msg->msg_namelen);
	else
	{
		/*
		 *	For awkward cases we linearise the buffer first. In theory this is only frames
		 *	whose iovec's don't split on 4 byte boundaries, and soon encrypted stuff (to keep
		 *	skip happy). We are a bit more general about it.
		 */
		 
		unsigned char *buf;
		int fs;
		int err;
		if(len>65515)
			return -EMSGSIZE;
		buf=kmalloc(len, GFP_KERNEL);
		if(buf==NULL)
			return -ENOBUFS;
		memcpy_fromiovec(buf, msg->msg_iov, len);
		fs=get_fs();
		set_fs(get_ds());
		err=udp_sendto(sk,buf,len, noblock, flags, msg->msg_name, msg->msg_namelen);
		set_fs(fs);
		kfree_s(buf,len);
		return err;
	}
}
Exemplo n.º 2
0
static void ICACHE_FLASH_ATTR send_offer(struct dhcps_msg *m)
{
        uint8_t *end;
	    struct pbuf *p, *q;
	    u8_t *data;
	    u16_t cnt=0;
	    u16_t i;
        create_msg(m);

        end = add_msg_type(&m->options[4], DHCPOFFER);
        end = add_offer_options(end);
        end = add_end(end);

	    p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM);
#if DHCPS_DEBUG
		os_printf("udhcp: send_offer>>p->ref = %d\n", p->ref);
#endif
	    if(p != NULL){

#if DHCPS_DEBUG
	        os_printf("dhcps: send_offer>>pbuf_alloc succeed\n");
	        os_printf("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len);
	        os_printf("dhcps: send_offer>>p->len = %d\n", p->len);
#endif
	        q = p;
	        while(q != NULL){
	            data = (u8_t *)q->payload;
	            for(i=0; i<q->len; i++)
	            {
	                data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG
					os_printf("%02x ",data[i]);
					if((i+1)%16 == 0){
						os_printf("\n");
					}
#endif
	            }

	            q = q->next;
	        }
	    }else{

#if DHCPS_DEBUG
	        os_printf("dhcps: send_offer>>pbuf_alloc failed\n");
#endif
	        return;
	    }
#if DHCPS_DEBUG
        err_t SendOffer_err_t = udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT );
        os_printf("dhcps: send_offer>>udp_sendto result %x\n",SendOffer_err_t);
#else
        udp_sendto( pcb_dhcps, p, &broadcast_dhcps, DHCPS_CLIENT_PORT );
#endif
	    if(p->ref != 0){
#if DHCPS_DEBUG
	        os_printf("udhcp: send_offer>>free pbuf\n");
#endif
	        pbuf_free(p);
	    }
}
Exemplo n.º 3
0
/**
 * 	Handler called from the UDP stack.  If message is relevant, then send
 * 	back a reply.  Return 1 tells UDP stack not to copy this into a
 * 	client buffer; it has already been taken care of-thank you.
 *
 * 	RETURN: 	1 = handler has done all relevant processing for datagram.
 */
int echo_handler(int event, udp_Socket * s, ll_Gather * g,
						_udp_datagram_info * udi)
{
	// Datagram has come in.  It is in the Ethernet receive buffer.  No need to
	// copy it anywhere - we just transmit it straight back to the sender.
	// The relevant information comes in the following fields (not all of which
	// we use here) (see LIB\tcpip\net.lib for structure):
	//  g->data1  ->  IP and UDP headers (root)
	//  g->len1   ->  IP and UDP header length
	//  g->data2  ->  UDP datagram data (far) - first buffer
	//  g->len2   ->  UDP datagram data length - first buffer
	//  g->data3  ->  UDP datagram data (far) - second buffer **
	//  g->len3   ->  UDP datagram data length - second buffer
	//  udi->remip  -> sender's IP address
	//  udi->remport  ->  sender's UDP port number
	//  udi->flags  -> flags.

   // ** Note: prior to Dynamic C 9.0, only one buffer would be provided (i.e. g->data3 would
   // always be zero).  From DC9.0, it is now possible for the incoming data to be split into
   // two areas.

	// The 'event' parameter determines the type of event.  As of DC 7.30, this is either
	//  UDP_DH_INDATA : incoming datagram
	//  UDP_DH_ICMPMSG : incoming ICMP message.

	if (event == UDP_DH_ICMPMSG) {
		return 1;	// Just ignore incoming ICMP errors.
	}

	// Otherwise, bounce the packet back!  We ignore errors because this
	// is UDP, and the sender will try again.  Not a good idea in general, though.

   printf("Got UDP  len1=%2u len2=%4u len3=%4u remip=%08lX remport=%5u len=%u\n",
   	g->len1, g->len2, g->len3, udi->remip, udi->remport, udi->len);

	if (!g->len3)
   	// No second buffer.  This is easy - just use udp_sendto directly
		udp_sendto(s, g->data2, g->len2, udi->remip, udi->remport);
   else {
   	// Awkward: got 2 areas, so copy them into a contiguous root buffer and send.
		_f_memcpy(pktbuf, g->data2, g->len2);
      _f_memcpy(pktbuf + g->len2, g->data3, g->len3);
		udp_sendto(s, pktbuf, g->len2+g->len3, udi->remip, udi->remport);
   }

	// Return 1 to indicate that all processing has been done.  No copy to
	// normal udp socket receive buffer.
	return 1;
}
Exemplo n.º 4
0
/**
  * @brief  sends a TFTP message
  * @param  upcb: pointer on a udp pcb
  * @param  to_ip: pointer on remote IP address
  * @param  to_port: pointer on remote port  
  * @param buf: pointer on buffer where to create the message  
  * @param err: error code of type tftp_errorcode
  * @retval error code
  */
err_t tftp_send_message(struct udp_pcb *upcb, struct ip_addr *to_ip, unsigned short to_port, char *buf, unsigned short buflen)
{
  err_t err;
  struct pbuf *pkt_buf; /* Chain of pbuf's to be sent */


  /* PBUF_TRANSPORT - specifies the transport layer */
  pkt_buf = pbuf_alloc(PBUF_TRANSPORT, buflen, PBUF_POOL);

  if (!pkt_buf)      /*if the packet pbuf == NULL exit and end transmission */
    return ERR_MEM;

  /* Copy the original data buffer over to the packet buffer's payload */
  // memcpy(pkt_buf->payload, buf, buflen);
  // change use this to prevent error
  pbuf_take(pkt_buf, buf, buflen);

  /* Sending packet by UDP protocol */
  err = udp_sendto(upcb, pkt_buf, to_ip, to_port);

  /* free the buffer pbuf */
  pbuf_free(pkt_buf);

  return err;
}
Exemplo n.º 5
0
void mg_if_udp_send(struct mg_connection *nc, const void *buf, size_t len) {
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
  if (nc->sock == INVALID_SOCKET || cs->pcb.udp == NULL) {
    /*
     * In case of UDP, this usually means, what
     * async DNS resolve is still in progress and connection
     * is not ready yet
     */
    DBG(("%p socket is not connected", nc));
    return;
  }
  struct udp_pcb *upcb = cs->pcb.udp;
  struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
  ip_addr_t *ip = (ip_addr_t *) &nc->sa.sin.sin_addr.s_addr;
  u16_t port = ntohs(nc->sa.sin.sin_port);
  memcpy(p->payload, buf, len);
  cs->err = udp_sendto(upcb, p, (ip_addr_t *) ip, port);
  DBG(("%p udp_sendto = %d", nc, cs->err));
  pbuf_free(p);
  if (cs->err != ERR_OK) {
    mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
  } else {
    cs->num_sent += len;
    mg_lwip_post_signal(MG_SIG_SENT_CB, nc);
  }
}
socket_error_t lwipv4_socket_send_to(struct socket *socket, const void * buf, const size_t len, const struct socket_addr *addr, const uint16_t port)
{
    ip_addr_t a;
    err_t err = ERR_VAL;
    switch(socket->family) {
    case SOCKET_DGRAM: {
        struct pbuf *pb = pbuf_alloc(PBUF_TRANSPORT,len,PBUF_RAM);
        socket_event_t e;
        socket_api_handler_t handler = socket->handler;
        err = pbuf_take(pb, buf, len);
        if (err != ERR_OK)
        	break;
        a.addr = socket_addr_get_ipv4_addr(addr);
        err = udp_sendto(socket->impl, pb, &a, port);
        pbuf_free(pb);
        if (err != ERR_OK)
        	break;
        e.event = SOCKET_EVENT_TX_DONE;
        e.sock = socket;
        e.i.t.sentbytes = len;
        socket->event = &e;
        handler();
        socket->event = NULL;
        break;
    }
    case SOCKET_STREAM:
        err = ERR_USE;
        break;
    }
    return lwipv4_socket_error_remap(err);

}
Exemplo n.º 7
0
// Helper function for send/sendto to handle UDP packets.
STATIC mp_uint_t lwip_udp_send(lwip_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) {
    if (len > 0xffff) {
        // Any packet that big is probably going to fail the pbuf_alloc anyway, but may as well try
        len = 0xffff;
    }

    // FIXME: maybe PBUF_ROM?
    struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
    if (p == NULL) {
        *_errno = ENOMEM;
        return -1;
    }

    memcpy(p->payload, buf, len);

    err_t err;
    if (ip == NULL) {
        err = udp_send((struct udp_pcb*)socket->pcb, p);
    } else {
        ip_addr_t dest;
        IP4_ADDR(&dest, ip[0], ip[1], ip[2], ip[3]);
        err = udp_sendto((struct udp_pcb*)socket->pcb, p, &dest, port);
    }

    pbuf_free(p);

    if (err != ERR_OK) {
        *_errno = error_lookup_table[-err];
        return -1;
    }

    return len;
}
Exemplo n.º 8
0
int SockLib::sock_sendto(int sock, char *buf, int length, int handshake) {
	int ret = SOCKET_ERROR, left = length;
	UDPPACKET udp;
	char *p = buf;

	// divide the tcp packet into small udp frames
	while (left > 0) {
		// add udp header and copy the data
		if (add_udpheader(&udp, p)) {
			return -1;
		}
		if (handshake) {
			udp.ackType = ACKTYPE_HANDSHAKE;
		}
		if (udp_sendto(sock, (char *)&udp, sizeof(UDPPACKET), handshake) <= 0) {
			return -1;
		}

		left -= BUFFER_LENGTH;
		p = buf + BUFFER_LENGTH;
		sendCnt++;

		if (showFile) {
			printf("%s\n", udp.data);
		}
	}
	if (left < 0) {
		left = 0;		// to be compatible with TCP
	}

	return left;
}
Exemplo n.º 9
0
errval_t tftp_send_ack(struct udp_pcb *pcb, uint32_t blockno,
                       struct ip_addr *addr, u16_t port,
                       struct pbuf *p, void *payload)
{
    TFTP_DEBUG_PACKETS("sending ack(%u)\n", blockno);

    p->len = TFTP_MAX_MSGSIZE;
    p->tot_len = TFTP_MAX_MSGSIZE;
    p->payload = payload;

    memset(p->payload, 0, sizeof(uint32_t) + sizeof(uint16_t));

    size_t length = set_opcode(p->payload, TFTP_OP_ACK);
    length += set_block_no(p->payload + length, blockno);

    p->len = (uint16_t)length +1;
    p->tot_len = (uint16_t)length+1;

    int r = udp_sendto(pcb, p, addr, port);
    if (r != ERR_OK) {
        TFTP_DEBUG("send failed\n");
    }

    return SYS_ERR_OK;
}
Exemplo n.º 10
0
/** Actually send an sntp request to a server.
 *
 * @param server_addr resolved IP address of the SNTP server
 */
static void sntp_send_request(ip_addr_t *server_addr) {
    struct pbuf* p;
    p = pbuf_alloc(PBUF_TRANSPORT, SNTP_MSG_LEN, PBUF_RAM);

    if (p != NULL) {
        struct sntp_msg *sntpmsg = (struct sntp_msg *)p->payload;
        LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_send_request: Sending request to server\n"));

        /* Initialize request message */
        sntp_initialize_request(sntpmsg);

        /* Send request */
        udp_sendto(sntp_pcb, p, server_addr, SNTP_PORT);
        pbuf_free(p);    // [iva2k] fixing memory leak

        /* Set up receive timeout: try next server or retry on timeout */
        sys_timeout((u32_t)SNTP_RECV_TIMEOUT, sntp_try_next_server, NULL);

#if SNTP_CHECK_RESPONSE >= 1
        /* Save server address to verify it in sntp_recv */
        ip_addr_set(&sntp_last_server_address, server_addr);
#endif /* SNTP_CHECK_RESPONSE >= 1 */
    } else {
        LWIP_DEBUGF(SNTP_DEBUG_SERIOUS, ("sntp_send_request: Out of memory, trying again in %"U32_F" ms\n",
                                         (u32_t)SNTP_RETRY_TIMEOUT));
        /* Out of memory: set up a timer to send a retry */
        sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_request, NULL);
    }
}
Exemplo n.º 11
0
Usage: test_udp client server_port\n\
       test_udp server server_port\n\
";

#define PING "ping"
#define PONG "pong"
#define TIMES 10

static void client(int port) {
    int i, s;
    Octstr *ping, *pong, *addr, *from;

    s = udp_client_socket();
    ping = octstr_create(PING);
    addr = udp_create_address(octstr_create("localhost"), port);
    if (s == -1 || addr == NULL)
        panic(0, "Couldn't set up client socket.");

    for (i = 0; i < TIMES; ++i) {
        if (udp_sendto(s, ping, addr) == -1)
            panic(0, "Couldn't send ping.");
        if (udp_recvfrom(s, &pong, &from) == -1)
            panic(0, "Couldn't receive pong");
        info(0, "Got <%s> from <%s:%d>", octstr_get_cstr(pong),
             octstr_get_cstr(udp_get_ip(from)),
             udp_get_port(from));
    }
}
Exemplo n.º 12
0
/*..........................................................................*/
err_t send_buffer (struct udp_pcb *pcb,
		                 /*struct ip_addr *to_ip,
		                 int to_port,*/
		                 char *buf,
		                 int buflen)
{
	struct ip_addr *to_ip = &(pcb->remote_ip);
	int to_port = pcb->remote_port;

    err_t err;
    struct pbuf *pkt_buf;

    pkt_buf = pbuf_alloc(PBUF_TRANSPORT, buflen, PBUF_POOL);
    if (!pkt_buf)
    {
//        iprintf("error allocating pkt buffer\n\r");
        return ERR_MEM;
    }
    // Copy the original data buffer over to the packet buffer's payload
    memcpy(pkt_buf->payload, buf, buflen);
    // send message
    err = udp_sendto(pcb, pkt_buf, to_ip, to_port);
    pbuf_free(pkt_buf);
    return err;
}
Exemplo n.º 13
0
/**
 * Send data on a UDP pcb 
 */
static void net_do_send(void *ctx)
{
    struct tls_net_msg *msg = (struct tls_net_msg *)ctx;
    struct tls_netconn *conn = msg->conn;
    struct pbuf *p;

    //TLS_DBGPRT_INFO("=====>\n"); 

    p = msg->p;
#if LWIP_CHECKSUM_ON_COPY
    if (ip_addr_isany(&msg->addr)) {
        msg->err = udp_send_chksum(conn->pcb.udp, p,
                0, 0);
    } else {
        msg->err = udp_sendto_chksum(conn->pcb.udp, p,
                &msg->addr, msg->port,
                0, 0);
    }
#else /* LWIP_CHECKSUM_ON_COPY */
    if (ip_addr_isany(&msg->addr)) {
        msg->err = udp_send(conn->pcb.udp, p);
    } else {
        msg->err = udp_sendto(conn->pcb.udp, p, 
                &msg->addr, msg->port);
    }
#endif /* LWIP_CHECKSUM_ON_COPY */

    pbuf_free(p);
    sys_sem_signal(conn->op_completed);
}
Exemplo n.º 14
0
void SendMessage(int id, unsigned int msg_id, void* data, unsigned int len)
{
  if(data)memcpy(&tx_udp_msg[id].data[0],data,len);
  
  tx_udp_msg[id].msg_id  = msg_id;
  tx_udp_msg[id].msg_len = len;
  tx_udp_msg[id].msg_crc = crc32((uint8_t*)&tx_udp_msg[id]+4,(UDP_HEADER_SIZE-4) + tx_udp_msg[id].msg_len,UDP_MESSAGE_SIZE); 
  
  pbuf_free(pout[id]);
  pout[id] = pbuf_alloc(PBUF_TRANSPORT,tx_udp_msg_size,PBUF_RAM);
  
  pout[id]->tot_len = len + UDP_HEADER_SIZE;
     
  if(pbuf_take(pout[id],(uint8_t*)&tx_udp_msg[id],tx_udp_msg[id].msg_len + UDP_HEADER_SIZE) == ERR_OK)
  {
    struct ip_addr tx;
    IP4_ADDR(&tx,192,9,206,204);
    pudp_pcb[id]->remote_ip = tx;
    pudp_pcb[id]->remote_port = 456;
    
     if(udp_sendto(pudp_pcb[id],pout[id],&eth_ini_dat.addr[id],eth_ini_dat.UDP_TX_PORT[id]) != ERR_OK) 
     {
        rew_status[id] = 2;
     }
  }
  else rew_status[id] = 1;
}
Exemplo n.º 15
0
/* Transmit a packet on the General Port. */
size_t netSendGeneral(Octet *buf, UInteger16 length, NetPath *netPath)
{
    int i, j;
    struct pbuf *pcopy;

    /* Reallocate the tx pbuf based on the current size. */
    pbuf_realloc(netPath->generalTxBuf, length);
    pcopy = netPath->generalTxBuf;

    /* Copy the incoming data into the pbuf payload. */
    j = 0;
    for(i = 0; i < length; i++) {
        ((u8_t *)pcopy->payload)[j++] = buf[i];
        if(j == pcopy->len) {
            pcopy = pcopy->next;
            j = 0;
        }
    }

    /* send the buffer. */
    udp_sendto(netPath->eventPcb, netPath->generalTxBuf,
               (void *)&netPath->multicastAddr, PTP_GENERAL_PORT);

    return(length);
}
Exemplo n.º 16
0
static void recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
                 ip_addr_t *addr, u16_t port)
{
    pbuf_free(p);

    LWIP_DEBUGF(SIMPLE_DISCOVERY_DEBUG | LWIP_DBG_STATE,
                ("simple discovery from "));
    ip_addr_debug_print(SIMPLE_DISCOVERY_DEBUG | LWIP_DBG_STATE, addr);
    LWIP_DEBUGF(SIMPLE_DISCOVERY_DEBUG | LWIP_DBG_STATE, ("\n"));

    struct netif *netif = (struct netif *) arg;
    int hostlen = strlen(netif->hostname) + 1;

    struct pbuf *sendp = pbuf_alloc(PBUF_TRANSPORT,
                                    sizeof(struct response) + hostlen,
                                    PBUF_RAM);

    struct response *resp = (struct response *) sendp->payload;
    resp->addr = netif->ip_addr;
    memcpy(resp->hwaddr, netif->hwaddr, sizeof(resp->hwaddr));
    memcpy(resp->hostname, netif->hostname, hostlen);

    udp_sendto(upcb, sendp, addr, port);
    pbuf_free(sendp);
}
Exemplo n.º 17
0
/**
 * Send some data on a RAW or UDP pcb contained in a netconn
 * Called from netconn_send
 *
 * @param msg the api_msg_msg pointing to the connection
 */
void
do_send(struct api_msg_msg *msg)
{
  if (!ERR_IS_FATAL(msg->conn->err)) {
    if (msg->conn->pcb.tcp != NULL) {
      switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
      case NETCONN_RAW:
        if (msg->msg.b->addr == NULL) {
          msg->conn->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
        } else {
          msg->conn->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
        }
        break;
#endif
#if LWIP_UDP
      case NETCONN_UDP:
        if (msg->msg.b->addr == NULL) {
          msg->conn->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
        } else {
          msg->conn->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
        }
        break;
#endif /* LWIP_UDP */
      default:
        break;
      }
    }
  }
  TCPIP_APIMSG_ACK(msg);
}
Exemplo n.º 18
0
/**
 * Sends an generic or enterprise specific trap message.
 *
 * @param generic_trap is the trap code
 * @param eoid points to enterprise object identifier
 * @param specific_trap used for enterprise traps when generic_trap == 6
 * @return ERR_OK when success, ERR_MEM if we're out of memory
 *
 * @note the caller is responsible for filling in outvb in the trap_msg
 * @note the use of the enterpise identifier field
 * is per RFC1215.
 * Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps
 * and .iso.org.dod.internet.private.enterprises.yourenterprise
 * (sysObjectID) for specific traps.
 */
err_t
snmp_send_trap(s8_t generic_trap, struct snmp_obj_id *eoid, s32_t specific_trap)
{
	struct snmp_trap_dst *td;
	struct netif *dst_if;
	ip_addr_t dst_ip;
	struct pbuf *p;
	u16_t i,tot_len;

	for (i=0, td = &trap_dst[0]; i<SNMP_TRAP_DESTINATIONS; i++, td++) {
		if ((td->enable != 0) && !ip_addr_isany(&td->dip)) {
			/* network order trap destination */
			ip_addr_copy(trap_msg.dip, td->dip);
			/* lookup current source address for this dst */
			dst_if = ip_route(&td->dip);
			ip_addr_copy(dst_ip, dst_if->ip_addr);
			/* @todo: what about IPv6? */
			trap_msg.sip_raw[0] = ip4_addr1(&dst_ip);
			trap_msg.sip_raw[1] = ip4_addr2(&dst_ip);
			trap_msg.sip_raw[2] = ip4_addr3(&dst_ip);
			trap_msg.sip_raw[3] = ip4_addr4(&dst_ip);
			trap_msg.gen_trap = generic_trap;
			trap_msg.spc_trap = specific_trap;
			if (generic_trap == SNMP_GENTRAP_ENTERPRISESPC) {
				/* enterprise-Specific trap */
				trap_msg.enterprise = eoid;
			} else {
				/* generic (MIB-II) trap */
				snmp_get_snmpgrpid_ptr(&trap_msg.enterprise);
			}
			snmp_get_sysuptime(&trap_msg.ts);

			/* pass 0, calculate length fields */
			tot_len = snmp_varbind_list_sum(&trap_msg.outvb);
			tot_len = snmp_trap_header_sum(&trap_msg, tot_len);

			/* allocate pbuf(s) */
			p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL);
			if (p != NULL) {
				u16_t ofs;

				/* pass 1, encode packet ino the pbuf(s) */
				ofs = snmp_trap_header_enc(&trap_msg, p);
				snmp_varbind_list_enc(&trap_msg.outvb, p, ofs);

				snmp_inc_snmpouttraps();
				snmp_inc_snmpoutpkts();

				/** send to the TRAP destination */
				udp_sendto(trap_msg.pcb, p, &trap_msg.dip, SNMP_TRAP_PORT);

				pbuf_free(p);
			} else {
				return ERR_MEM;
			}
		}
	}
	return ERR_OK;
}
Exemplo n.º 19
0
/* Callback functions... */
int echo_handler(int event, udp_Socket * s, ll_Gather * g, _udp_datagram_info * udi)
{
	// This is explained in "samples\udp\udp_echo_dh.c"
	if (event != UDP_DH_INDATA)
		return 1;
	udp_sendto(s, g->data2, g->len2, udi->remip, udi->remport);
	return 1;
}
Exemplo n.º 20
0
static void resend_data(struct udp_pcb *upcb, ip_addr_t *addr, u16_t port,
                        struct tftp_state *ts)
{
    struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, ts->last_data->len, PBUF_RAM);
    pbuf_copy(p, ts->last_data);
    udp_sendto(upcb, p, addr, port);
    pbuf_free(p);
}
Exemplo n.º 21
0
static ssize_t dns_udp_send_reply(int sock, char *buf, ssize_t len,
		struct sockaddr *dst_addr, socklen_t addrlen)
{
	/*
	 * Ignore the first two bytes. The buffer contains the TCP DNS request and
	 * must not use the first two bytes used for length.
	 */
	return udp_sendto(sock, buf + 2, len, dst_addr, addrlen);
}
Exemplo n.º 22
0
static VOID _DHCPOfferGenAndSend(PDHCP_CLIENT pClient, INT8U * pClientMacAddr, INT32U Xid)
{
	INT32U Len;
	INT8U * Body;
	PDHCP_MSG pDhcpMsg;
	struct pbuf * pDhcpBuf;

	pDhcpBuf = pbuf_alloc(PBUF_TRANSPORT, sizeof(DHCP_MSG), PBUF_RAM);
	if(pDhcpBuf == NULL)
	{
		return;
	}
	
	pDhcpMsg = &DhcpMsg;
	memset(pDhcpMsg, 0, sizeof(*pDhcpMsg));

	/* Initialize the DHCP message header. */
	pDhcpMsg->Op = DHCP_OP_REPLY;
	pDhcpMsg->HType = DHCP_HWTYPE_ETHERNET;
	pDhcpMsg->HLen = 6;
	pDhcpMsg->Xid = htonl(Xid);
	pDhcpMsg->Yiaddr = pClient->IpAddr.addr;
	pDhcpMsg->Siaddr = 0;
	NST_MOVE_MEM(pDhcpMsg->Chaddr, pClientMacAddr, 6);
	pDhcpMsg->Magic = htonl(DHCP_MAGIC);

	Len = 240;
	Body = &pDhcpMsg->Options[0];

	/* Set the message type. */
	DHCP_SET_OPTION_MSG_TYPE(Body, DHCP_MSG_OFFER, Len);

	/* Set the lease time. */
	DHCP_SET_OPTION_LEASE_TIME(Body, DHCP_DEFAULT_LEASE_TIME, Len);

	/* Set the server's ip address */
	DHCP_SET_OPTION_SERVER_ID(Body, DhcpServer.ServerIpAddr.addr, Len);

	/* Set the subnet mask. */
	DHCP_SET_OPTION_SUBNET_MASK(Body, DhcpServer.SubnetMask.addr, Len);

	/* Set the default gatway's ip address. */
	DHCP_SET_OPTION_GW(Body, DhcpServer.GateWay.addr, Len);

	/* Set the dns server's ip address. */
	DHCP_SET_OPTION_DNS(Body, DhcpServer.Dns1.addr, Len);

	DHCP_SET_OPTION_END(Body, Len);

	pbuf_take(pDhcpBuf, (const VOID *)pDhcpMsg, Len);
	pbuf_realloc(pDhcpBuf, Len);

	/* Send broadcast to the DHCP client. */
	udp_sendto(DhcpServer.Socket, pDhcpBuf, IP_ADDR_BROADCAST, DHCP_CLIENT_UDP_PORT);
	pbuf_free(pDhcpBuf);
}
Exemplo n.º 23
0
static ssize_t netSend(const octet_t *buf, int16_t  length, TimeInternal *time, const int32_t * addr, struct udp_pcb * pcb)
{
	err_t result;
	struct pbuf * p;

	/* Allocate the tx pbuf based on the current size. */
	p = pbuf_alloc(PBUF_TRANSPORT, length, PBUF_RAM);
	if (NULL == p)
	{
		ERROR("netSend: Failed to allocate Tx Buffer\n");
		goto fail01;
	}

	/* Copy the incoming data into the pbuf payload. */
	result = pbuf_take(p, buf, length);
	if (ERR_OK != result)
	{
		ERROR("netSend: Failed to copy data to Pbuf (%d)\n", result);
		goto fail02;
	}

	/* send the buffer. */
	result = udp_sendto(pcb, p, (void *)addr, pcb->local_port);
	if (ERR_OK != result)
	{
		ERROR("netSend: Failed to send data (%d)\n", result);
		goto fail02;
	}

	if (time != NULL)
	{
#if LWIP_PTP
		time->seconds = p->time_sec;
		time->nanoseconds = p->time_nsec;
#else
		/* TODO: use of loopback mode */
		/*
		time->seconds = 0;
		time->nanoseconds = 0;
		*/
		getTime(time);
#endif
		DBGV("netSend: %d sec %d nsec\n", time->seconds, time->nanoseconds);
	} else {
		DBGV("netSend\n");
	}


fail02:
	pbuf_free(p);

fail01:
	return length;

	/*  return (0 == result) ? length : 0; */
}
Exemplo n.º 24
0
void udp_echo_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct
					ip_addr *addr, u16_t port)
{
    if (p != NULL) {
        /* send received packet back to sender */
        udp_sendto(pcb, p, addr, port);
        /* free the pbuf */
        pbuf_free(p);
    }
}
Exemplo n.º 25
0
static void send_ack(struct udp_pcb *upcb, ip_addr_t *addr, u16_t port,
                     int blknum)
{
    struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 4, PBUF_RAM);
    u16_t *payload = (u16_t *) p->payload;
    payload[0] = htons(ACK);
    payload[1] = htons(blknum);
    udp_sendto(upcb, p, addr, port);
    pbuf_free(p);
}
Exemplo n.º 26
0
ssize_t usys_sendto(int *err, uuprocess_t *u, int fd, const void *buf, size_t buflen, int flags, const struct sockaddr *to, socklen_t tolen)
{
    if( u == 0 )
    {
        *err = ENOTSOCK; // TODO correct?
        return -1;
    }

    CHECK_FD(fd);
    struct uufile *f = GETF(fd);

    struct uusocket *us = f->impl;
	(void) us;

    if( tolen < (int)sizeof(struct sockaddr_in) )
    {
        *err = EINVAL;
        return -1;
    }

    if( ! (f->flags & UU_FILE_FLAG_NET))
    {
        *err = ENOTSOCK;
        return -1;
    }

    if( flags )
        SHOW_ERROR( 0, "I don't know this flag %d", flags );

    /*
    struct sockaddr_in *sto = (struct sockaddr_in *)to;

    if( sto->sin_family != PF_INET )
        SHOW_ERROR0( 0, "not inet addr?");
    */

    i4sockaddr tmp_addr;
    errno_t rc = sockaddr_unix2int( &tmp_addr, to, tolen );
    if( rc )
    {
        *err = rc;
        return -1;
    }

    //tmp_addr.port = ntohs(sto->sin_port);
    //NETADDR_TO_IPV4(tmp_addr.addr) = ntohl(sto->sin_addr.s_addr);

    SHOW_FLOW( 8, "flags %x", flags );
    //SHOW_FLOW( 7, "port %d, ip %s", tmp_addr.port, inet_itoa(htonl(NETADDR_TO_IPV4(tmp_addr.addr))) );
    int ret = udp_sendto( us->prot_data, buf, buflen, &tmp_addr);
    if( ret < 0 )
        *err = -ret;

    return *err ? -1 : ret;
}
Exemplo n.º 27
0
void UDP_broadcast(void) 
{ 
    struct udp_pcb *UdpPcb; 
    struct ip_addr ipaddr;
    struct pbuf *p;
    p = pbuf_alloc(PBUF_RAW,sizeof(UDPData),PBUF_RAM);
    printf("\nUDP broadcast\n");
    if(p==NULL)
        return;
    p->payload=(void *)UDPData;   ///给pbuf初始化


    UdpPcb=udp_new();                                     // 建立UDP 通信的控制块(pcb)             
    udp_bind(UdpPcb,IP_ADDR_ANY,8080);                     //  绑定本地IP 地址                       
    IP4_ADDR(&ipaddr,255, 255, 255, 255);
    udp_sendto(UdpPcb,p,&ipaddr,8080);                    // 将收到的数据再发送出去                 //
    udp_sendto(UdpPcb,p,&ipaddr,8080); 
    udp_sendto(UdpPcb,p,&ipaddr,8080); 
    udp_recv(UdpPcb,UDP_Receive,NULL);                  // 设置数据接收时的回调函数               //
} 	 
Exemplo n.º 28
0
void UDP_Receive(void *arg, struct udp_pcb *upcb, struct pbuf *p,struct ip_addr *addr, u16_t port) 
{ 

    struct ip_addr ipaddr=*addr;
    printf("----udp have receive-------\n");
    if(p != NULL){
	app_delay();
        udp_sendto(upcb,p,&ipaddr,port);  /* 将收到的数据再发送出去                   */
        pbuf_free(p);                                           /*  释放缓冲区数据                      */
    } 
} 
Exemplo n.º 29
0
uint32_t coap_transport_write(const coap_port_t    * p_port,
                              const coap_remote_t  * p_remote,
                              const uint8_t        * p_data,
                              uint16_t               datalen)
{

    err_t err = NRF_ERROR_NOT_FOUND;
    uint32_t index;

    NULL_PARAM_CHECK(p_port);
    NULL_PARAM_CHECK(p_remote);
    NULL_PARAM_CHECK(p_data);

	//Search for the corresponding port.
    for (index = 0; index < COAP_PORT_COUNT; index++)
    {
        if (m_port_table[index].port_number == p_port->port_number)
        {
		    //Allocate Buffer to send the data on port.
            struct pbuf * lwip_buffer = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM);

            if (NULL != lwip_buffer)
            {
			    //Make a copy of the data onto the buffer.
                memcpy(lwip_buffer->payload, p_data, datalen);

                COAP_MUTEX_UNLOCK();

				//Send on UDP port.
                err = udp_sendto(m_port_table[index].p_socket,
                                 lwip_buffer,
                                 (ip6_addr_t *)p_remote->addr,
                                 p_remote->port_number);

                COAP_MUTEX_LOCK();


                if (err != ERR_OK)
                {
				    //Free the allocated buffer as send procedure has failed.
                    err = NRF_ERROR_INTERNAL;
                }
                UNUSED_VARIABLE(pbuf_free(lwip_buffer));
            }
            else
            {
			    //Buffer allocation failed, cannot send data.
                err = NRF_ERROR_NO_MEM;
            }
            break;
        }
    }
    return err;
}
Exemplo n.º 30
0
void UdpConnection::sendTo(IPAddress remoteIP, uint16_t remotePort, const char* data, int length)
{
	remotePort = 1000;
	pbuf* p = pbuf_alloc(PBUF_TRANSPORT, length, PBUF_RAM);
	memcpy(p->payload, data, length);
	udp_sendto(udp, p, remoteIP, remotePort);
	pbuf_free(p);

	debugf("UdpConnection sendTo : %s %d %s", remoteIP.toString().c_str(),remotePort, data);

}