Subversion Repositories planix.SVN

Rev

Blame | Last modification | View Log | RSS feed

/*

Copyright (c) 2019, The PLANIX Project
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#include <stdint.h>


#include "c9.h"
#include "sock.h"

/**
 * Convert a string to a sockaddr struct.
 * The string can contain an ipv4 or ipv6 address, including a port number.
 * @param s address string, the address string MUST not be longer 
 * than 128 characters
 * @param saout output buffer
 * @return 1 on success,  otherwise no success, consult errno!
 */
int sock_strtoaddr(const char * s,struct sockaddr * saout){

        char ips[128],*ps;
        struct in_addr ia;
        int port;

#ifndef WITHOUT_IPV6
        struct in6_addr ia6;
#endif
        int rc;

        /* copy the string */
        strcpy(ips,s);

        /* search for a collon to separate the port */
        ps = strchr(ips,':');
        if (ps != NULL){
                *ps='\0';
                ps++;
        }
        else
                ps="0";

        /* try to parse ip4 address */
        rc = inet_pton(AF_INET,ips,&ia);


        if (rc==1)
        {
                port = atoi(ps);
                if (port > 65535)
                        rc=0;
        }



        if (rc==1){
                /* it's an ipv4 address */
                struct sockaddr_in * sa;
                sa = (struct sockaddr_in*)saout;
                memset(sa,0,sizeof(struct sockaddr_in));
#ifdef HAVE_SIN_LEN
                sa->sin_len=sizeof(struct sockaddr_in);
#endif
                sa->sin_family = AF_INET;
                sa->sin_addr=ia;
                sa->sin_port=htons(port);
        }

#ifndef WITHOUT_IPV6
        if (rc==0){
                strcpy(ips,s);

                ps = strchr(ips,'.');
                if (ps != NULL){
                        *ps='\0';
                        ps++;
                }
                else
                        ps="0";


                rc = inet_pton(AF_INET6,s,&ia6);


                if (rc==1){
                        /* it's an ipv6 address */
                        struct sockaddr_in6 * sa;
                        sa = (struct sockaddr_in6*)saout;
                        memset(sa,0,sizeof(struct sockaddr_in6));

#ifdef HAVE_SIN_LEN
                        sa->sin6_len=sizeof(struct sockaddr_in6);
#endif
                        sa->sin6_family = AF_INET6;
                        sa->sin6_addr=ia6;
                        sa->sin6_port=htons(atoi(ps));


                }

        }
#endif

        if (rc!=1){
                if (rc!=-1)
                        errno=EINVAL;
        }

        return rc;

}

struct sockdata {
        int fd;
};


int 
end_fun(C9ctx *ctx)
{
        printf("The end sender is called\n");   
//      n = send (fd, pkt, l,0);
        return 0;       
}


uint8_t *
begin_fun( C9ctx *ctx, uint32_t size ){
        printf ("The begin fun\n");
        return malloc(size);
}

C9ctx * 
sock_connect(const char *addrstr)
{

        C9ctx * ctx = NULL;
        struct sockdata  * priv = NULL;
        struct sockaddr_storage sa;
        int rc;
        
        ctx = malloc (sizeof (C9ctx));
        if (ctx == NULL)
                goto errX;

        priv = malloc (sizeof(struct sockdata));
        if (priv==NULL)
                goto errX;

        if ( sock_strtoaddr(addrstr,(struct sockaddr*)&sa) != 1){
                goto errX;
        };
        
        priv->fd = socket(sa.ss_family, SOCK_STREAM,0);
        if (priv->fd == -1){
                goto errX;
        }

        printf("Socket is %d\n",priv->fd);

        rc = connect (priv->fd, (struct sockaddr*)&sa, sizeof(struct sockaddr));

        printf ("Crc = %d\n",rc);

        if (rc==-1){
                goto errX;
        }

        ctx->aux = priv;
        ctx->end = end_fun;
        ctx->begin = begin_fun;
        return ctx;     
errX:
        free(priv);
        free(ctx);
        return NULL;
}