コード例 #1
0
ファイル: makuosan.c プロジェクト: yasui0906/makuosan
int is_send(mfile *m)
{
  if(!m){
    return(0);
  }
  if(m->mdata.head.nstate == MAKUO_SENDSTATE_WAIT){
    return(0);
  }
  if(m->mdata.head.flags & MAKUO_FLAG_ACK){
    return(1);
  }
  switch(m->mdata.head.opcode){
    case MAKUO_OP_SEND:
    case MAKUO_OP_DEL:
      if(!S_ISLNK(m->fs.st_mode) && S_ISDIR(m->fs.st_mode)){
        if(!mfdirchk(m)){
          return(0);
        }
      }
      break;
  }
  if(!ack_check(m, MAKUO_RECVSTATE_NONE)){
    m->sendwait = 0;
  }
  if(m->sendwait){
    if(!mtimeout(&(m->lastsend), MAKUO_SEND_TIMEOUT)){
      return(0);
    }
    if(!(m->retrycnt)){
      recv_timeout(m);
    }
  }
  return(1);
}
コード例 #2
0
/* This parses a version 2 Proxy protocol header (from haproxy).
 *
 * When called from a UNIX socket (where we don't have any SSL
 * info), we additionally read information about the SSL session.
 * We expect to receive the peer's certificate verification status,
 * and CN. That corresponds to send-proxy-v2-ssl-cn and send-proxy-v2-ssl
 * haproxy config options.
 */
int parse_proxy_proto_header(struct worker_st *ws, int fd)
{
	proxy_hdr_v2 hdr;
	int data_size;
	uint8_t cmd, family, proto;
	uint8_t ver;
	uint8_t *p;
	int ret;

	ret = recv_timeout(fd, &hdr, 16, 3);
	if (ret < 0) {
		oclog(ws, LOG_ERR,
		      "proxy-hdr: recv timed out");
		return -1;
	}

	if (ret < 16) {
		oclog(ws, LOG_ERR, "proxy-hdr: invalid v2 header size");
		return -1;
	}

	if (memcmp(hdr.sig, PROXY_HEADER_V2, PROXY_HEADER_V2_SIZE) != 0) {
		oclog(ws, LOG_ERR, "proxy-hdr: invalid v2 header");
		return -1;
	}

	data_size = ntohs(hdr.len);

	if (data_size > sizeof(hdr.data)) {
		oclog(ws, LOG_ERR, "proxy-hdr: too long v2 header size");
		return -1;
	}

	ret = recv_timeout(fd, hdr.data, data_size, 3);
	if (ret < 0) {
		oclog(ws, LOG_ERR,
		      "proxy-hdr: recv data timed out");
		return -1;
	}

	cmd = hdr.ver_cmd & 0x0f;
	ver = (hdr.ver_cmd & 0xf0) >> 4;
	if (ver != 0x02) {
		oclog(ws, LOG_ERR, "proxy-hdr: unsupported version (%x), skipping message", (unsigned)ver);
		return 0;
	}

	if (cmd != 0x01) {
		if (hdr.family == 0)
			oclog(ws, LOG_DEBUG, "proxy-hdr: received health check command");
		else
			oclog(ws, LOG_ERR, "proxy-hdr: received unsupported command %x", (unsigned)cmd);
		return -1;
	}

	family = (hdr.family & 0xf0) >> 4;
	proto = hdr.family & 0x0f;

	if (family != 0x1 && family != 0x2) {
		oclog(ws, LOG_ERR, "proxy-hdr: received unsupported family %x; skipping header", (unsigned)family);
		return 0;
	}

	if ((proto != 0x1 && proto != 0x0)) {
		oclog(ws, LOG_ERR, "proxy-hdr: received unsupported protocol %x; skipping header", (unsigned)proto);
		return 0;
	}

	p = hdr.data;

	if (family == 0x01) { /* AF_INET */
		struct sockaddr_in *sa = (void*)&ws->remote_addr;

		if (data_size < 12) {
			oclog(ws, LOG_INFO, "proxy-hdr: received not enough IPv4 data");
			return 0;
		}

		memset(&ws->remote_addr, 0, sizeof(ws->remote_addr));
		sa->sin_family = AF_INET;
		memcpy(&sa->sin_port, p+8, 2);
		memcpy(&sa->sin_addr, p, 4);
		ws->remote_addr_len = sizeof(struct sockaddr_in);

		memset(&ws->our_addr, 0, sizeof(ws->our_addr));
		sa = (void*)&ws->our_addr;
		memcpy(&sa->sin_addr, p+4, 4);
		memcpy(&sa->sin_port, p+10, 2);
		ws->our_addr_len = sizeof(struct sockaddr_in);

		p += 12;
		data_size -= 12;
	} else if (family == 0x02) { /* AF_INET6 */
		struct sockaddr_in6 *sa = (void*)&ws->remote_addr;

		if (data_size < 36) {
			oclog(ws, LOG_INFO, "proxy-hdr: received not enough IPv6 data");
			return 0;
		}

		memset(&ws->remote_addr, 0, sizeof(ws->remote_addr));
		sa->sin6_family = AF_INET6;
		sa->sin6_port = 0;
		memcpy(&sa->sin6_addr, p, 16);
		memcpy(&sa->sin6_port, p+32, 2);
		ws->remote_addr_len = sizeof(struct sockaddr_in6);

		memset(&ws->our_addr, 0, sizeof(ws->our_addr));
		sa = (void*)&ws->our_addr;
		memcpy(&sa->sin6_addr, p+16, 16);
		memcpy(&sa->sin6_port, p+34, 2);
		ws->our_addr_len = sizeof(struct sockaddr_in);

		p += 36;
		data_size -= 36;
	}

	/* Find CN if needed */
	if (ws->conn_type == SOCK_TYPE_UNIX && data_size > 0) {
		parse_ssl_tlvs(ws, p, data_size);
	}

	return 0;
}
コード例 #3
0
ファイル: net.c プロジェクト: eldesh/nanonet
int recv_buffer_timeout(socket_t sock, buffer * buff, int flags, struct timeval timeout) {
	int len=recv_timeout(sock, (char*)&buff->buffer[buff->used], buff->size-buff->used, flags, timeout);
	if (0<len)
		buff->used += len;
	return len;
}
コード例 #4
0
ファイル: Stato.cpp プロジェクト: giovannipessiva/shareemall
// Create sockets and thread for control and video messager
// Return false if an error occur and the initialization fails
bool Stato::initialize()
{
	HDC hdc = GetDC(hWnd);
	hdcBuf = CreateCompatibleDC(hdc);
	if(!hdcBuf)
		printError(L"CreateCompatibleDC");
	ReleaseDC(hWnd,hdc);

	bmpBuf=NULL;	// Set this in setArea(), when you have the area size	

	struct sockaddr_in addr;
	s_ctrl=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(s_ctrl==INVALID_SOCKET)
		return false;

	addr.sin_family = AF_INET;
	addr.sin_port = htons(this->port);
	addr.sin_addr.s_addr = htonl(this->ip);

	if(connect(s_ctrl,reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
	{
		MessageBox(hWnd,_T("Impossibile connettersi al server!"),_T("Errore"),MB_OK);
		return false;
	}
		
	// Send the password and your nickname
	sendCtrlMessage(CTRL_PSWD,strlen(pswd)+1,pswd);
	sendCtrlMessage(CTRL_NICK,strlen(user)+1,user);


	char *reply=new char[HEADER_CTRL_DIM];
	if(!recv_timeout(s_ctrl,reply,HEADER_CTRL_DIM,0))
	{
		MessageBox(hWnd,_T("Nessuna risposta dal server!\n(1)"),_T("Errore"),MB_OK);			
		return false;
	}

	int code;
	memcpy(&code,reply,2);
	delete[] reply;

	switch(ntohs(code))
	{
	case CTRL_LOGIN_200:	// Ok
		break;

	case CTRL_LOGIN_401:	// Unauthorized
		MessageBox(hWnd,_T("La password inserita non è corretta."),_T("Errore"),MB_OK);
		return false;

	case CTRL_LOGIN_409:	// Conflict
		MessageBox(hWnd,_T("Lo username scelto è già in uso."),_T("Errore"),MB_OK);
		return false;

	case CTRL_LOGIN_503:	// Service Unavailable
		MessageBox(hWnd,_T("Il server ha raggiunto la capacità massima di client connessi."),_T("Errore"),MB_OK);
		return false;

	default:
		MessageBox(hWnd,_T("Risposta inattesa dal server!"),_T("Errore"),MB_OK);
		return false;
	};

	// Get the new port of the server
	reply=new char[HEADER_CTRL_DIM + 2];
	if(!recv_timeout(s_ctrl,reply,HEADER_CTRL_DIM + 2,0))
	{
		MessageBox(hWnd,_T("Nessuna risposta dal server!\n(2)"),_T("Errore"),MB_OK);		
		return false;
	}

	memcpy(&code,reply,2);
	code=ntohs(code);
	if(code!=CTRL_PORT)
	{
		MessageBox(hWnd,_T("Risposta inattesa dal server!\n(3)"),_T("Errore"),MB_OK);
		return false;
	}

	int port_serv;
	memcpy(&port_serv,reply+HEADER_CTRL_DIM,2);
	delete[] reply;
	// Contact server on this port to create 2 more connections
	struct sockaddr_in addr2;
	addr2.sin_family = AF_INET;
	addr2.sin_port = port_serv;
	addr2.sin_addr.s_addr = htonl(this->ip);

	// Create socket video and a thread that will receive video messages
	SOCKET s_video=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(s_video==INVALID_SOCKET)
		return false;

	if(connect(s_video,(struct sockaddr *) &addr2, sizeof(addr2)))
	{
		MessageBox(hWnd,_T("Impossibile connettersi al server!\n(s_video)"),_T("Errore"),MB_OK);
		return false;
	}

	std::auto_ptr<Runnable> r_video(reinterpret_cast<Runnable *>(new ClientVideo(hWnd,s_video)));
	t_video=new Thread(r_video);

	// Create a thread that will receive control messages
	std::auto_ptr<Runnable> r_ctrl(reinterpret_cast<Runnable *>(new ClientCtrl(hWnd,s_ctrl)));
	t_ctrl=new Thread(r_ctrl);

	
	Chat::GetChat(hWnd);	// Create "Chat" window (singleton singleton in the DLL)

	closing = false;
	return true;
}
コード例 #5
0
ファイル: download.c プロジェクト: PraveenSubramaniyam/CCS
int appDownload(char *host , char *url)
{
    int socket_desc,tmp;
    int fileFd;
    struct sockaddr_in server;
    char getMsg[200] , server_reply[2000];
    struct stat st;
    char fileName[1024];


    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
        return -1;
    }

    server.sin_addr.s_addr = inet_addr(host);
    server.sin_family = AF_INET;
    server.sin_port = htons( downloadPort );

    //Connect to remote server
    if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        PRINTF_FL("connect error");
        return -1;
    }

    PRINTF_FL("Success fully Connected to apache server");

    //Send some data
    //message = "GET /module HTTP/1.1\r\nHost: www.msn.com\r\n\r\n";
    snprintf(getMsg,sizeof(getMsg)-1,"GET /%s.zip HTTP/1.1\r\nHost: www.msn.com\r\n\r\n",url);
    if( send(socket_desc , getMsg , strlen(getMsg) , 0) < 0)
    {
        PRINTF_FL("Send failed");
        return -1;
    }
    PRINTF_FL("Data Send Url:%s",getMsg);

    //tmp = getFileNameStartPosition(url);
    snprintf(fileName,sizeof(fileName)-1,"%s/APP/%s.zip",fileRoot,url);
    //fileName = url+tmp+1;
    fileFd = ccsFileOpen(fileName,O_CREAT|O_WRONLY);
    if(fileFd <=0 )
    {
      PRINTF_FL("File Open failed : %s",url+tmp+1);
      return -1;
    }
    //Now receive full data
    int total_recv = recv_timeout(socket_desc,fileFd, 4);
    if(total_recv > 0){
      PRINTF_FL("Content Length received in header: %d",total_recv);
    }
    else
    {
      ccsFileClose(fileFd);
      remove(fileName);
      return -1;
    }

    if (stat(fileName, &st) == 0)
    {
       if( st.st_size != total_recv || total_recv == 0 )
       {
         PRINTF_FL("File size:%d,Content-Length:%d",(int)st.st_size,total_recv);
         ccsFileClose(fileFd);
        // remove(fileName);
         return -1;
       }
       else
       {
         PRINTF_FL("Module Download success");
       }
    }
    ccsFileClose(fileFd);
    snprintf(fileName,sizeof(fileName)-1,"%s %s/APP/%s.zip -d %s/APP/",unzipCmd,fileRoot,url,fileRoot);
    system(fileName);
    sleep(5);
    return 0;
}
コード例 #6
0
int main ()
{
    int socket_desc,tmp;
    int fileFd;
    struct sockaddr_in server;
    char getMsg[200] , server_reply[2000];
    struct stat st;
    char *fileName;
char *host ="127.0.0.1",  *url = "/helloworld.txt";


    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    server.sin_addr.s_addr = inet_addr(host);
    server.sin_family = AF_INET;
    server.sin_port = htons( 80 );

    //Connect to remote server
    if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected\n");

    //Send some data
    //message = "GET /module HTTP/1.1\r\nHost: www.msn.com\r\n\r\n";
    snprintf(getMsg,sizeof(getMsg)-1,"GET %s HTTP/1.1\r\nHost: www.msn.com\r\n\r\n",url);
    if( send(socket_desc , getMsg , strlen(getMsg) , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
    puts("Data Send\n");

    tmp = strlen(url)-1;
    while(tmp >= 0)
    {
      if(url[tmp] == '/')
        break;
      tmp-- ;
    }
    fileName = url+tmp+1;
    fileFd = open(fileName,O_CREAT|O_WRONLY|O_APPEND);
    if(fileFd <=0 )
    {
      printf("File Open failed : %s:%d\n",url+tmp+1,errno);
      return -1;
    }
    //Now receive full data
    int total_recv = recv_timeout(socket_desc,fileFd, 4);
    if(total_recv > 0){
      printf("Content Length received in header: %d\n",total_recv);
    }
    else
    {
      close(fileFd);
      remove(fileName);
      return -1;
    }

    if (stat(fileName, &st) == 0)
    {
       if( st.st_size != total_recv)
       {
         printf("File size:%d,Content-Length:%d\n",st.st_size,total_recv);
         return -1;
       }
    }

    return 0;
}
コード例 #7
0
static void server(int fd)
{
	int ret, csend = 0;
	gnutls_anon_server_credentials_t anoncred;
	char buffer[MAX_BUF + 1];
	gnutls_datum_t cookie_key;
	gnutls_dtls_prestate_st prestate;
	gnutls_session_t session;
	unsigned try = 0;

	/* this must be called once in the program
	 */
	global_init();

	if (debug) {
		gnutls_global_set_log_function(server_log_func);
		gnutls_global_set_log_level(4711);
	}

	ret = gnutls_key_generate(&cookie_key, GNUTLS_COOKIE_KEY_SIZE);
	if (ret < 0) {
		fail("Cannot generate key: %s\n", gnutls_strerror(ret));
		exit(1);
	}

	gnutls_anon_allocate_server_credentials(&anoncred);

	gnutls_init(&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
	gnutls_handshake_set_timeout(session, SERV_TIMEOUT * 1000);
	gnutls_dtls_set_mtu(session, 1500);

	/* avoid calling all the priority functions, since the defaults
	 * are adequate.
	 */
	gnutls_priority_set_direct(session,
				   "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
				   NULL);

	gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);

	gnutls_transport_set_int(session, fd);
	gnutls_transport_set_push_function(session, push);

	for (;;) {
		ret = recv_timeout(fd, buffer, sizeof(buffer), MSG_PEEK, SERV_TIMEOUT);
		if (ret < 0) {
			if (try != 0) {
				success("Server was terminated as expected!\n");
				goto exit;
			} else {
				fail("Error receiving first message\n");
				exit(1);
			}
		}
		try++;

		memset(&prestate, 0, sizeof(prestate));
		prestate.record_seq = 105791312;
		prestate.hsk_write_seq = 67166359;
		ret =
		    gnutls_dtls_cookie_verify(&cookie_key, CLI_ADDR,
					      CLI_ADDR_LEN, buffer, ret,
					      &prestate);
		if (ret < 0) {	/* cookie not valid */
			if (debug)
				success("Sending hello verify request\n");

			ret =
			    gnutls_dtls_cookie_send(&cookie_key, CLI_ADDR,
						    CLI_ADDR_LEN,
						    &prestate,
						    (gnutls_transport_ptr_t)
						    (long) fd, push);
			if (ret < 0) {
				fail("Cannot send data\n");
				exit(1);
			}

			/* discard peeked data */
			recv_timeout(fd, buffer, sizeof(buffer), 0, SERV_TIMEOUT);
			csend++;

			if (csend > 2) {
				fail("too many cookies sent\n");
				exit(1);
			}

			continue;
		}

		/* success */
		break;
	}

	fail("Shouldn't have reached here\n");
	exit(1);
 exit:
	gnutls_deinit(session);
	gnutls_free(cookie_key.data);

	gnutls_anon_free_server_credentials(anoncred);

	gnutls_global_deinit();
}
コード例 #8
0
void *ClientData::run()
{
	USHORT sz,fr;

	if(type==ClientData::t_send)
	{
		// Send on the socket the datas shared

		// Send the format of the clipboard data
		char info[sizeof(USHORT)*2];
		sz=htons(*size);
		fr=htons(*format);
		memcpy(info,&sz,sizeof(USHORT));
		memcpy(info+sizeof(USHORT),&fr,sizeof(USHORT));

		// Send the data
		int result=send(s,info,sizeof(USHORT)*2,NULL);

		if(result==0	|| result==SOCKET_ERROR)
		{
			//printError(L"send");
			//MessageBox(hWnd,_T("Invio dei dati preliminari fallito"),_T("Errore"),MB_OK);
			return NULL;
		}

		result=send(s,data,*size,NULL);

		if(result==0	|| result==SOCKET_ERROR)
		{
			//printError(L"send");
			//MessageBox(hWnd,_T("Invio dei dati fallito"),_T("Errore"),MB_OK);
			return NULL;
		}

	}else{

		// Connect with the owner of the clipboard required

		sockaddr_in c_in;
		int addr_sz=sizeof(c_in);
		
		SOCKET s_tmp=accept(s,reinterpret_cast<sockaddr *>(&c_in),&addr_sz);
		if(s_tmp==INVALID_SOCKET)
		{
			//printError(L"accept");
			MessageBox(hWnd,_T("Nessuna risposta dal proprietario; trasferimento della clipboard fallito."),_T("Errore"),MB_OK);
			return NULL;
		}

		closesocket(s);
		s=s_tmp;

	
		// Receive the size and format of the clipboard data
		char info[sizeof(USHORT)*2];
		if(!recv_timeout(s,info,sizeof(USHORT)*2,0))
		{
			//MessageBox(hWnd,_T("Nessun dato preliminare dal client!"),_T("Errore"),MB_OK);
			MessageBox(hWnd,_T("Nessuna risposta dal proprietario; trasferimento della clipboard fallito."),_T("Errore"),MB_OK);
			return NULL;
		}

		memcpy(&sz,info,sizeof(USHORT));
		memcpy(&fr,info+sizeof(USHORT),sizeof(USHORT));
		sz=htons(sz);
		fr=htons(fr);

		// Receive the data
		if(sz<1)
		{
			MessageBox(hWnd,_T("Ricevuto un valore di size inaccettabile; trasferimento della clipboard fallito."),_T("Errore"),MB_OK);
			return NULL;
		}

		char *cbData=new char[sz];

		if(!recv_timeout(s,cbData,sz,0))
		{
			//printError(L"recv_timeout");
			MessageBox(hWnd,_T("Nessuna risposta dal proprietario; trasferimento della clipboard fallito."),_T("Errore"),MB_OK);	
			return NULL;
		}

		// Copy datas in the clipboard
		if(OpenClipboard(hWnd)==false)
		{
			//printError(L"OpenClipboard");
			MessageBox(hWnd,L"Impossibile accedere alla Clipboard; trasferimento della clipboard fallito.",_T("Errore"),MB_OK);
			delete[] cbData;
			return false;
		}

		if(EmptyClipboard()==false)
		{
			//printError(L"EmptyClipboard");
			MessageBox(hWnd,L"Impossibile svuotare la clipboard; trasferimento della clipboard fallito.",_T("Errore"),MB_OK);
			delete[] cbData;
			CloseClipboard();
			return false;
		}

		HANDLE hData=GlobalAlloc(GMEM_MOVEABLE, sz); 
        if (hData == NULL) 
        { 
            //printError(L"GlobalAlloc");
			MessageBox(hWnd,L"Impossibile allocare l'oggetto globale; trasferimento della clipboard fallito.",_T("Errore"),MB_OK);
			delete[] cbData;
			CloseClipboard();
			return false;
		}
 
        // Lock the handle and copy the text to the buffer. 
        LPVOID lptstrCopy = GlobalLock(hData); 
        memcpy(lptstrCopy, cbData, sz);
        GlobalUnlock(hData); 

        // Place the handle on the clipboard. 
		if(SetClipboardData(fr,hData)==NULL)
		{
			//printError(L"SetClipboardData");
			MessageBox(hWnd,L"Impossibile impostare i dati nella clipboard.",_T("Errore"),MB_OK);
			GlobalFree(hData);
			delete[] cbData;
			return false;
		}

		if(CloseClipboard()==false)
		{
			printError(L"CloseClipboard");
			MessageBox(hWnd,L"Impossibile chiudere la Clipboard.",_T("Errore"),MB_OK);
			return false;
		}
		MessageBox(hWnd,_T("I dati richiesti sono stati trasferiti nella tua clipboard."),_T("Avviso"),MB_OK);
	}

	return NULL;
}