示例#1
0
void removeCurrentHost(int num_ss)
{

    char *ip = getCurrentIP();
    FILE *ifp = fopen("chainlist.txt", "r");
    FILE *ofp = fopen("chainlist.txt.new", "w");
    char *line = calloc(30, sizeof(char));
    char *backup_line = calloc(30, sizeof(char));
    char *compIP;


    fgets(line, 30, ifp);

    fprintf(ofp, "%d\n", num_ss);

    while (fgets(line, 30, ifp))
    {
        memcpy(backup_line, line, 30);
        compIP = parseIP(line);
        /* Only write to the new file if it's not the current IP address */
        if (strcmp(ip, compIP) != 0)
        {
            fprintf(ofp, "%s", backup_line);
        }
        memset(line, 0, 30);
        memset(backup_line, 0, 30);
    }
    fclose(ifp);
    fclose(ofp);
    rename("chainlist.txt.new", "chainlist.txt");
}
示例#2
0
文件: snmp.c 项目: kenchan13579/SNMP
// Show agents current interface that has an ip
// MIB OID used: ipAdEntAddr
void showInteferfaces() {
  int monitor_index = 0; // this records the last interfaces that should be monitored
 struct interfaces ifs[10];
 char *oid ="ipAdEntAddr" ;
 int counter = 0 ;
 snmpbulkget(oid); // bulkget
 printf("-------------------------Interfaces-------------------------\n");
 printf("Number --> IP\n");
  if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
    // 1st forloop to get the ip address ::ipAdEntAddr
    for ( vars = response->variables;vars; vars = vars->next_variable) {
      if ( vars->type == ASN_IPADDRESS) {
        char tmp[50] ;
        strcpy(ifs[counter++].ipaddress , parseIP(tmp)); // add to if struct
        int cp = strcmp(tmp , "127.0.0.1");
        if (cp!=0) {
          monitor_index = counter;
          strcpy(monitor.ipaddress , tmp);
        }
        if (counter >= 10 ) {
          printf("Too many interfaces.\n");
          vars = vars->next_variable;
          break;
        }
      } else {
        counter = 0 ; // reset counter to 0
        break;
      }
   } // end 1st loop
   // 2nd forloop to get the if index IP-MIB::ipAdEntIfIndex
   for ( vars ;vars; vars = vars->next_variable) {
      if ( vars->type == ASN_INTEGER) {
        ifs[counter++].ifIndex = (int) *(vars->val.integer);
        if (counter == monitor_index) {
          monitor.ifIndex = (int) *(vars->val.integer);
        }
         if (counter >= 10 ) {
          printf("Too many interfaces.\n");
          break;
        }
      } else {
        break;
      }
   } // end 2nd loop
  } else {
    errHandles(status);
  }
 cleanup();
 // display the table
  counter--;
  while ( counter >= 0 ) {
    printf("%i  -->  %s\n" , ifs[counter].ifIndex, ifs[counter].ipaddress);
    counter--;
  }
  printf("------------------------------------------------------------\n");
  printf("\n\n");
}
示例#3
0
int main(void)
{ 	  	
	while(1){
		char *uptime = parseUptime();
		char *load = parseLoadtime();
  		char *CPU = parseTemp();
		char *IP = parseIP();
		checkThreshold(CPU);
		
		char postit[POST_SIZE];
		snprintf(postit, POST_SIZE, "Up: %s, Load: %s, CPU: %s, IP: %s", uptime, load, CPU, IP);
		post(postit);
		
		sleep(90);
	}
	return 0;
}
示例#4
0
文件: snmp.c 项目: kenchan13579/SNMP
// Show agent's nieghbor and Ips
// mib object used : ipNetToMediaIfIndex , ipNetToMediaNetAddress
void showNeighbor() {
  char ifIndex_oid[50] = "ipNetToMediaIfIndex";
  char ip_oid[50] = "ipNetToMediaNetAddress";
  printf("-----------------------Neighbor----------------------\n");
  printf("Interface  -->  Neighbor\n");
     while ( true ) {

    snmpgetnext(ifIndex_oid);
    int index ;
    char *ip ;
    vars  = response->variables;

    if ( vars->type == ASN_INTEGER) {
      char tmp[50] ;
     snprint_objid(tmp,50,vars->name,vars->name_length); // this returns response oid
      strcpy(ifIndex_oid,tmp); // update the oid for next getnext
      index = (int) *(vars->val.integer);
    } else {
      break;
    }
    cleanup();
    snmpgetnext(ip_oid);
    vars = response->variables;
    if ( vars->type == ASN_IPADDRESS) {
      char tmp[50];
      char oidTemp[50];
      snprint_objid(oidTemp,50,vars->name,vars->name_length);//this returns response oid
      strcpy(ip_oid,oidTemp); // update the oid for next getnext
      ip = parseIP(tmp);
    } else {
      break;
    }
    printf("%i  -->  %s\n" , index , ip);
    cleanup();
  }
  printf("----------------------------------------------------\n\n\n");
}
示例#5
0
int recvResult(int fd, struct actionParameters *ap,struct array * results){
	int counter=0, res;
	struct flEntry file;///, *f;
	struct array * results2;
	char port[7];
	char ipstr[56];
	flushBuf(&ap->combuf);
	/* I don't flush results on purpose  */
	/* get a line */
	logmsg(ap->semid, ap->logfd, LOGLEVEL_WARN, "before loop%lu\n\n", 
      results->itemcount);
	while (( res = getTokenFromStream(fd, &ap->combuf, &ap->comline, 
					"\n", "\r\n",NULL ))){
		/* get first word -> ip */
		strncpy(ipstr, (char *)ap->comline.buf, 56);
		if (res ==  -1) return -3;
		logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) ip: %s\n", ipstr);

		/* get second word -> port */
		if (getTokenFromBuffer(&ap->combuf, &ap->comline, " ", "\n",NULL ) == -1)
			return -3;
		strncpy(port, (char *)ap->comline.buf, 7);
		logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) port: %s\n", 
        port);

		if (parseIP(ipstr, (struct sockaddr *)&file.ip, port, 0) == -1)
			return -3;

		/* get third word -> filename */
		if (getTokenFromBuffer( &ap->combuf, &ap->comline, " ", "\n",NULL ) == -1)
			return -3;
		strcpy(file.filename, (char *)ap->comline.buf);
		logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) filename: %s\n",
			 	file.filename);

		/* get fourth word -> size */
		if (getTokenFromBuffer( &ap->combuf, &ap->comline, " ", "\n",NULL ) == -1)
			return -3;
    long size;
		if ((size = my_strtol((char *)ap->comline.buf)) < 0 || errno) return -3;
    file.size = size;
		logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "(recvResult) size: %d\n", 
        file.size);

		logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, "got this: %s\n%d\n%s\n%lu\n", 
        putIP((struct sockaddr *)&file.ip), 
					getPort((struct sockaddr *) &file.ip), file.filename, file.size);

		results2 = addArrayItem(results, &file);
		if (results2) results = results2;
		else {
			logmsg(ap->semid, ap->logfd, LOGLEVEL_FATAL, 
          "couldn't resize Resultsarray");
			return -3;
		}

		file = *((struct flEntry *)getArrayItem(results, counter));
		logmsg(ap->semid, ap->logfd, LOGLEVEL_DEBUG, 
				"Array Memory got this: %s\n%d\n%s\n%lu - entry no %d\n",
			 	putIP((struct sockaddr *)&file.ip), getPort((struct sockaddr *) &file.ip),
				file.filename, file.size, results->itemcount);
		counter++;
	}
	return counter;
}
PyObject *
captureObject_next(captureObject *self)
{
    u_char *packet = NULL;
    struct pcap_pkthdr header;
    PyObject *ethernet = NULL;
    PyObject *result = NULL;
    PyObject *resultTuple = NULL;
    ethernet_t *eth_header = NULL;
    char buffer[255];
    double packetTime;
    int packet_offset = 0;
    PyObject *remaining = NULL;
    int offset[5] = {
        0, 0, 0, 0, 0
    };
    

    packet = (u_char *) pcap_next(self->pcap, &header);
    if (! packet)
    {
        Py_INCREF(Py_None);
        return Py_None;

        //        PyErr_SetString(ErrorObject, "No data available before timeout");
        //        return NULL;
    }
    sprintf(buffer, "%ld.%ld", (long) header.ts.tv_sec, (long) header.ts.tv_usec);
    packetTime = strtod(buffer, NULL);
    result = PyList_New(0);
    switch (pcap_datalink(self->pcap))
    {
        case DLT_EN10MB:
        {
            eth_header = (ethernet_t *) packet;
            ethernet = (PyObject *) PyProtocol_newEthernetObjectFromPacket(eth_header, &packet_offset);
            offset[0] = packet_offset;
            PyList_Append(result, ethernet);
            Py_DECREF(ethernet);

            switch (ntohs(eth_header->ether_type))
            {
                case ETHERTYPE_IP:
                {
                    if (! parseIP(result, header, packet, &packet_offset))
                    {
                        Py_DECREF(result);
                        return NULL;
                    }
                    offset[1] = packet_offset;
                    break;
                }
                case ETHERTYPE_ARP:
                    if (! parseARP(result, header, packet, &packet_offset))
                    {
                        Py_DECREF(result);
                        return NULL;
                    }
                    offset[2] = packet_offset;
                    break;
                default:
                {
                    
                }
            }
            break;
        }
        case DLT_NULL:
        {
            packet_offset = 4;
            if (! parseIP(result, header, packet, &packet_offset))
            {
                Py_DECREF(result);
                return NULL;
            }
            offset[3] = packet_offset;
            break;
        }
    }
    if ((int) (header.len) - packet_offset < 0)
    {
        Py_DECREF(result);
        PyErr_Format(ErrorObject, "Parsed parsed end of packet (%d %d %d %d)",
                        offset[0], offset[1], offset[2], offset[3]);
        return NULL;
    }
    remaining = PyString_FromStringAndSize((char *) (packet + packet_offset),
                                                     header.len - packet_offset);
    PyList_Append(result, remaining);
    Py_DECREF(remaining);
    
    
    PyList_Append(result, PyFloat_FromDouble(packetTime));
    resultTuple = PyList_AsTuple(result);
    Py_DECREF(result);
    return resultTuple;
}
示例#7
0
void client(char *next_ss_info, char *url)
{

    char *mutable_info = calloc(strlen(next_ss_info), sizeof(char));
    memcpy(mutable_info, next_ss_info, strlen(next_ss_info));

    int sockfd, status, connect_status;
    struct sockaddr_in serv_addr;
    struct addrinfo hints;
    struct addrinfo *servinfo;
    char *ip = parseIP(mutable_info);
    if (DEBUG) printf("Parsed IP address: %s\n", ip);
    memset(mutable_info, 0, strlen(next_ss_info));
    memcpy(mutable_info, next_ss_info, strlen(next_ss_info));
    char *port = parsePort(mutable_info);
    if (DEBUG) printf("Parsed port: %s\n", port);

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;


    if ((status = getaddrinfo(ip, port, &hints, &servinfo)) != 0)
    {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

    sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);

    connect_status = connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
    if (connect_status < 0)
    {
        perror("Error trying to connect\n");
        return;
    }
    char *url_packet = calloc(strlen(url) + 6, sizeof(char));
    uint16_t url_length = strlen(url);
    url_length = htons(url_length);
    memcpy(url_packet + 4, &url_length, 2);
    memcpy(url_packet + 6, url, strlen(url));
    if (DEBUG) printf("Connected to socket, trying to send url packet\n");
    if (send(sockfd, url_packet, strlen(url) + 6, 0) < 0)
    {
        printf("Send failed\n");
        // figure out how to handle failed send
    }
    if (DEBUG) printf("Sent url packet\n");

    char *rec_buffer = calloc(500, sizeof(char));

    int recv_status = recv(sockfd, rec_buffer, 500, 0);

    char *tmp_buffer = calloc(100, sizeof(char));
    char *filename = "chainlist.txt";

    FILE *file;
    file = fopen(filename, "rb");

    uint32_t fileLength = getFileLength(filename);

    ///////////////// check size //////////////
    if (fileLength < 401)
    {
        //printf("File length under 400bytes\n");
        char *content = (char *) calloc(406, sizeof(char));

        uint16_t packetLength = fileLength;
        uint32_t fileLengthSend = htonl(fileLength);
        uint16_t packetLengthSend = htons(packetLength);

        memcpy(content + 0, &fileLengthSend, 4);
        memcpy(content + 4, &packetLengthSend, 2);
        char ch;
        int counter = 0;

        while ((ch = fgetc(file)) != EOF)
        {
            content[counter + 6] = ch;
            counter++;
        }

        if (send(sockfd, content, 6 + fileLength, 0) < 0)
        {
            printf("Send failed\n");
            abort();
        }
        recv_status = recv(sockfd, tmp_buffer, 500, 0);

        free(content);
    }
    else
    {
        /*File over 400 bytes*/
        //printf("File length over 400bytes\n");

        /*All parts of file will be 400 bytes*/

        /*Find out how many times file needs to be split*/
        int numberOfParts = fileLength / 400;
        /*Figure out last part of files size*/
        int sizeOfLastPart = fileLength % 400;
        /*Loop through the file the number of times it is split*/
        int i = 0;
        for (i; i <= numberOfParts; i++)
        {
            //if(DEBUG) printf("In iteration %d of the loop for breaking up packets\n", i);
            char *content;
            /*Last part of the file.  Could be smaller than 400 bytes*/
            if (numberOfParts == i)
            {
                //if(DEBUG) printf("Sending last part of file\n");
                content = (char *) calloc(6 + sizeOfLastPart, sizeof(char));
                /*Go to right position in file*/
                fseek(file, i * 400, SEEK_SET);

                /*Set variables for packet header*/
                uint16_t packetLength = 6 + sizeOfLastPart;
                uint32_t fileLengthSend = htonl(fileLength);
                uint16_t packetLengthSend = htons(packetLength);

                /*Fill buffer with correct information*/
                memcpy(content + 0, &fileLengthSend, 4);
                memcpy(content + 4, &packetLengthSend, 2);

                /*Write chars into buffer*/
                int j = 0;
                for (j; j < sizeOfLastPart; j++)
                {
                    char ch;
                    ch = fgetc(file);
                    content[6 + j] = ch;
                }

                if (send(sockfd, content, 6 + sizeOfLastPart, 0) < 0)
                {
                    printf("Send failed\n");
                    abort();
                }

                recv_status = recv(sockfd, tmp_buffer, 500, 0);

                //printf("send in loop iteration %d is:\n", i);
                char c;
                int k = 0;
                for (k = 0; k < sizeOfLastPart + 6; k++)
                {
                    printf("%c", content[k]);
                }


                free(content);
            }
                /*Middle of file where the parts are still 400 bytes*/
            else
            {
                //if(DEBUG) printf("Sending middle parts of file\n");
                char *content_buffer = calloc(406, sizeof(char));
                /*Go to right position in file*/
                fseek(file, i * 400, SEEK_SET);

                /*Set variables for packet header*/
                uint16_t packetLength = 400;
                uint32_t fileLengthSend = htonl(fileLength);
                uint16_t packetLengthSend = htons(packetLength);

                /*Fill buffer with correct information*/
                memcpy(content_buffer + 0, &fileLengthSend, 4);
                memcpy(content_buffer + 4, &packetLengthSend, 2);

                /*Write chars into buffer*/
                int j = 0;
                for (j; j < 400; j++)
                {
                    char ch;
                    ch = fgetc(file);
                    content_buffer[6 + j] = ch;

                    /******************************************/
                    if (i == 0)
                    {
                        //printf("Char at %d is %c\n", i, ch);
                    }
                    /******************************************/


                }


                if (send(sockfd, content_buffer, 406, 0) < 0)
                {
                    printf("Send failed\n");
                    abort();
                }

                recv_status = recv(sockfd, tmp_buffer, 500, 0);

                //printf("send in loop iteration %d is:\n", i);
                free(content_buffer);

            }
        }
    }


    printf("waiting for file...\n");


    uint32_t total_bytes_received = 0;
    uint32_t file_length = 1;
    uint32_t data_received = 0;
    unsigned int msg_length;
    char *ack = "Ack";

    outBuff = NULL;
    outBuff_size = 0;

    int counter = 0;
    while (data_received < file_length)
    {
        char *receiverBuffer = (char *) calloc(500, sizeof(char));
        msg_length = 0;
        recv_status = recv(sockfd, receiverBuffer, 500, 0);
        if (recv_status < 0)
        { perror("Error: receive failed\n"); }

        memcpy(&file_length, receiverBuffer + 0, 4);
        file_length = ntohl(file_length);
        memcpy(&msg_length, receiverBuffer + 4, 2);
        msg_length = ntohs(msg_length);

        if (outBuff == NULL)
        {
            int total_packets = file_length / 400;
            if (file_length % 400 > 0) total_packets++;
            //memset(&outBuff_size, 0, sizeof(uint32_t));
            //memset(&outBuff, 0, sizeof(uint32_t));
            outBuff_size = file_length + (total_packets * 6);
            outBuff = (char *) calloc(outBuff_size, sizeof(char));

            file_length += file_length + (total_packets * 6);
        }


        char *data = calloc(msg_length + 6, sizeof(char));
        memcpy(data, receiverBuffer, msg_length + 6);
        memcpy(outBuff + total_bytes_received, data, msg_length + 6);
        data_received += msg_length;
        total_bytes_received += msg_length + 6;

        free(data);
        memset(rec_buffer, 0, 500);

        /* send ack to add temporary to delay */
        if (send(sockfd, ack, strlen(ack), 0) < 0)
        {
            int send_status = send(sockfd, ack, strlen(ack), 0);
            while (send_status < 0)
            {
                send_status = send(sockfd, ack, strlen(ack), 0);
            }

        }
        counter++;
    }

    printf("Relaying file...\n");


    //printf("Goodbye!\n");

    return;
}
示例#8
0
void set_MaskIP(void) {
  uint8_t ipaddr_port[4];
  parseIP(cmdlineGetArgStr(1),ipaddr_port,4);
  config(GC_SAVE,ipaddr_port,GC_NETMASK);
}
示例#9
0
void set_RemoteIP(void) {
  uint8_t ipaddr_port[5];
  parseIP(cmdlineGetArgStr(1),ipaddr_port,5);
  config(GC_SAVE,ipaddr_port,GC_REMOTE_IP);  
  config(GC_SAVE,&ipaddr_port[4],GC_REMOTE_PORT);
}
示例#10
0
void set_LocalIP(void) {
  uint8_t ipaddr_port[5];
  parseIP(cmdlineGetArgStr(1),ipaddr_port,5);
  config(GC_SAVE,ipaddr_port,GC_LOCAL_IP);  
  config(GC_SAVE,&ipaddr_port[4],GC_LOCAL_PORT);
}