static int net_close(struct open_file *f) { #ifdef NETIF_DEBUG if (debug) printf("net_close: opens=%d\n", netdev_opens); #endif f->f_devdata = NULL; #ifndef NETIF_OPEN_CLOSE_ONCE /* Extra close call? */ if (netdev_opens <= 0) return (0); netdev_opens--; /* Not last close? */ if (netdev_opens > 0) return (0); /* On last close, do netif close, etc. */ #ifdef NETIF_DEBUG if (debug) printf("net_close: calling net_cleanup()\n"); #endif net_cleanup(); #endif return (0); }
int connection_termination(connection_data *connection, uint8_t side) { handshake_packet *rst_ack_packet = NULL; char *ptr = NULL; int i; #ifdef _DEBUG_ printf("Terminating connection.\n"); #endif switch(side) { case SERVER: #ifdef _DEBUG_ printf("Preparing ACK packet for termination.\n"); #endif rst_ack_packet = malloc(sizeof(handshake_packet)); rst_ack_packet->proto_id = "herp"; rst_ack_packet->packet_type = HANDSHAKE; rst_ack_packet->client_ip = connection->client_ip; rst_ack_packet->client_port = connection->client_port; rst_ack_packet->client_id = connection->client_id; rst_ack_packet->flags = ACK; rst_ack_packet->trailer = "derp"; #ifdef _DEBUG_ printf("Converting to string.\n"); #endif ptr = packet_to_string(rst_ack_packet, HANDSHAKE); #ifdef _DEBUG_ printf("Sending ACK for termination.\n"); #endif for(i = 0; i < 2; i++) { if (sendto(connection->sock_fd, ptr, strlen(ptr), 0, (struct sockaddr *)connection->destination, sizeof(struct sockaddr)) < 0) { perror("sendto() failed in ACK for termination"); CLOSE(connection->sock_fd); return -1; } } free(ptr); free(rst_ack_packet); break; case CLIENT: #ifdef _DEBUG_ printf("Closing socket.\n"); #endif CLOSE(connection->sock_fd); #ifdef _DEBUG_ printf("Freeing connection_data.\n"); #endif free(connection->destination); free(connection); break; } net_cleanup(); return 0; }
/* * Called by devopen after it sets f->f_dev to our devsw entry. * This opens the low-level device and sets f->f_devdata. * This is declared with variable arguments... */ static int net_open(struct open_file *f, ...) { char temp[FNAME_SIZE]; struct iodesc *d; va_list args; char *devname; /* Device part of file name (or NULL). */ int error = 0; va_start(args, f); devname = va_arg(args, char*); va_end(args); #ifdef NETIF_OPEN_CLOSE_ONCE /* Before opening another interface, close the previous one first. */ if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0) net_cleanup(); #endif /* On first open, do netif open, mount, etc. */ if (netdev_opens == 0) { /* Find network interface. */ if (netdev_sock < 0) { netdev_sock = netif_open(devname); if (netdev_sock < 0) { printf("net_open: netif_open() failed\n"); return (ENXIO); } netdev_name = strdup(devname); #ifdef NETIF_DEBUG if (debug) printf("net_open: netif_open() succeeded\n"); #endif } /* * If network params were not set by netif_open(), try to get * them via bootp, rarp, etc. */ if (rootip.s_addr == 0) { /* Get root IP address, and path, etc. */ error = net_getparams(netdev_sock); if (error) { /* getparams makes its own noise */ free(netdev_name); netif_close(netdev_sock); netdev_sock = -1; return (error); } } /* * Set the variables required by the kernel's nfs_diskless * mechanism. This is the minimum set of variables required to * mount a root filesystem without needing to obtain additional * info from bootp or other sources. */ d = socktodesc(netdev_sock); sprintf(temp, "%6D", d->myea, ":"); setenv("boot.netif.hwaddr", temp, 1); setenv("boot.netif.ip", inet_ntoa(myip), 1); setenv("boot.netif.netmask", intoa(netmask), 1); setenv("boot.netif.gateway", inet_ntoa(gateip), 1); setenv("boot.nfsroot.server", inet_ntoa(rootip), 1); setenv("boot.nfsroot.path", rootpath, 1); } netdev_opens++; f->f_devdata = &netdev_sock; return (error); }
int main(void) { int rv; int client_socket = -1; // Initialize networking rv = net_init(); if(rv != 0) { fprintf(stderr, "Failed to initialize networking.\n"); goto out; } // Connect to the server // DON'T FORGET, THE SERVER NEEDS TO RUN FIRST. rv = socket_connect("localhost", "21012", &client_socket); if(rv != 0) { fprintf(stderr, "Failed to connect.\n"); goto out; } printf("Connected!!\n"); for(;;) // Forever { HeartbleedMessage *hm = NULL; char buff[2048] = ""; int read = 0, sent = 0, msg_len, i, hm_size; // Prompt the user printf("Input message to echo: \n"); scanf("%s", buff); printf("Input message length: \n"); scanf( "%d", &msg_len); // Allocate space for our little message. hm_size = sizeof(HeartbleedMessage) + strlen(buff) + 1; hm = (HeartbleedMessage *) malloc(hm_size); hm->hm_len = msg_len; strcpy(hm->hm_data, buff); // Send the message sent = socket_send(client_socket, (const char *) hm, hm_size); if(sent <= 0) { fprintf(stderr, "Failed to send.\n"); break; } // Free what we sents free(hm); // Recieve a (hopefully) large message from the server. read = socket_recv(client_socket, buff, sizeof(buff)); if(read <= 0) { fprintf(stderr, "Failed to recv.\n"); break; } // cast our message hm = (HeartbleedMessage *) buff; // Print what we got. >:) printf("Echo: "); for(i = 0; i < hm->hm_len && i < sizeof(buff); i++ ){ putchar(hm->hm_data[i]); } printf("\n"); } printf("Disconnected.\n"); out: socket_close(&client_socket); net_cleanup(); return 0; }
static void kore_server_start(void) { u_int32_t tmp; int quit; #if defined(KORE_SINGLE_BINARY) void (*preload)(void); #endif if (foreground == 0 && daemon(1, 1) == -1) fatal("cannot daemon(): %s", errno_s); kore_pid = getpid(); if (!foreground) kore_write_kore_pid(); kore_log(LOG_NOTICE, "%s is starting up", __progname); #if defined(KORE_USE_PGSQL) kore_log(LOG_NOTICE, "pgsql built-in enabled"); #endif #if defined(KORE_USE_TASKS) kore_log(LOG_NOTICE, "tasks built-in enabled"); #endif #if defined(KORE_USE_JSONRPC) kore_log(LOG_NOTICE, "jsonrpc built-in enabled"); #endif #if defined(KORE_SINGLE_BINARY) *(void **)&(preload) = kore_module_getsym("kore_preload"); if (preload != NULL) preload(); #endif kore_platform_proctitle("kore [parent]"); kore_msg_init(); kore_worker_init(); /* Set worker_max_connections for kore_connection_init(). */ tmp = worker_max_connections; worker_max_connections = worker_count; net_init(); kore_connection_init(); kore_platform_event_init(); kore_msg_parent_init(); quit = 0; worker_max_connections = tmp; while (quit != 1) { if (sig_recv != 0) { switch (sig_recv) { case SIGHUP: #if !defined(KORE_SINGLE_BINARY) kore_worker_dispatch_signal(sig_recv); kore_module_reload(0); #endif break; case SIGINT: case SIGQUIT: case SIGTERM: quit = 1; kore_worker_dispatch_signal(sig_recv); continue; default: kore_log(LOG_NOTICE, "no action taken for signal %d", sig_recv); break; } sig_recv = 0; } kore_worker_wait(0); kore_platform_event_wait(100); kore_connection_prune(KORE_CONNECTION_PRUNE_DISCONNECT); } kore_platform_event_cleanup(); kore_connection_cleanup(); kore_domain_cleanup(); net_cleanup(); }