Skip to content

Commit

Permalink
Use a int64 for Unix time to make this code 2038 proof.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjeker committed Dec 19, 2023
1 parent 8ded2cb commit b8a26d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
12 changes: 6 additions & 6 deletions cmd/stayrtr/stayrtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func processData(vrplistjson []prefixfile.VRPJson,
// grab the current time every time it's invoked, time calls can be slow on
// some platforms, so lets just get the unix time when we start and use that
// to compare it all
NowUnix := time.Now().UTC().Unix()
NowUnix := time.Now().Unix()

var vrplist []rtr.VRP
var brklist = make([]rtr.BgpsecKey, 0)
Expand All @@ -235,7 +235,7 @@ func processData(vrplistjson []prefixfile.VRPJson,
if v.Expires != nil {
// Prevent stale VRPs from being considered
// https://github.com/bgp/stayrtr/issues/15
if int(NowUnix) > int(*v.Expires) {
if NowUnix > *v.Expires {
continue
}
}
Expand Down Expand Up @@ -297,7 +297,7 @@ func processData(vrplistjson []prefixfile.VRPJson,
if v.Expires != nil {
// Prevent stale VRPs from being considered
// https://github.com/bgp/stayrtr/issues/15
if int(NowUnix) > int(*v.Expires) {
if NowUnix > *v.Expires {
continue
}
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func processData(vrplistjson []prefixfile.VRPJson,
func handleASPAList(list []prefixfile.ASPAJson, NowUnix int64, aspalist []rtr.VAP, AFI uint8) []rtr.VAP {
for _, v := range list {
if v.Expires != nil {
if int(NowUnix) > int(*v.Expires) {
if NowUnix > *v.Expires {
continue
}
}
Expand Down Expand Up @@ -377,7 +377,7 @@ func (s *state) updateFromNewState() error {

buildtime, err := time.Parse(time.RFC3339, s.lastdata.Metadata.Buildtime)
if s.lastdata.Metadata.GeneratedUnix != nil {
buildtime, err = time.Unix(int64(*s.lastdata.Metadata.GeneratedUnix), 0), nil
buildtime, err = time.Unix(*s.lastdata.Metadata.GeneratedUnix, 0), nil
}
if s.checktime {
if err != nil {
Expand Down Expand Up @@ -422,7 +422,7 @@ func (s *state) reloadFromCurrentState() error {

buildtime, err := time.Parse(time.RFC3339, s.lastdata.Metadata.Buildtime)
if s.lastdata.Metadata.GeneratedUnix != nil {
buildtime, err = time.Unix(int64(*s.lastdata.Metadata.GeneratedUnix), 0), nil
buildtime, err = time.Unix(*s.lastdata.Metadata.GeneratedUnix, 0), nil
}
if s.checktime {
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/stayrtr/stayrtr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

func TestProcessData(t *testing.T) {
var stuff []prefixfile.VRPJson
NowUnix := int(time.Now().Unix())
ExpiredTime := 1337
NowUnix := time.Now().Unix()
ExpiredTime := int64(1337)

stuff = append(stuff,
prefixfile.VRPJson{
Expand Down Expand Up @@ -158,8 +158,8 @@ func TestJson(t *testing.T) {
t.Errorf("Unable to decode json: %v", err)
}

Ex1 := 1627568318
Ex2 := 1627575699
Ex1 := int64(1627568318)
Ex2 := int64(1627575699)

want := (&prefixfile.VRPList{
Metadata: prefixfile.MetaData{
Expand Down
10 changes: 6 additions & 4 deletions prefixfile/prefixfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ type VRPJson struct {
Length uint8 `json:"maxLength"`
ASN interface{} `json:"asn"`
TA string `json:"ta,omitempty"`
Expires *int `json:"expires,omitempty"`
Expires *int64 `json:"expires,omitempty"`
}

type MetaData struct {
Counts int `json:"vrps"`
CountASPAs int `json:"aspas"`
CountBgpSecKeys int `json:"aspas"`
Buildtime string `json:"buildtime,omitempty"`
GeneratedUnix *int `json:"generated,omitempty"`
GeneratedUnix *int64 `json:"generated,omitempty"`
}

func (md MetaData) GetBuildTime() time.Time {
Expand All @@ -39,7 +41,7 @@ type VRPList struct {

type BgpSecKeyJson struct {
Asn uint32 `json:"asn"`
Expires *uint32 `json:"expires,omitempty"`
Expires *int64 `json:"expires,omitempty"`
Ta string `json:"ta,omitempty"`

// Base32 encoded, but encoding/json handles this for us
Expand All @@ -58,7 +60,7 @@ type ProviderAuthorizationsJson struct {

type ASPAJson struct {
CustomerAsid uint32 `json:"customer_asid"`
Expires *uint32 `json:"expires,omitempty"`
Expires *int64 `json:"expires,omitempty"`
Providers []uint32 `json:"providers"`
}

Expand Down

0 comments on commit b8a26d2

Please sign in to comment.