vector* get_all_interfaces(void) { if (!vector_empty(&interfaces)) { return &interfaces; } struct ifi_info *ifi, *ifi_head = Get_ifi_info_plus(AF_INET, 0); for (ifi = ifi_head; ifi != NULL; ifi = ifi->ifi_next) { vector_push_back(&interfaces, ifi); } return &interfaces; }
int main(int argc, char **argv) { struct ifi_info *ifihead; int sockfd; if (argc != 2) err_quit("usage: client <FileName>"); read_input_file(argv[1]); // Print information about the IP address and network masks ifihead = Get_ifi_info_plus(AF_INET, 1); prifinfo_plus(ifihead); // Create a socket that connects to the server sockfd = create_socket(); receive_file(sockfd); return 0; }
Interface* discoverInterfaces(Config *config, bool shouldBind) { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; bool insert = false; int server_fd = 0; // Create memory for the list. Interface *list = NULL; // Use crazy code to loop through the interfaces for (ifihead = ifi = Get_ifi_info_plus(AF_INET, 1); ifi != NULL; ifi = ifi->ifi_next) { // Create node Interface *node = malloc(sizeof(Interface)); insert = false; // Null out pointers node->next = NULL; node->prev = NULL; // Copy the name of the interface strcpy(node->name, ifi->ifi_name); // Copy the IPAddress if((sa = ifi->ifi_addr) != NULL) { strcpy(node->ip_address, Sock_ntop_host(sa, sizeof(*sa))); } // Copy the network mask if((sa = ifi->ifi_ntmaddr) != NULL) { strcpy(node->network_mask, Sock_ntop_host(sa, sizeof(*sa))); } // Print out info info("<%s> [%s%s%s%s%s\b] IP: %s Mask: %s\n", node->name, ifi->ifi_flags & IFF_UP ? "UP " : "", ifi->ifi_flags & IFF_BROADCAST ? "BCAST " : "", ifi->ifi_flags & IFF_MULTICAST ? "MCAST " : "", ifi->ifi_flags & IFF_LOOPBACK ? "LOOP " : "", ifi->ifi_flags & IFF_POINTOPOINT ? "P2P " : "", node->ip_address, node->network_mask); if(shouldBind) { /* Config was successfully parsed; attempt to bind sockets */ server_fd = createServer(node->ip_address, config->port); if(server_fd != SERVER_SOCKET_BIND_FAIL) { node->sockfd = server_fd; insert = true; } else { /* Unable to bind socket to port */ fprintf(stderr, "Failed to bind socket: %s:%u\n", node->ip_address, config->port); } } else { insert = true; } // If all was successful insert into the list if(insert) { if(list == NULL) { list = node; } else { // Just push it down the list // IE: It goes in reverse discovery order node->next = list; list->prev = node; list = node; } } } free_ifi_info_plus(ifihead); return list; }
int check_if_local() { struct ifi_info *ifi, *ifihead; struct sockaddr *ip_addr, *ntm_addr; struct sockaddr_in clientAddr, clientMask; int isLocal = 0; int isSameHost = 0; memset((char *) &IPServer, 0, sizeof(IPServer)); IPServer.sin_family = AF_INET; IPServer.sin_port = htons(cdata.server_port_no); IPServer.sin_addr.s_addr = inet_addr(cdata.server_ip); for (ifihead = ifi = Get_ifi_info_plus(AF_INET, 1); ifi != NULL; ifi = ifi->ifi_next) { if ((ip_addr = ifi->ifi_addr) != NULL) { printf("\n*************************************************************************\n"); printf("\n[INFO]Client IP address = %s\n", Sock_ntop_host(ip_addr,sizeof(*ip_addr))); //printf("\nServer IP address = %s\n", Sock_ntop_host(&IPServer,sizeof(IPServer))); clientAddr.sin_addr.s_addr = inet_addr(Sock_ntop_host(ip_addr,sizeof(*ip_addr))); if(IPServer.sin_addr.s_addr == clientAddr.sin_addr.s_addr) // same host { printf("\n[INFO]Client and server running on same host\n"); IPClient.sin_family = AF_INET; IPClient.sin_addr.s_addr = inet_addr("127.0.0.1"); IPServer.sin_addr.s_addr = inet_addr("127.0.0.1"); isLocal = 1; isSameHost = 1; } else { //printf("Client and server running on different hosts\n"); if(isLocal == 0) { IPClient.sin_family = AF_INET; IPClient.sin_addr.s_addr = inet_addr(Sock_ntop_host(ip_addr,sizeof(*ip_addr))); } } } if((ntm_addr = ifi->ifi_ntmaddr) != NULL) { clientAddr.sin_addr.s_addr = inet_addr(Sock_ntop_host(ip_addr,sizeof(*ip_addr))); clientMask.sin_addr.s_addr = inet_addr(Sock_ntop_host(ntm_addr,sizeof(*ntm_addr))); struct in_addr client_network; struct in_addr server_network; // bitwise AND of ip and netmask gives the network client_network.s_addr = clientAddr.sin_addr.s_addr & clientMask.sin_addr.s_addr; server_network.s_addr = IPServer.sin_addr.s_addr & clientMask.sin_addr.s_addr; printf("\n[INFO]Client Network Mask = %s\n", Sock_ntop_host(ntm_addr,sizeof(*ntm_addr))); printf("\n[INFO]Client Network Address = %s\n", inet_ntoa(client_network)); //printf("Network address of server = %s\n", inet_ntoa(server_network)); if(client_network.s_addr == server_network.s_addr && isSameHost == 0) { printf("\n[INFO]Client and server running on the same network\n"); IPClient.sin_family = AF_INET; IPClient.sin_addr.s_addr = clientAddr.sin_addr.s_addr; isLocal = 1; } else if(isSameHost == 0) { printf("\n[INFO]Client and server running on different networks\n"); IPClient.sin_family = AF_INET; IPClient.sin_addr.s_addr = inet_addr(Sock_ntop_host(ip_addr,sizeof(*ip_addr))); } } } printf("\n*************************************************************************\n"); return isLocal; }
int main(int argc, char **argv) { FILE *fp; char buf[MAX_LINE]; int count=0; int serv_port; int max_sending_window_size; const int on=1; struct socket_descripts sock_desc[10]; int sock_count = 0; struct ifi_info *ifi, *ifihead; int sockfd; struct sockaddr_in *sa; int i = 0; fp=fopen("server.in","r"); if(fp == NULL) { printf("ERROR MSG: No server.in file!\n"); exit(1); } while(fgets(buf, MAX_LINE -1, fp) != NULL) { if(count == 0) serv_port = atoi(buf); else if(count == 1) max_sending_window_size = atoi(buf); count++; } fclose(fp); for (ifihead = ifi = Get_ifi_info_plus( AF_INET, 1); ifi != NULL; ifi = ifi->ifi_next) { /* bind unicast adress */ sockfd = Socket(AF_INET, SOCK_DGRAM, 0); Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,&on, sizeof(on)); sa = (struct sockaddr_in *) ifi->ifi_addr; sa->sin_family = AF_INET; sa->sin_port = htons(serv_port); Bind(sockfd, (SA *) sa, sizeof(*sa)); sock_desc[sock_count].sockfd = sockfd; sock_desc[sock_count].ip_addr = sa; sock_desc[sock_count].net_mask = (struct sockaddr_in *)ifi->ifi_ntmaddr; //printf("SERVER: sockdesc[%d]\n", sock_count); printf("SERVER: IP: %s\n", Sock_ntop_host(sock_desc[sock_count].ip_addr, sizeof(struct in_addr))); printf("SERVER: Network mask: %s\n", Sock_ntop_host(sock_desc[sock_count].net_mask, sizeof(struct in_addr))); //printf("SERVER: sockdesc[%d]\n", sock_count); sock_count++; } int maxfd; fd_set rset; int nready; int len; char recvline[MAXLINE]; struct sockaddr_in cliaddr, child_server_addr, server_addr, sock_store; int n; int server_len; char IPserver[MAXLINE]; int pid; socklen_t serv_len, store_len; struct list_ips* r = NULL; maxfd=sock_desc[0].sockfd; for(i=0;i < sock_count; i++) { if(maxfd < sock_desc[i].sockfd) maxfd = sock_desc[i].sockfd; } for( ; ; ) { FD_ZERO(&rset); for(i = 0; i < sock_count; i++) FD_SET(sock_desc[i].sockfd, &rset); //printf("SERVER: waiting using select()\n"); if( (nready=select(maxfd+1, &rset, NULL, NULL, NULL)) < 0 ) { if (errno == EINTR){ continue; }else{ printf("ERROR MSG: server select error\n"); exit(0); } } for(i=0; i < sock_count; i++) { if(FD_ISSET(sock_desc[i].sockfd, &rset)) { char filename[MAXLINE]; int client_window_size; len = sizeof(cliaddr); //printf("SERVER: before revefrom func()\n"); n = recvfrom(sock_desc[i].sockfd, recvline, MAXLINE, 0, (SA *) &cliaddr, &len); sscanf(recvline, "%s %d", filename, &client_window_size); //printf("%s %d\n", filename, client_window_size); //printf("SERVER: first message is recevied: %s\n", recvline); r = search_list(cliaddr, NULL); if( r != NULL) continue; server_len = sizeof(server_addr); if( getsockname( sock_desc[ i ].sockfd, (SA *)&server_addr, &server_len ) < 0 ) { printf( "SERVER: getsockname error\n" ); exit(1); } inet_ntop( AF_INET, &(server_addr.sin_addr), IPserver, MAXLINE); //printf("SERVER: IPserver after receving filename: %s\n", IPserver); inet_pton( AF_INET, IPserver, &child_server_addr.sin_addr ); child_server_addr.sin_family = AF_INET; child_server_addr.sin_port = htonl(0); add_list(cliaddr); if ( (pid = fork() ) == 0) { int j; for(j=0; j < sock_count; j++) { if(i!=j) close(sock_desc[j].sockfd); } //printf("%d %d\n", max_sending_window_size, client_window_size); max_sending_window_size = min(max_sending_window_size, client_window_size); handshake(sock_desc[i].sockfd,&child_server_addr, sizeof(child_server_addr), &cliaddr, sizeof(cliaddr), filename, max_sending_window_size ); del_list(cliaddr); exit(0); } } } } }