Пример #1
0
MONO_API int
monodroid_getifaddrs (m_ifaddrs **ifap)
{
	char buff[1024];
	FILE * f = fopen ("/proc/net/route", "r");
	if (f) {
		int i = 0;
		fgets (buff, 1023, f);
		fgets (buff, 1023, f);
		while (!isspace (buff [i]) && i < 1024)
			++i;
		buff [i] = 0;
		fclose (f);
	} else {
		strcpy (buff, "wlan0");
	}

	m_ifaddrs *res = calloc (1, sizeof (m_ifaddrs));
	memset (res, 0, sizeof (res));

	res->ifa_next = NULL;
	res->ifa_name = m_strdup_printf ("%s", buff);
	res->ifa_flags = 0;
	res->ifa_ifu.ifu_dstaddr = NULL;
	init_sock_addr (&res->ifa_addr, "192.168.0.1");
	init_sock_addr (&res->ifa_netmask, "255.255.255.0");

	*ifap = res;
	return 0;
}
Пример #2
0
int main(int argc, char * argv[]){

	//first, check the number of argument
    if (argc != 2){
        printf("Usage: serveur.exe <port number>\n");
        exit(1);
    }

    //trap SIGINT
    signal(SIGINT,SIGINT_handler);

    //variable definition
    int sock_Data, addr_len,pid, seq, cwnd,normal_wnd, advanced_wnd;
    int test =-1;
    int SEG_SIZE,size,inc,descriptorToCheck[2];
    int reuse = 1;
    int ctrlBuff_SIZE = 12;
    int isSYN=0;
    int seq_advanced, seq_expected,checkBreak=0;

    char SYN_ACK[12];
    char FIN[4]="FIN";
    char ctrlBuffer[ctrlBuff_SIZE];
    char * dataBuffer;

    FILE * file;
    DATA DATA = NULL;
    struct timeval ZERO = {0, 0};


    // struct sockaddr_in
    SOCKADDR_IN * my_addr_UDP;//Adress for listening UDP
    SOCKADDR_IN * my_client_addr;//Adress client, for control
    SOCKADDR_IN * data_addr;//Adress client for Data

    int port_nb = atoi(argv[1]);
    int sock_Control= mySocket(AF_INET,SOCK_DGRAM,0); //socketUDP

	//display the descriptor value and pid of server
	printf("Id Socket: %i\n",sock_Control);
	printf("PID father: %i\n",getpid());

	//allow to reuse immediately the socket
	setsockopt( sock_Control, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));

	// structure sockaddr_in initialization
    my_addr_UDP = conf_sock_addr(AF_INET, port_nb, "0.0.0.0");
	my_client_addr = init_sock_addr();
	size = sizeof(*my_client_addr);

	//link the socket to the structure
	myBind(sock_Control,my_addr_UDP,sizeof(*my_addr_UDP));

	descriptorToCheck[0] = sock_Control;

	//init fd set
	fd_set * my_read_set = init_fd_set();

	//loop to be a daemon
	while (isSIGINT==0){

		//set the fd_set
		set_fd_set(my_read_set,descriptorToCheck[0]);
		select(sock_Control+1,my_read_set,NULL,NULL,&ZERO);

		if ( FD_ISSET(sock_Control, my_read_set) ){
			// message reception
			myReceiveFrom(sock_Control, ctrlBuffer, ctrlBuff_SIZE,my_client_addr,&size);
            printf("Reception UDP by %d: %s\n",getpid(),ctrlBuffer);

            // if message of type SYN
            if ( check_SYN(ctrlBuffer) == 1){
                port_nb= (port_nb+1)%8975+1025 ;
                sprintf(SYN_ACK,"SYN-ACK%04d",port_nb);

                //new socket for receiving data
                sock_Data = mySocket(AF_INET,SOCK_DGRAM,0);
                data_addr = conf_sock_addr(AF_INET, port_nb, "0.0.0.0");
                addr_len = sizeof(* data_addr);
                descriptorToCheck[1]=sock_Data;

                //binding sock on addr client
                myBind(sock_Data,data_addr,addr_len);
                printf("Connexion Asked by client, dialoguing on port %d \n", ntohs(data_addr->sin_port));
                printf("Adresse client %s\n",inet_ntoa(my_client_addr->sin_addr));
                data_addr = conf_sock_addr(AF_INET, port_nb, inet_ntoa(my_client_addr->sin_addr));

                // variable for receiving
                SEG_SIZE= getMTU(data_addr)-28; // (MTU - header UDP + header IP)
                SEG_SIZE=(SEG_SIZE> 5000)?5000:SEG_SIZE;
                dataBuffer = (char *)malloc(SEG_SIZE*sizeof(char));


                isSYN=1;
                mySendTo(sock_Control, SYN_ACK,12,my_client_addr);
            }

            if ( check_ACK(ctrlBuffer) == 1 && isSYN ==1){
                printf("Connexion Accept\n");
                isSYN = 0;
                //a little fork
                pid = fork();

                //if error
                if (pid == -1){
                    perror("Fork Error\n");
                    exit(-1);
                }

                /*======================================================================================*/
                /*===================================== child code =====================================*/
                /*======================================================================================*/
				else if (pid == 0){
                    myClose(sock_Control);
                    // reception of file name
                    myReceiveFrom(sock_Data,dataBuffer, SEG_SIZE,data_addr,&addr_len);
                    printf("Client Asking for file %s\n",dataBuffer);
                    //retrieve file name
                    file=myOpen(dataBuffer,"r");

                    seq=1, seq_advanced=1, cwnd =400,normal_wnd=320, advanced_wnd=cwnd-normal_wnd;

                    //while something to send, send and wait for ack
                    do {

                        checkBreak=0;
                        seq_expected=(seq == seq_advanced)?seq+cwnd:seq_advanced+advanced_wnd;
                        printf("seq :%d, seq_expected %d, seq_advanced %d\n",seq, seq_expected, seq_advanced);

                        //construct DATA to send from file
                        if (test == -1) test= readFileToBUFFER(file,&DATA,seq,(seq+cwnd==seq_expected)?cwnd:seq_advanced-seq+advanced_wnd,SEG_SIZE);
                        else readFileToBUFFER(file,&DATA,seq,test-seq,SEG_SIZE);

                        //send dataBuffer to client
                        if(seq+normal_wnd==seq_expected || seq == 1) {
                            sendData(sock_Data, cwnd, DATA, data_addr);
                        }else{
                            printf("fast retransmit, resend sequence presumed lost\n");
                            sendData(sock_Data, normal_wnd, DATA, data_addr);
                            printf("fast retransmit, send selected data\n");
                            sendSelectedData(sock_Data,seq_advanced,advanced_wnd,DATA,data_addr);
                        }

                        //while there is something to read
                        set_fd_set(my_read_set, descriptorToCheck[1]);
                        select(sock_Data + 1, my_read_set, NULL, NULL, &ZERO);
                        while (FD_ISSET(sock_Data,my_read_set) ) {


                            //reception
                            myReceiveFrom(sock_Data, ctrlBuffer, ctrlBuff_SIZE, data_addr, &addr_len);
                            //number of sequences acknowledged, (-1 si duplicate ACK)
                            inc = check_ACK_Seq(seq,cwnd,ctrlBuffer);

                            if (inc == -1) // if duplicate ACK
                            {
                                printf("ACK Duplicate ");
                                sendSelectedData(sock_Data,seq,1,DATA,data_addr);
                                if (checkBreak==0){
                                    usleep(100000);
                                    checkBreak=1;
                                }

                            }
                            else if (inc > 0){// if good ACK
                                checkBreak=0;
                                seq += inc;
                                if (seq==seq_expected)break;
                            }

                            //set the descriptor to check
                            set_fd_set(my_read_set, descriptorToCheck[1]);
                            select(sock_Data + 1, my_read_set, NULL, NULL, &ZERO);
                        }


                        if ( test == -1)
                            seq_advanced=(seq_advanced+advanced_wnd > seq+2000 || seq_advanced < seq+normal_wnd)?seq+normal_wnd:seq_advanced+advanced_wnd;
                        else
                            seq_advanced=(seq_advanced+advanced_wnd > seq + 2000 || seq_advanced+advanced_wnd> test+1)?seq+normal_wnd:seq_advanced+advanced_wnd;


                    } while(seq != test+1 );

                    printf("client dialoguing on port %d\n",ntohs(data_addr->sin_port));
                    for(reuse=0;reuse<1000000;reuse++)
                       mySendTo(sock_Data, FIN, 4, data_addr); //Send FIN to client

                    //to do a clean exit
                    myClose(sock_Data);
                    free(my_addr_UDP);
                    free(my_client_addr);
                    free(dataBuffer);
                    free_Data(DATA);
                    fclose(file);
					printf("\e[1;3%dmEnd of connexion. Son with pid:%d is closing, communicate on %d\e[0m\n", sock_Data % 8, getpid(),port_nb);
                    exit(1);


				}
                /*======================================================================================*/
                /*==================================== end of child ====================================*/
                /*======================================================================================*/
			}
		}
Пример #3
0
static void main_loop(const char *serverIp, Uint16 port)
{
	int sockTcp = socket_tcp_server(56790, 5);
	assert(sockTcp >= 0);
	int sockUdp = socket_udp_server(56790);
	assert(sockUdp >= 0);

	int err = set_sock_send_timeout(sockTcp, 5);
	err |= set_sock_recv_timeout(sockTcp, 5);
	assert(err == 0);

	err = set_sock_buf_size(sockUdp, 512 * 1024, 512 * 1024);
	err |= set_sock_linger(sockUdp, TRUE, 2);
	err |= set_sock_block(sockUdp, FALSE);
	DBG("err = %d", err);
	assert(err == 0);

	close(sockTcp);
	close(sockUdp);

	sockTcp = socket(AF_INET, SOCK_STREAM, 0);
	struct sockaddr_in addr;
	init_sock_addr(&addr, serverIp, port);

	err = connect_nonblock(sockTcp, (struct sockaddr *)&addr, sizeof(addr), 5000);
	if(err < 0) {
		ERR("Connect nonblocking server failed.");
		goto exit;
	} else {
		DBG("Connect to %s:%u success!", serverIp, port);
	}

	close(sockTcp);
	close(sockUdp);
	sockTcp = sockUdp = -1;
	
	sockTcp = socket(AF_INET, SOCK_STREAM, 0);
	err = connect(sockTcp, (struct sockaddr *)&addr, sizeof(addr));
	if(err < 0) {
		ERR("Connect server failed.");
		goto exit;
	} else {
		DBG("Connect to %s:%u success!", serverIp, port);
	}

	sockUdp = socket(AF_INET, SOCK_DGRAM, 0);
	char *sendLine = "Hello";
	err = sendto(sockUdp, sendLine, strlen(sendLine), 0, (struct sockaddr *)&addr, sizeof(addr));
	if(err < 0)
		ERR("Send to failed.");
	else
		DBG("Sendto ok...");

	char ipBuf[32];

	err = get_local_ip(ipBuf, sizeof(ipBuf));
	assert(err == E_NO);
	DBG("get local ip: %s", ipBuf);

	Uint8 macAddr[6];
	err = sys_get_mac("eth0", macAddr, sizeof(macAddr));
	assert(err == E_NO);
	DBG("get mac addr: %02X-%02X-%02X-%02X-%02X-%02X", 
		macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);

exit:

	if(sockTcp > 0)
		close(sockTcp);
	if(sockUdp > 0)
		close(sockUdp);
	
}