void NetworkProcess::createNetworkConnectionToWebProcess() { #if USE(UNIX_DOMAIN_SOCKETS) IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection(); RefPtr<NetworkConnectionToWebProcess> connection = NetworkConnectionToWebProcess::create(socketPair.server); m_webProcessConnections.append(connection.release()); IPC::Attachment clientSocket(socketPair.client); parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientSocket), 0); #elif OS(DARWIN) // Create the listening port. mach_port_t listeningPort; mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort); // Create a listening connection. RefPtr<NetworkConnectionToWebProcess> connection = NetworkConnectionToWebProcess::create(IPC::Connection::Identifier(listeningPort)); m_webProcessConnections.append(connection.release()); IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND); parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientPort), 0); #else notImplemented(); #endif }
/* connect to SMTP server and returns the socket fd */ static SOCKET smtpConnect(char *smtp_server,int port) { SOCKET sfd; if (g_use_protocol == MSOCK_USE_IPV4) { showVerbose("Forcing to use IPv4 address of SMTP server\n"); } else if (g_use_protocol == MSOCK_USE_IPV6) { showVerbose("Forcing to use IPv6 address of SMTP server\n"); } else { showVerbose("Will detect IPv4 or IPv6 automatically\n"); } sfd=clientSocket(g_use_protocol, smtp_server,port, g_connect_timeout); if (sfd == INVALID_SOCKET) { errorMsg("Could not connect to SMTP server \"%s\" at port %d", smtp_server,port); return (INVALID_SOCKET); } /* set the socket to msock lib's static place, not thread safe*/ msock_set_socket(sfd); return (sfd); }
void PluginProcess::createWebProcessConnection() { bool didHaveAnyWebProcessConnections = !m_webProcessConnections.isEmpty(); #if OS(DARWIN) // Create the listening port. mach_port_t listeningPort; mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort); // Create a listening connection. RefPtr<WebProcessConnection> connection = WebProcessConnection::create(IPC::Connection::Identifier(listeningPort)); if (m_audioHardwareListener) { if (m_audioHardwareListener->hardwareActivity() == WebCore::AudioHardwareActivityType::IsActive) connection->audioHardwareDidBecomeActive(); else if (m_audioHardwareListener->hardwareActivity() == WebCore::AudioHardwareActivityType::IsInactive) connection->audioHardwareDidBecomeInactive(); } m_webProcessConnections.append(connection.release()); IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND); parentProcessConnection()->send(Messages::PluginProcessProxy::DidCreateWebProcessConnection(clientPort, m_supportsAsynchronousPluginInitialization), 0); #elif USE(UNIX_DOMAIN_SOCKETS) IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection(); RefPtr<WebProcessConnection> connection = WebProcessConnection::create(socketPair.server); m_webProcessConnections.append(connection.release()); IPC::Attachment clientSocket(socketPair.client); parentProcessConnection()->send(Messages::PluginProcessProxy::DidCreateWebProcessConnection(clientSocket, m_supportsAsynchronousPluginInitialization), 0); #else notImplemented(); #endif if (NetscapePluginModule* module = netscapePluginModule()) { if (!didHaveAnyWebProcessConnections) { // Increment the load count. This is matched by a call to decrementLoadCount in removeWebProcessConnection. // We do this so that the plug-in module's NP_Shutdown won't be called until right before exiting. module->incrementLoadCount(); } } disableTermination(); }
void child() { int server_sockfd, client_sockfd; if((server_sockfd = serverSocket(IPADDR, PORTNM)) == -1) { fprintf(stderr, "serverSocket"); exit(EXIT_FAILURE); } if(listen(server_sockfd, SOMAXCONN) == -1) { perror("listen"); closeSocket(server_sockfd); exit(EXIT_FAILURE); } if((client_sockfd = clientSocket(server_sockfd)) == -1) { fprintf(stderr, "clientSocket"); exit(EXIT_FAILURE); } mesgWnd(client_sockfd); closeSocket(server_sockfd); closeSocket(client_sockfd); exit(EXIT_SUCCESS); }
void Server::onNewConnection() { QLocalSocket* socket = m_server->nextPendingConnection(); if (!socket) { log("No pending client connections!", LogError); } else if ( socket->state() != QLocalSocket::ConnectedState ) { log("Client is not connected!", LogError); socket->deleteLater(); } else { QScopedPointer<ClientSocket> clientSocket( new ClientSocket(socket) ); const Arguments args = clientSocket->readArguments(); if ( !args.isEmpty() ) { ++m_socketCount; connect( clientSocket.data(), SIGNAL(destroyed()), this, SLOT(onSocketClosed()) ); connect( this, SIGNAL(destroyed()), clientSocket.data(), SLOT(close()) ); connect( this, SIGNAL(destroyed()), clientSocket.data(), SLOT(deleteAfterDisconnected()) ); emit newConnection( args, clientSocket.take() ); } } }
void NetGuard_DHCPD::packet_in(struct user_data *u_data, int *mode, unsigned int *vlanid, struct tpacket_hdr *h, struct ether_header *eth, struct iphdr *ip, struct tcphdr *tcp, void *data) { if (!security) { ng_logerror_buff(0,"missing Security Module!"); exit(-1); return; } //we only want it once if (*mode == TRAFFIC_INCOMING || *mode == TRAFFIC_OUTGOING ) return; //if (!(*mode == TRAFFIC_OUTGOING )) return; if (eth->ether_type != htons_ETHERTYPE_IP) return; //only ip if (ip->protocol!=IPPROTO_UDP) return; //udp only if (ntohs(tcp->dest) != LISTEN_PORT) return; //only the dhcpd port if ((ip->daddr != zero_ip) && (ip->daddr != ntohl(my_ip)) && (ip->daddr != b_ip)) return; //only send to us //if ((ip->saddr != zero_ip)) return; //only send from zero ip //so we got an dhcp package it seams like dest and port is correct struct dhcpMessage *packet; //void *data2 = data; //it crashes else? packet = (struct dhcpMessage *)((char*)data-12); //TODO check why 12?! crashes? unsigned char *state; //, *hw_addr; unsigned char *server_id; if(htonl(packet->cookie) != MAGIC) { ng_logdebug_spam("ignoring dhcp message with wrong cookie %x",htonl(packet->cookie)); return; } ng_logdebug_spam("got package -- processing"); char *tmpstr = (char*)malloc(5000); sprint_package(tmpstr,vlanid,h,eth,ip,tcp,data); ng_logdebug_spam("%s",tmpstr); free(tmpstr); tmpstr = (char*)malloc(5000); sprint_dhcp_package(tmpstr,packet); ng_logdebug_spam("%s",tmpstr); free(tmpstr); if((state = getOption(packet->options, DHCP_MESSAGE_TYPE)) == NULL) { ng_logdebug_spam("couldnt get option from packet (MSG_TYPE) -- ignoring"); return; } sec_data_idx idx; memcpy(&idx.hw_addr,ð->ether_shost,sizeof(mac_addr)); idx.vlan_id = (*vlanid); user_data *m_u_data = (user_data *)security->get_data(&idx); u_int32_t offerip; //lets see if we can find the ip to offer if (!m_u_data) { ng_logdebug("got client message - but we dont have any known user for that mac %02x:%02x:%02x:%02x:%02x:%02x in vlan %d",printf_mac_params(eth->ether_shost),(*vlanid)); return; //todo look at port etc } offerip = m_u_data->saddr; /* Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: Request CLASS Option 77, length 14: "RRAS.Microsoft" Client-ID Option 61, length 17: ether 52:41:53:20:00:14:0b:3b:43:00:00:00:01:00:00:00 Requested-IP Option 50, length 4: 141.30.225.74 Server-ID Option 54, length 4: 141.30.225.1 Hostname Option 12, length 7: "Lins-PC" FQDN Option 81, length 10: [N] "Lins-PC" Vendor-Class Option 60, length 8: "MSFT 5.0" Parameter-Request Option 55, length 12: Subnet-Mask, Domain-Name, Default-Gateway, Domain-Name-Server Netbios-Name-Server, Netbios-Node, Netbios-Scope, Router-Discovery Static-Route, Classless-Static-Route, Classless-Static-Route-Microsoft, Vendor-Option Client-Ethernet-Address 08:10:75:0a:3a:33 Vendor-rfc1048 Extensions Magic Cookie 0x63825363 DHCP-Message Option 53, length 1: Request Client-ID Option 61, length 7: ether 08:10:75:0a:3a:33 Hostname Option 12, length 9: "Routerata" Domain-Name Option 15, length 12: "RouterDomain" FQDN Option 81, length 13: "Routerata." Vendor-Class Option 60, length 8: "MSFT 5.0" Requested-IP Option 50, length 4: 141.30.225.42 Subnet-Mask Option 1, length 4: 255.255.255.0 Default-Gateway Option 3, length 4: 141.30.225.1 Domain-Name-Server Option 6, length 8: 141.30.225.3,141.30.66.135 Server-ID Option 54, length 4: 141.30.225.1 Parameter-Request Option 55, length 11: Subnet-Mask, Domain-Name, Default-Gateway, Domain-Name-Server Netbios-Name-Server, Netbios-Node, Netbios-Scope, Router-Discovery Static-Route, Classless-Static-Route-Microsoft, Vendor-Option */ unsigned char *host = getOption(packet->options, 12); unsigned int hostl = getOptionLength(packet->options, 12); unsigned char *fqdn = getOption(packet->options, 15); unsigned int fqdnl = getOptionLength(packet->options, 15); unsigned char *vendor = getOption(packet->options, 60); unsigned int vendorl = getOptionLength(packet->options, 60); unsigned char *dclass = getOption(packet->options, 77); unsigned int dclassl = getOptionLength(packet->options, 77); if (vendor != NULL) { if (strlen((const char*)vendor) == strlen("Adobe Flash Proxy Auto-Discovery")) { if (strncmp("Adobe Flash Proxy Auto-Discovery",(const char*)vendor,strlen("Adobe Flash Proxy Auto-Discovery"))) { ng_logdebug_spam("ignoring Adobe Flash Proxy Auto-Discovery"); return; } } } ng_log_ext_buff(0,500,"offer %s to mac %02x:%02x:%02x:%02x:%02x:%02x - host:%.*s fqdn: %.*s vendor:%.*s class:%.*s", inet_ntoa(*(struct in_addr *)&offerip),printf_mac_params(eth->ether_shost),hostl,host,fqdnl,fqdn,vendorl,vendor,dclassl,dclass); int client_socket; if((client_socket = clientSocket(LISTEN_PORT, SEND_PORT)) == -1) { //syslog(LOG_ERR, "couldn't create client socket -- i'll try again"); return; } struct ifreq intf; //syslog(LOG_INFO, "Binding to interface '%s'\n", interface_name); bzero(&intf, sizeof(intf)); strncpy(intf.ifr_name, interface_name.c_str(), IFNAMSIZ); if (setsockopt(client_socket, SOL_SOCKET, SO_BINDTODEVICE, &intf, sizeof(intf)) < 0) { //syslog(LOG_INFO, "setsockopt(SO_BINDTODEVICE) %d\n", errno); close(client_socket); return; }; switch(state[0]) { case DHCPDISCOVER: ng_logdebug_spam("received DISCOVER"); if(sendOffer(client_socket, packet,offerip,subnet_ip,my_ip,dns_ip1,dns_ip2,wins_ip) == -1) { ng_logerror("send OFFER failed -- ignoring"); } else ng_logdebug_spam("send sendOffer"); break; case DHCPREQUEST: ng_logdebug_spam("received DHCPREQUEST"); //syslog(LOG_INFO,"received REQUEST"); server_id = getOption(packet->options, 0x36); if(server_id == NULL) { ng_logdebug("get option on 0x36 failed! NAKing"); sendNAK(client_socket, packet,my_ip); /* Let's send an offer as well */ if(sendOffer(client_socket, packet,offerip,subnet_ip,my_ip,dns_ip1,dns_ip2,wins_ip) == -1) { ng_logerror("send OFFER failed -- ignoring"); } } else { ng_logdebug_spam("server_id = %02x%02x%02x%02x", server_id[0], server_id[1],server_id[2], server_id[3]); if(memcmp(server_id, (char *)&my_ip, 4) == 0) { ng_logdebug_spam("sending ACK - server_id matched"); if (sendACK(client_socket, packet,offerip,subnet_ip,my_ip,dns_ip1,dns_ip2,wins_ip) == -1) { ng_logdebug("send ACK failed - sending NAK"); sendNAK(client_socket, packet,my_ip); } else { ng_logdebug_spam("send ACK"); } } else { ng_logdebug_spam("sending NAK - server_id missmatch"); sendNAK(client_socket,packet,my_ip); } } break; default: ng_logdebug("unsupported DHCP message (%02x) -- ignoring",state[0]); break; } close(client_socket); }
int main(int argc, char* argv[]) { UINT16 serverPort = 0; ProtoAddress serverAddr; if (3 == argc) // "simple listen <portNumber>" { if (!strncmp("listen", argv[1], strlen(argv[1]))) { if (1 != (sscanf(argv[2], "%hu", &serverPort))) { fprintf(stderr, "simple: bad <port>\n"); serverPort = 0; } } } else if (4 == argc) // "simple connect <serverAddr> <serverPort> { if (!strncmp("connect", argv[1], strlen(argv[1]))) { if (1 != (sscanf(argv[3], "%hu", &serverPort))) { fprintf(stderr, "simple: bad <serverPort>\n"); serverPort = 0; } else if (!serverAddr.ResolveFromString(argv[2])) { fprintf(stderr, "simple: bad <serverAddr>\n"); serverPort = 0; } else { serverAddr.SetPort(serverPort); } } } if (!serverAddr.IsValid() && (0 == serverPort)) { usage(); return -1; } if (serverAddr.IsValid()) // connect to server address as a "client" and make a request { ProtoSocket clientSocket(ProtoSocket::TCP); fprintf(stderr, "simple: client connecting to server: %s/%hu ...\n", serverAddr.GetHostString(), serverAddr.GetPort()); if (!clientSocket.Connect(serverAddr)) { fprintf(stderr, "simple: error connecting to server: %s/%hu\n", serverAddr.GetHostString(), serverAddr.GetPort()); return -1; } fprintf(stderr, "simple: client sending request to server ...\n"); char* clientRequest = "Hello Server, this is a simple protolib client!"; unsigned int length = strlen(clientRequest) + 1; unsigned int sent = 0; while (sent < length) { unsigned int numBytes = length - sent; if (!clientSocket.Send(clientRequest+sent, numBytes)) { fprintf(stderr, "simple: error sending to server\n"); clientSocket.Close(); return -1; } else { sent += numBytes; } } fprintf(stderr, "simple: client awaiting response from server ...\n"); bool receiving = true; while (receiving) { char buffer[256]; buffer[256] = '\0'; unsigned int numBytes = 255; if (!clientSocket.Recv(buffer, numBytes)) { fprintf(stderr, "simple: error receiving from server\n"); clientSocket.Close(); return -1; } else if (numBytes > 0) { fprintf(stdout, "simple: client recvd \"%s\" from server: %s/%hu\n", buffer, serverAddr.GetHostString(), serverAddr.GetPort()); } else { fprintf(stderr, "simple: server shutdown connection.\n"); receiving = false; } } // end while(receiving) clientSocket.Close(); } else // act as a "server" listening to the indicated port, responding to requests { for (;;) { ProtoSocket serverSocket(ProtoSocket::TCP); if (!serverSocket.Listen(serverPort)) { fprintf(stderr, "simple: server error listening\n"); serverSocket.Close(); return -1; } else { fprintf(stderr, "simple: server listening on port:%hu ... (use <CTRL-C> to exit)\n", serverPort); if (!serverSocket.Accept()) { fprintf(stderr, "simple: server error accepting connection\n"); serverSocket.Close(); return -1; } fprintf(stderr, "simple: server accepted connection from client: %s/%hu ...\n", serverSocket.GetDestination().GetHostString(), serverSocket.GetDestination().GetPort()); bool receiving = true; while (receiving) { char buffer[256]; buffer[256] = '\0'; unsigned int numBytes = 255; if (!serverSocket.Recv(buffer, numBytes)) { fprintf(stderr, "simple: error receiving from client\n"); serverSocket.Close(); return -1; } else if (numBytes > 0) { fprintf(stdout, "simple: server recvd \"%s\" from client: %s/%hu\n", buffer, serverSocket.GetDestination().GetHostString(), serverSocket.GetDestination().GetPort()); if (NULL != strchr(buffer, '!')) { fprintf(stderr, "simple: server recvd EOT character '!' from client ...\n"); receiving = false; } break; } else { fprintf(stderr, "simple: client closed connection\n"); receiving = false; } } fprintf(stderr, "simple: server sending response to client ...\n"); char* serverResponse = "Hi there Client, this is a simple protolib server"; unsigned int length = strlen(serverResponse) + 1; unsigned int sent = 0; while (sent < length) { unsigned int numBytes = length - sent; if (!serverSocket.Send(serverResponse+sent, numBytes)) { fprintf(stderr, "simple: error sending to client\n"); serverSocket.Close(); return -1; } else { sent += numBytes; } } serverSocket.Shutdown(); // After "Shutdown" on a blocking socket, ProtoSocket::Recv() will unblock upon // receiving "FIN" from client TCP char buffer[8]; unsigned int numBytes = 8; serverSocket.Recv(buffer, numBytes); if (0!= numBytes) fprintf(stderr, "simple: server received extra data from client?\n\n"); else fprintf(stderr, "simple: server transmission complete.\n\n"); } serverSocket.Close(); } // end for (;;) } } // end main();