Skip to content
Extraits de code Groupes Projets
our_utils.h 1,88 ko
Newer Older
  • Learn to ignore specific revisions
  • /**
    
    Vany Ingenzi's avatar
    Vany Ingenzi a validé
     * @file our_utils.h
    
     * @brief This header contains utils that were coded for the packet sending on inginious
     * @date 2022-03-17
     */
    #ifndef __OUR_UTILS_H_
    #define __OUR_UTILS_H_
    
    #include <arpa/inet.h>
    #include <errno.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h>
    
    
    /* Resolve the resource name to an usable IPv6 address
     * @address: The name to resolve
     * @rval: Where the resulting IPv6 address descriptor should be stored
     * @return: NULL if it succeeded, or a pointer towards
     *          a string describing the error if any.
     *          (const char* means the caller cannot modify or free the return value,
     *           so do not use malloc!)
     */
    const char * real_address(const char *address, struct sockaddr_in6 *rval);
    
    
    /* Creates a socket and initialize it
     * @source_addr: if !NULL, the source address that should be bound to this socket
     * @src_port: if >0, the port on which the socket is listening
     * @dest_addr: if !NULL, the destination address to which the socket should send data
     * @dst_port: if >0, the destination port to which the socket should be connected
     * @return: a file descriptor number representing the socket,
     *         or -1 in case of error (explanation will be printed on stderr)
     */
    int create_socket(struct sockaddr_in6 *source_addr, int src_port, struct sockaddr_in6 *dest_addr, int dst_port);
    
    
    /* Block the caller until a message is received on sfd,
     * and connect the socket to the source addresse of the received message
     * @sfd: a file descriptor to a bound socket but not yet connected
     * @return: 0 in case of success, -1 otherwise
    
     * @POST: This call is idempotent, it does not 'consume' the data of the message,
     * and could be repeated several times blocking only at the first call.
    
    int wait_for_client(int sfd);