xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/os/ustime.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 /*
3  * lib/krb5/os/ustime.c
4  *
5  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  * krb5_crypto_us_timeofday() does all of the real work; however, we
28  * handle the time offset adjustment here, since this is context
29  * specific, and the crypto version of this call doesn't have access
30  * to the context variable.  Fortunately the only user of
31  * krb5_crypto_us_timeofday in the crypto library doesn't require that
32  * this time adjustment be done.
33  */
34 
35 #include <k5-int.h>
36 
37 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
38 krb5_us_timeofday(context, seconds, microseconds)
39     krb5_context context;
40     krb5_int32 FAR *seconds;
41     krb5_int32 FAR *microseconds;
42 {
43     krb5_os_context os_ctx = context->os_context;
44     krb5_int32 sec, usec;
45     krb5_error_code retval;
46 
47     if (os_ctx->os_flags & KRB5_OS_TOFFSET_TIME) {
48 	    *seconds = os_ctx->time_offset;
49 	    *microseconds = os_ctx->usec_offset;
50 	    return 0;
51     }
52     retval = krb5_crypto_us_timeofday(&sec, &usec);
53     if (retval)
54 	    return retval;
55     if (os_ctx->os_flags & KRB5_OS_TOFFSET_VALID) {
56 	    usec += os_ctx->usec_offset;
57 	    if (usec > 1000000) {
58 		    usec -= 1000000;
59 		    sec++;
60 	    }
61 	    if (usec < 0) {
62 		usec += 1000000;
63 		sec--;
64 	    }
65 	    sec += os_ctx->time_offset;
66     }
67     *seconds = sec;
68     *microseconds = usec;
69     return 0;
70 }
71