func main() { flag.Parse() ip_addr := net.ParseIP(flag.Arg(0)) fmt.Printf("Starting traceroute for ip = %s\n", flag.Arg(0)) for i := 0; i < MAX_HOP; i++ { resHop, err := Hop(1337+i, i+1, ip_addr) if err != nil { log.Errorf("[TTL: %d] %q", i+1, err) } if resHop.Success() { GeoIp := geoip.TelizeRequest{IP: resHop.Addr} result, err := GeoIp.GetGeo() if err != nil { log.Errorf("[TTL: %d] %q", i+1, err) } if result.Type == geoip.RES_SUCCESS { fmt.Print(resHop.TTL, " \t ", resHop.Addr.String(), " \t [", result.GeoInfo.Country, " ", result.GeoInfo.City, " ", result.GeoInfo.Asn, "]\n") } else { fmt.Print(resHop.TTL, " \t ", resHop.Addr.String(), " \t [", result.Error.Message, "]\n") } } else { fmt.Print(resHop.TTL, " \t * * *\n") } } }
// NotifyCallback handles posting a JSON back to the URI given with the task. func NotifyCallback(task *types.EremeticTask) { if len(task.CallbackURI) == 0 { return } cbData := callbackData{ Time: task.Status[len(task.Status)-1].Time, Status: task.Status[len(task.Status)-1].Status, TaskID: task.ID, } body, err := json.Marshal(cbData) if err != nil { log.Errorf("Unable to create message for task %s, target uri %s", task.ID, task.CallbackURI) return } go func() { _, err = http.Post(task.CallbackURI, "application/json", bytes.NewBuffer(body)) if err != nil { log.Error(err.Error()) } else { log.Debugf("Sent callback to %s", task.CallbackURI) } }() }
// Run the eremetic scheduler func Run(s *eremeticScheduler) { driver, err := createDriver(s) if err != nil { log.Errorf("Unable to create scheduler driver: %s", err) return } defer close(s.shutdown) defer driver.Stop(false) if status, err := driver.Run(); err != nil { log.Errorf("Framework stopped with status %s and error: %s\n", status.String(), err.Error()) } log.Info("Exiting...") }
func Hop(port, ttl int, IP_addr net.IP) (*Hop_ret, error) { ret_addr := net.IPv4(0, 0, 0, 0) success := false // make sockets send_udp_s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP) if err != nil { return nil, err } recv_icmp_s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP) if err != nil { return nil, err } //editing TTL value for outgoing IPv4 packets if err := syscall.SetsockoptInt(send_udp_s, syscall.SOL_IP, syscall.IP_TTL, ttl); err != nil { return nil, err } tv := syscall.NsecToTimeval(1000 * 1000 * TIME_OUT_MS) syscall.SetsockoptTimeval(recv_icmp_s, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &tv) defer syscall.Close(send_udp_s) defer syscall.Close(recv_icmp_s) //connect sockets if err := syscall.Bind(recv_icmp_s, &syscall.SockaddrInet4{Port: port, Addr: [4]byte{137, 224, 226, 47}}); err != nil { return nil, err } //send udp-packet var IP [4]byte copy(IP[:], IP_addr.To4()) if err := syscall.Sendto(send_udp_s, []byte{0x42, 0x42}, 0, &syscall.SockaddrInet4{Port: 1337, Addr: IP}); err != nil { return nil, err } //receive ICMP recv_buffer := make([]byte, 4096) _, _, err = syscall.Recvfrom(recv_icmp_s, recv_buffer, 0) if err == nil { header, err := ipv4.ParseHeader(recv_buffer) if err != nil { log.Errorf("%q", err) } success = true ret_addr = header.Src } else { //time out success = false ret_addr = net.IPv4(0, 0, 0, 0) //log.Errorf("%q", err) } //resolve (timeout) errors, retry or return false... return &Hop_ret{Addr: ret_addr, TTL: ttl, success: success}, nil }
func handleError(err error, w http.ResponseWriter, message string) { if err == nil { return } log.Debugf("%v", err.Error()) var errorMessage = struct { Error string `json:"error"` Message string `json:"message"` }{ err.Error(), message, } if err = writeJSON(422, errorMessage, w); err != nil { log.Errorf("%v", err.Error()) panic(err) } }
func (s *eremeticScheduler) FrameworkMessage( driver sched.SchedulerDriver, executorID *mesos.ExecutorID, slaveID *mesos.SlaveID, message string) { log.Debug("Getting a framework message") switch *executorID.Value { case "eremetic-executor": var result interface{} err := json.Unmarshal([]byte(message), &result) if err != nil { log.Errorf("Error deserializing Result: [%s]", err) return } log.Debug(message) default: log.Debugf("Received a framework message from some unknown source: %s", *executorID.Value) } }
func ReconcileTasks(driver sched.SchedulerDriver) *Reconcile { cancel := make(chan struct{}) done := make(chan struct{}) go func() { var ( c uint delay int ) tasks, err := database.ListNonTerminalTasks() if err != nil { log.Errorf("Failed to list non-terminal tasks: %s", err) close(done) return } log.Infof("Trying to reconcile with %d task(s)", len(tasks)) start := time.Now() for len(tasks) > 0 { select { case <-cancel: log.Info("Cancelling reconciliation job") close(done) return case <-time.After(time.Duration(delay) * time.Second): // Filter tasks that has received a status update ntasks := []*types.EremeticTask{} for _, t := range tasks { nt, err := database.ReadTask(t.ID) if err != nil { log.Warnf("Task %s not found in database", t.ID) continue } if nt.LastUpdated().Before(start) { ntasks = append(ntasks, &nt) } } tasks = ntasks // Send reconciliation request if len(tasks) > 0 { var statuses []*mesos.TaskStatus for _, t := range tasks { statuses = append(statuses, &mesos.TaskStatus{ State: mesos.TaskState_TASK_STAGING.Enum(), TaskId: &mesos.TaskID{Value: proto.String(t.ID)}, SlaveId: &mesos.SlaveID{Value: proto.String(t.SlaveId)}, }) } log.Debugf("Sending reconciliation request #%d", c) driver.ReconcileTasks(statuses) } if delay < maxReconciliationDelay { delay = 10 << c if delay >= maxReconciliationDelay { delay = maxReconciliationDelay } } c += 1 } } log.Info("Reconciliation done") close(done) }() return &Reconcile{ cancel: cancel, done: done, } }