/**
 * gst_rtsp_address_pool_add_range:
 * @pool: a #GstRTSPAddressPool
 * @min_address: a minimum address to add
 * @max_address: a maximum address to add
 * @min_port: the minimum port
 * @max_port: the maximum port
 * @ttl: a TTL or 0 for unicast addresses
 *
 * Adds the addresses from @min_addess to @max_address (inclusive)
 * to @pool. The valid port range for the addresses will be from @min_port to
 * @max_port inclusive.
 *
 * When @ttl is 0, @min_address and @max_address should be unicast addresses.
 * @min_address and @max_address can be set to
 * #GST_RTSP_ADDRESS_POOL_ANY_IPV4 or #GST_RTSP_ADDRESS_POOL_ANY_IPV6 to bind
 * to all available IPv4 or IPv6 addresses.
 *
 * When @ttl > 0, @min_address and @max_address should be multicast addresses.
 *
 * Returns: %TRUE if the addresses could be added.
 */
gboolean
gst_rtsp_address_pool_add_range (GstRTSPAddressPool * pool,
                                 const gchar * min_address, const gchar * max_address,
                                 guint16 min_port, guint16 max_port, guint8 ttl)
{
    AddrRange *range;
    GstRTSPAddressPoolPrivate *priv;
    gboolean is_multicast;

    g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool), FALSE);
    g_return_val_if_fail (min_port <= max_port, FALSE);

    priv = pool->priv;

    is_multicast = ttl != 0;

    range = g_slice_new0 (AddrRange);

    if (!fill_address (min_address, min_port, &range->min, is_multicast))
        goto invalid;
    if (!fill_address (max_address, max_port, &range->max, is_multicast))
        goto invalid;

    if (range->min.size != range->max.size)
        goto invalid;
    if (memcmp (range->min.bytes, range->max.bytes, range->min.size) > 0)
        goto invalid;

    range->ttl = ttl;

    GST_DEBUG_OBJECT (pool, "adding %s-%s:%u-%u ttl %u", min_address, max_address,
                      min_port, max_port, ttl);

    g_mutex_lock (&priv->lock);
    priv->addresses = g_list_prepend (priv->addresses, range);

    if (!is_multicast)
        priv->has_unicast_addresses = TRUE;
    g_mutex_unlock (&priv->lock);

    return TRUE;

    /* ERRORS */
invalid:
    {
        GST_ERROR_OBJECT (pool, "invalid address range %s-%s", min_address,
                          max_address);
        g_slice_free (AddrRange, range);
        return FALSE;
    }
}
internet_socket_listener_ptr internet_socket_listener::create(
    const std::string& host, const uint16_t& port)
{
    internet_socket_listener_ptr result;
    const int32_t socket_fd = socket(PF_INET, SOCK_STREAM, 0);
    // only listen if a valid socket file descriptor was created
    if (socket_fd >= 0) {

        // This is an accepted exceptional use of an union (breaks MISRA
        // C++:2008 Rule 9-5-1). Alternatively a reinterpret_cast could be used,
        // but anyway there must be a way to fulfill the BSD socket interface.
        union {
            sockaddr base;
            sockaddr_in in;
        } addr;

        addr.in = fill_address(host, port);
        if (addr.in.sin_family != AF_UNSPEC) {
            const int32_t result1 =
                bind(socket_fd, &addr.base, sizeof(addr.in));
            const int32_t result2 = ::listen(socket_fd, 4);
            // return a valid object only if bind and listen does not return an
            // error
            if ((result1 != -1) && (result2 != -1)) {
                result = std::make_shared<internet_socket_listener>(socket_fd);
            }
        }
    }

    return result;
}
void create_child(int prev_socket_fd,struct sockaddr *client_address, request *client_req)
{
    pid_t pid=fork();
    if(pid<0)
        err("Error in creating child");
    else if(pid == 0)
    {
        close(prev_socket_fd);

        int socket_fd,addr_len=sizeof(struct sockaddr);
        struct sockaddr_in child_address;

        socket_fd=create_socket();
        fill_address(&child_address,0,NULL);
        bind_socket(&child_address,socket_fd);

        if (getsockname(socket_fd, (struct sockaddr *)&child_address, (socklen_t *)&addr_len) == -1)
            err("Error in resolving child address");

        printf("Child process created.Listening on port %d\n\n",ntohs(child_address.sin_port));

        ack_data *ack=(ack_data *)malloc(sizeof(ack_data));
        memset((void *)ack,0,sizeof(ack_data));

        if(!existence(client_req->filename))
        {
            perror("File does not exists");
            printf("File name is %s\n",client_req->filename );
            file_data *fdata=(file_data *)malloc(sizeof(file_data));
            fdata->data_block=-1;
            ssize_t nbytes=sendto(socket_fd,fdata,sizeof(file_data),0,client_address,(socklen_t )addr_len);

            exit(-1);
        }

        data_send(socket_fd,client_address,addr_len,client_req->filename);

        close(socket_fd);
        exit(0);
    }
}
void start_server(int port)
{
    signal(SIGCHLD,sigchld_handler);

    int client_length,socket_fd;
    struct sockaddr_in server_address,client_address;
    char *buf;
    ssize_t nbytes;

    socket_fd=create_socket();
    fill_address(&server_address,port,NULL);
    bind_socket(&server_address,socket_fd);

    printf("Server listening on port %i \n",ntohs(server_address.sin_port));

    while(1)
    {
        buf=(char *)malloc(sizeof(request));
        memset(buf,0,sizeof(request));
        client_length=sizeof(client_address);

        nbytes=recvfrom(socket_fd,buf,BUFFLEN,0,(struct sockaddr *)&client_address,&client_length);
        if(nbytes<0)
            err("Error in receiving request from client\n");
//        receive_message(socket_fd,buf,&client_address);

        fprintf(stderr,"\n\nReceived packet from client %s:%d\n",inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
        fflush(stdin);
        request *client_req=(request *)malloc(sizeof(request));
        decompress(client_req,buf);

        create_child(socket_fd,(struct sockaddr *)&client_address, client_req);
    }

    close(socket_fd);
}
/**
 * gst_rtsp_address_pool_reserve_address:
 * @pool: a #GstRTSPAddressPool
 * @ip_address: The IP address to reserve
 * @port: The first port to reserve
 * @n_ports: The number of ports
 * @ttl: The requested ttl
 * @address: (out): storage for a #GstRTSPAddress
 *
 * Take a specific address and ports from @pool. @n_ports consecutive
 * ports will be allocated of which the first one can be found in
 * @port.
 *
 * If @ttl is 0, @address should be a unicast address. If @ttl > 0, @address
 * should be a valid multicast address.
 *
 * Returns: #GST_RTSP_ADDRESS_POOL_OK if an address was reserved. The address
 * is returned in @address and should be freed with gst_rtsp_address_free
 * after use.
 */
GstRTSPAddressPoolResult
gst_rtsp_address_pool_reserve_address (GstRTSPAddressPool * pool,
                                       const gchar * ip_address, guint port, guint n_ports, guint ttl,
                                       GstRTSPAddress ** address)
{
    GstRTSPAddressPoolPrivate *priv;
    Addr input_addr;
    GList *list;
    AddrRange *addr_range;
    GstRTSPAddress *addr;
    gboolean is_multicast;
    GstRTSPAddressPoolResult result;

    g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool),
                          GST_RTSP_ADDRESS_POOL_EINVAL);
    g_return_val_if_fail (ip_address != NULL, GST_RTSP_ADDRESS_POOL_EINVAL);
    g_return_val_if_fail (port > 0, GST_RTSP_ADDRESS_POOL_EINVAL);
    g_return_val_if_fail (n_ports > 0, GST_RTSP_ADDRESS_POOL_EINVAL);
    g_return_val_if_fail (address != NULL, GST_RTSP_ADDRESS_POOL_EINVAL);

    priv = pool->priv;
    addr_range = NULL;
    addr = NULL;
    is_multicast = ttl != 0;

    if (!fill_address (ip_address, port, &input_addr, is_multicast))
        goto invalid;

    g_mutex_lock (&priv->lock);
    list = find_address_in_ranges (priv->addresses, &input_addr, port, n_ports,
                                   ttl);
    if (list != NULL) {
        AddrRange *range = list->data;
        guint skip_port, skip_addr;

        skip_addr = diff_address (&input_addr, &range->min);
        skip_port = port - range->min.port;

        GST_DEBUG_OBJECT (pool, "diff 0x%08x/%u", skip_addr, skip_port);

        /* we found a range, remove from the list */
        priv->addresses = g_list_delete_link (priv->addresses, list);
        /* now split and exit our loop */
        addr_range = split_range (pool, range, skip_addr, skip_port, n_ports);
        priv->allocated = g_list_prepend (priv->allocated, addr_range);
    }

    if (addr_range) {
        addr = g_slice_new0 (GstRTSPAddress);
        addr->pool = g_object_ref (pool);
        addr->address = get_address_string (&addr_range->min);
        addr->n_ports = n_ports;
        addr->port = addr_range->min.port;
        addr->ttl = addr_range->ttl;
        addr->priv = addr_range;

        result = GST_RTSP_ADDRESS_POOL_OK;
        GST_DEBUG_OBJECT (pool, "reserved address %s:%u ttl %u", addr->address,
                          addr->port, addr->ttl);
    } else {
        /* We failed to reserve the address. Check if it was because the address
         * was already in use or if it wasn't in the pool to begin with */
        list = find_address_in_ranges (priv->allocated, &input_addr, port, n_ports,
                                       ttl);
        if (list != NULL) {
            result = GST_RTSP_ADDRESS_POOL_ERESERVED;
        } else {
            result = GST_RTSP_ADDRESS_POOL_ERANGE;
        }
    }
    g_mutex_unlock (&priv->lock);

    *address = addr;
    return result;

    /* ERRORS */
invalid:
    {
        GST_ERROR_OBJECT (pool, "invalid address %s:%u/%u/%u", ip_address,
                          port, n_ports, ttl);
        *address = NULL;
        return GST_RTSP_ADDRESS_POOL_EINVAL;
    }
}
Esempio n. 6
0
/*
 * map_wellknown - map known devices & registers
 */
static void
map_wellknown(pnode_t curnode)
{
    extern int status_okay(int, char *, int);
    char tmp_name[MAXSYSNAME];
    int sok;

#ifdef VPRINTF
    VPRINTF("map_wellknown(%x)\n", curnode);
#endif /* VPRINTF */

    for (curnode = CHILD(curnode); curnode; curnode = NEXT(curnode)) {
        /*
         * prune subtree if status property indicating not okay
         */
        sok = status_okay((int)curnode, (char *)NULL, 0);
        if (!sok) {
            char devtype_buf[OBP_MAXPROPNAME];
            int size;

#ifdef VPRINTF
            VPRINTF("map_wellknown: !okay status property\n");
#endif /* VPRINTF */
            /*
             * a status property indicating bad memory will be
             * associated with a node which has a "device_type"
             * property with a value of "memory-controller"
             */
            if ((size = GETPROPLEN(curnode,
                                   OBP_DEVICETYPE)) == -1)
                continue;
            if (size > OBP_MAXPROPNAME) {
                cmn_err(CE_CONT, "node %x '%s' prop too "
                        "big\n", curnode, OBP_DEVICETYPE);
                continue;
            }
            if (GETPROP(curnode, OBP_DEVICETYPE,
                        devtype_buf) == -1) {
                cmn_err(CE_CONT, "node %x '%s' get failed\n",
                        curnode, OBP_DEVICETYPE);
                continue;
            }
            if (strcmp(devtype_buf, "memory-controller") != 0)
                continue;
            /*
             * ...else fall thru and process the node...
             */
        }
        bzero(tmp_name, MAXSYSNAME);
        if (GETPROP(curnode, OBP_NAME, (caddr_t)tmp_name) != -1)
            fill_address(curnode, tmp_name);
        if (GETPROP(curnode, OBP_DEVICETYPE, tmp_name) != -1 &&
                strcmp(tmp_name, "cpu") == 0) {
            fill_cpu(curnode);
        }
        if (strcmp(tmp_name, "tod") == 0)
            have_tod(curnode);
        if (sok && (strcmp(tmp_name, "memory-controller") == 0) &&
                (&plat_fill_mc != NULL))
            plat_fill_mc(curnode);
        map_wellknown(curnode);
    }
}