Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
  
  struct sockaddr_in saddr, caddr;
  int port;
  int s;
  char line[MAX_LINE_LEN + 1];
  int nReceived;
  uint caddrLen;
  history h;
  
  if(argc != 2) {
    fprintf(stderr, "Usage: %s <port>\n", argv[0]);
    return 1;
  }
  port = atoi(argv[1]);
  if(port == 0) {
    fprintf(stderr, "Invalid port\n");
    return 1;
  }
  saddr.sin_port = htons(port);
  saddr.sin_family = AF_INET;
  saddr.sin_addr.s_addr = INADDR_ANY;
  
  s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if(s < 0) {
    perror("Impossible to create socket");
    return 1;
  }
  
  if(bind(s, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    perror("Impossible to bind");
    return 1;
  }
  h = historyCreate();
  
  while(1) {
    memset(line, 0, MAX_LINE_LEN + 1);
    caddrLen = sizeof(caddr);
    nReceived = recvfrom(s, line, MAX_LINE_LEN, 0, (struct sockaddr *)&caddr, &caddrLen);
    //sleep(4);
    printf("Received from %s: %s\n", inet_ntoa(caddr.sin_addr), line);
    if(historyCheck(&h, inet_ntoa(caddr.sin_addr)) >= MAX_REQUESTS) {
      printf("banned\n");
      continue;
    }
    
    sendto(s, line, nReceived, 0, (struct sockaddr *)&caddr, caddrLen);
    printf("Sent response\n");
  }
  
  close(s);
  return 0;
}
void searchParmCreate (tSearchParm * s, tDiskParm * d, tSectorId givenSector) {
        s->sTrack = givenSector / d->nSectors;
        s->sSector = givenSector % d->nSectors;
        s->history = historyCreate (d->nSectors, d->nTracks);
        s->time = 0;
        }