int hostname_to_entry(bcast_t* bcast, char* hostname) { network_address_t addr; unsigned int ipaddr; int entry = -1; int i; if (hostname == NULL) return bcast->me; if (network_translate_hostname(hostname, addr) != 0) { kprintf("Error: could not resolve host name.\n"); AbortOnCondition(1,"Crashing."); } ipaddr = addr[0]; for (i=0; i<bcast->n_entries; i++) if (ipaddr == bcast->entries[i].addr[0]) entry = i; AbortOnCondition(entry == -1, "Error: host name not in broadcast table."); return entry; }
int transmit(int* arg) { char buffer[BUFFER_SIZE]; int length; int i; network_address_t addr; miniport_t port; miniport_t dest; AbortOnCondition(network_translate_hostname(hostname, addr) < 0, "Could not resolve hostname, exiting."); port = miniport_local_create(0); dest = miniport_remote_create(addr, 0); for (i=0; i<MAX_COUNT; i++) { printf("Sending packet %d.\n", i+1); sprintf(buffer, "Count is %d.\n", i+1); length = strlen(buffer) + 1; minimsg_send(port, dest, buffer, length); } miniport_destroy(port); return 0; }
void network_get_my_address(network_address_t my_address) { char hostname[64]; assert(gethostname(hostname, 64) == 0); network_translate_hostname(hostname, my_address); my_address[1] = htons(my_udp_port); }
static int receiver(int* arg) { int len; char buffer[BUFFER_SIZE]; network_address_t dest; if (NULL != remote_name) { network_translate_hostname(remote_name, dest); connection = minisocket_client_create(dest, port_num, &error); } else { connection = minisocket_server_create(port_num, &error); } if (NULL == connection) { printf("Connection error.\n"); return -1; } else { minithread_fork(sender, NULL); printf("Connection successful. Type and enter to send messages.\n"); } while (1) { buffer[BUFFER_SIZE] = '\0'; len = minisocket_receive(connection, buffer, BUFFER_SIZE - 1, &error); buffer[len] = '\0'; printf("[Received message]: %s", buffer); } return 0; }
int transmit1(int* arg) { char buffer[BUFFER_SIZE]; int length; int i; network_address_t addr; miniport_t port; miniport_t dest; AbortOnCondition(network_translate_hostname(hostname, addr) < 0, "Could not resolve hostname, exiting."); port = miniport_create_unbound(0); dest = miniport_create_bound(addr, 1); for (i=MAX_COUNT/2; i<MAX_COUNT; i++) { printf("Sending packet %d.\n", i+1); sprintf(buffer, "Count is %d.\n", i+1); length = strlen(buffer) + 1; minimsg_send(port, dest, buffer, length); minithread_yield(); } return 0; }
int transmit_first(int* arg) { char buffer[BUFFER_SIZE]; int length = BUFFER_SIZE; int i; network_address_t addr; miniport_t port; miniport_t dest; miniport_t from; AbortOnCondition(network_translate_hostname(hostname, addr) < 0, "Could not resolve hostname, exiting."); port = miniport_create_unbound(0); dest = miniport_create_bound(addr, 1); for (i=0; i<MAX_COUNT; i++) { printf("Sending packet %d.\n", i+1); sprintf(buffer, "Received packet %d.\n", i+1); length = strlen(buffer) + 1; minimsg_send(port, dest, buffer, length); length = BUFFER_SIZE; minimsg_receive(port, &from, buffer, &length); printf("%s", buffer); miniport_destroy(from); } return 0; }
int transmit_first(int* arg) { char buffer[MINIMSG_MAX_MSG_SIZE + 10]; int length = 0; int i; network_address_t hostaddr, targetaddr; miniport_t port; miniport_t dest; struct mini_header hdr; AbortOnCondition(network_translate_hostname(hostname, targetaddr) < 0, "Could not resolve hostname, exiting."); port = miniport_create_unbound(0); dest = miniport_create_bound(targetaddr, 1); /* Form correct header */ network_get_my_address(hostaddr); hdr.protocol = PROTOCOL_MINIDATAGRAM; pack_address(hdr.source_address, hostaddr); pack_unsigned_short(hdr.source_port, port->num); pack_address(hdr.destination_address, dest->bound.addr); pack_unsigned_short(hdr.destination_port, dest->bound.remote); /* Send packages with short but correct header and zero data */ printf("Sending packages with short headers.\n"); sprintf(buffer, "Receiving packages with short headers.\n"); length = strlen(buffer) + 1; minimsg_send(port, dest, buffer, length); for (i = 0; i < MINIMSG_HDRSIZE; i++) network_send_pkt(targetaddr, i, (char*)&hdr, 0, buffer); /* Send packages to wrong ports */ printf("Sending packages to wrong destination ports.\n"); sprintf(buffer, "Receiving packages with wrong destination ports.\n"); length = strlen(buffer) + 1; minimsg_send(port, dest, buffer, length); sprintf(buffer, "This message is sent to a wrong port.\n"); length = strlen(buffer) + 1; minimsg_send(port, miniport_create_bound(targetaddr, 0), buffer, length); minimsg_send(port, miniport_create_bound(targetaddr, MAX_UNBOUNDED), buffer, length); minimsg_send(port, miniport_create_bound(targetaddr, MAX_UNBOUNDED + 1), buffer, length); minimsg_send(port, miniport_create_bound(targetaddr, MIN_BOUNDED), buffer, length); minimsg_send(port, miniport_create_bound(targetaddr, MAX_BOUNDED), buffer, length); printf("Send-first finished.\n"); return 0; }
void network_get_my_address(network_address_t my_address) { char hostname[64]; assert(gethostname(hostname, 64) == 0); network_translate_hostname(hostname, my_address); /* network_translate_hostname("localhost", my_address); my_address[1] = (long) htons(my_udp_port); */ }
int client(int* arg) { int i; minisocket_error error; network_address_t address; network_translate_hostname(hostname, address); recv_skt = minisocket_client_create(address, port, &error); for (i=0; i<THREAD_COUNTER; i++) { minithread_fork(receiver,&thread_id[i]); } return 0; }
void bcast_initialize(char* configfile, bcast_t* bcast) { FILE* config = fopen(configfile, "r"); char line[BCAST_MAX_LINE_LEN]; int i = 0; char* rv; network_address_t my_addr; unsigned int my_ip_addr; network_get_my_address(my_addr); my_ip_addr = my_addr[0]; while ((rv = fgets(line, BCAST_MAX_LINE_LEN, config)) != NULL) { if (line[0] == '\r' || line[0] == '\n') break; line[strlen(line)-1] = '\0'; strcpy(bcast->entries[i].name, line); bcast->entries[i].n_links = 0; if (network_translate_hostname(line, bcast->entries[i].addr) != 0) { kprintf("Error: could not resolve hostname %s.\n", line); AbortOnCondition(1,"Crashing."); } if (bcast->entries[i].addr[0] == my_ip_addr) bcast->me = i; i++; } bcast->n_entries = i; if (rv != NULL) for (i=0; i<bcast->n_entries; i++) { //int len; int j; AbortOnCondition(fgets(line, BCAST_MAX_LINE_LEN, config) == NULL, "Error: incomplete adjacency matrix."); //len = strlen(line); for (j=0; j<bcast->n_entries; j++) if (i == j) ; /* avoid self-links */ else if (line[j] != '.') { bcast->entries[i].links[bcast->entries[i].n_links] = j; bcast->entries[i].n_links++; } } fclose(config); }
int client(int* arg) { network_address_t address; minisocket_t socket; minisocket_error error; char msg[strlen("hello\n") + 1]; network_translate_hostname(hostname, address); /* create a network connection to the local machine */ socket = minisocket_client_create(address, port,&error); printf("made a client\n"); minisocket_receive(socket, msg, strlen("hello\n")+1, &error); return 0; }
int client(int* arg) { char buffer[BUFFER_SIZE]; int i; int bytes_received; network_address_t address; minisocket_t socket; minisocket_error error; network_translate_hostname(hostname, address); /* create a network connection to the local machine */ socket = minisocket_client_create(address, port,&error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } /* receive the message */ bytes_received=0; while (bytes_received!=BUFFER_SIZE){ int received_bytes; if ( (received_bytes=minisocket_receive(socket,buffer,BUFFER_SIZE-bytes_received, &error))==-1){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); /* close the connection */ minisocket_close(socket); return -1; } /* test the information received */ for (i=0; i<received_bytes; i++){ if (buffer[i]!=(char)( (bytes_received+i)%256 )){ printf("The %d'th byte received is wrong.\n", bytes_received+i); /* close the connection */ minisocket_close(socket); return -1; } } bytes_received+=received_bytes; } printf("All bytes received correctly.\n"); minisocket_close(socket); return 0; }
int client(int* arg) { int port; network_address_t address; minisocket_t socket; minisocket_error error; port = *arg; network_translate_hostname(hostname, address); socket = minisocket_client_create(address, port, &error); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } printf("Client connected on port %i\n", port); minisocket_close(socket); return 0; }
int transmit(int* arg) { char buffer[BUFFER_SIZE]; int length; network_address_t addr; miniport_t port; miniport_t dest; AbortOnCondition(network_translate_hostname(hostname, addr) < 0, "Could not resolve hostname, exiting."); port = miniport_create_unbound(42); dest = miniport_create_bound(addr, 42); minithread_fork(receive, NULL); while(1){ memset(buffer, 0, BUFFER_SIZE); length = miniterm_read(buffer, BUFFER_SIZE); minimsg_send(port, dest, buffer, length); } return 0; }
int network_initialize(interrupt_handler_t network_handler) { int arg = 1; /* initialise the NT socket library, inexplicably required by NT */ assert(WSAStartup(MAKEWORD(2, 0), &winsock_version_data) == 0); if (atomic_test_and_set(&initialized)) { return -1; } memset(&if_info, 0, sizeof(if_info)); if_info.sock = socket(PF_INET, SOCK_DGRAM, 0); if (if_info.sock < 0) { perror("socket"); return -1; } if_info.sin.sin_family = SOCK_DGRAM; if_info.sin.sin_addr.s_addr = htonl(0); if_info.sin.sin_port = htons(my_udp_port); if (bind(if_info.sock, (struct sockaddr *) &if_info.sin, sizeof(if_info.sin)) < 0) { /* kprintf("Error: code %ld.\n", GetLastError());*/ AbortOnError(0); perror("bind"); return -1; } /* set for fast reuse */ assert(setsockopt(if_info.sock, SOL_SOCKET, SO_REUSEADDR, (char *) &arg, sizeof(int)) == 0); if (BCAST_ENABLED){ if (BCAST_USE_TOPOLOGY_FILE){ bcast_initialize(BCAST_TOPOLOGY_FILE, &topology); } else { assert(setsockopt(if_info.sock, SOL_SOCKET, SO_BROADCAST, (char *) &arg, sizeof(int)) == 0); network_translate_hostname(BCAST_ADDRESS,broadcast_addr); } } /* * Print network information on the screen (mostly for Joranda). */ { network_address_t my_address; char my_hostname[256]; network_get_my_address(my_address); network_format_address(my_address, my_hostname, 256); kprintf("Hostname of local machine: %s.\n",my_hostname); } /* * Interrupts are handled through the caller's handler. */ start_network_poll(network_handler, &if_info.sock); return 0; }