예제 #1
0
/*
 * Sends a message through a locally bound port (the bound port already has an associated
 * receiver address so it is sufficient to just supply the bound port number). In order
 * for the remote system to correctly create a bound port for replies back to the sending
 * system, it needs to know the sender's listening port (specified by local_unbound_port).
 * The msg parameter is a pointer to a data payload that the user wishes to send and does not
 * include a network header; your implementation of minimsg_send must construct the header
 * before calling network_send_pkt(). The return value of this function is the number of
 * data payload bytes sent not inclusive of the header.
 * Return -1 if len is larger then the maximum package size.
 */
int
minimsg_send(miniport_t local_unbound_port, miniport_t local_bound_port,
             minimsg_t msg, int len)
{
    struct mini_header hdr;
    network_address_t dest;
    int sent;

    if (NULL == local_unbound_port || NULL == local_bound_port
            || UNBOUNDED != local_unbound_port->type
            || BOUNDED != local_bound_port->type
            || NULL == msg || len < 0 || len > MINIMSG_MAX_MSG_SIZE)
        return -1;

    minimsg_packhdr(&hdr, local_unbound_port, local_bound_port);
    network_address_copy(local_bound_port->bound.addr, dest);
    sent = miniroute_send_pkt(dest, MINIMSG_HDRSIZE, (char*)&hdr, len, msg);

    return sent - MINIMSG_HDRSIZE < 0 ? -1 : sent - MINIMSG_HDRSIZE;
}
예제 #2
0
파일: minimsg.c 프로젝트: kentalabur/egs
/* Sends a message through a locally bound port (the bound port already has an associated
 * receiver address so it is sufficient to just supply the bound port number). In order
 * for the remote system to correctly create a bound port for replies back to the sending
 * system, it needs to know the sender's listening port (specified by local_unbound_port).
 * The msg parameter is a pointer to a data payload that the user wishes to send and does not
 * include a network header; your implementation of minimsg_send must construct the header
 * before calling miniroute_send_pkt(). The return value of this function is the number of
 * data payload bytes sent not inclusive of the header. Returns -1 on error.
 * Fails if msg is too long. 
 */
int
minimsg_send(miniport_t local_unbound_port, miniport_t local_bound_port, minimsg_t msg, int len) {
  struct mini_header hdr;
  network_address_t dst_addr;
  
  if (len > MINIMSG_MAX_MSG_SIZE) {
    return -1;
  }

  if (local_unbound_port == NULL || 
      local_unbound_port->p_type != UNBOUND_PORT || 
      local_unbound_port->p_num >= BOUND_PORT_START ||
      miniport_array[local_unbound_port->p_num] != local_unbound_port) {
    return -1;
  }

  if (local_bound_port == NULL ||
      local_bound_port->p_type != BOUND_PORT ||
      local_bound_port->p_num < BOUND_PORT_START ||
      miniport_array[local_bound_port->p_num] != local_bound_port) {
    return -1;
  }


  network_address_copy(local_bound_port->u.bound.dest_addr, dst_addr); 
  hdr.protocol = PROTOCOL_MINIDATAGRAM;
  pack_address(hdr.source_address, my_addr);
  pack_unsigned_short(hdr.source_port, local_unbound_port->p_num);
  pack_address(hdr.destination_address, local_bound_port->u.bound.dest_addr);
  pack_unsigned_short(hdr.destination_port, local_bound_port->u.bound.dest_num);
  
  if (miniroute_send_pkt(dst_addr, sizeof(hdr), (char*)&hdr, len, msg)) {
    return -1;
  }
  return len;
}