SCNetworkConnect
SCNetworkConnect
- Subject: SCNetworkConnect
- From: Stefan Lehrner <email@hidden>
- Date: Sun, 30 Sep 2007 23:00:19 +0200
Hi Folks,
I have a Objective-C Application which lets me get the status of a
PPP connection! I am new to Objective-C and ask you for help
solving this:
Actually, when I enter "./PPPConnect -s" I will get the status of the
connection
I want to get the sent and received bytes and the connection time.
I found the SCNetworkConnection API - the following should help me
solving this, but I have no idea how I can
adapt my file:
The EndResult should be:
In Terminal:
when I enter "./PPPConnect -t" - it shall show me the Connection time
when I enter "./PPPConnect -i" - the Incoming bytes
when I enter "./PPPConnect -o" - the Outgoing bytes
I do not want you write this for me, but any hints/advices are welcome!
For the sent/received bytes:
SCNetworkConnectionCopyStatistics
CFDictionaryRef SCNetworkConnectionCopyStatistics (
SCNetworkConnectionRef connection );
Parameters
connection
The SCNetworkConnection to obtained statistics from.
Return Value
Returns the statistics dictionary. If NULL is returned, the error can
be retrieved using the SCError function.
Discussion
Returns the statistics of the SCNetworkConnection. A statistic
dictionary contains specific dictionaries with statistics for each
subcomponent of the service.
For example, a statistics dictionary will contain the following sub-
dictionaries, keys, and values:
PPP : BytesIn :
PPP : BytesOut : Contains the number of bytes going up into
(or coming out of) the network stack for
any networking protocol without the PPP
headers and trailers.
PPP : PacketsIn :
PPP : PacketsOut : Contains the number of packets going up into
(or coming out of) the network stack for
any networking protocol without the PPP
headers and trailers.
PPP : ErrorsIn :
PPP : ErrorsOut : Contains the number of errors going up into
(or coming out of) the network stack for
any networking protocol without the PPP
headers and trailers.
The statistics dictionary may be extended in the future to contain
additional information.
Availability
Introduced in Mac OS X v10.3.
For the ConnectTime:
CFDictionaryRef SCNetworkConnectionCopyExtendedStatus (
SCNetworkConnectionRef connection );
Parameters
connection
The SCNetworkConnection to obtain status from.
Return Value
Returns the status dictionary. If NULL is returned, the error can be
retrieved using the SCError function.
Discussion
Returns the extended status of the connection. An extended status
dictionary contains specific dictionaries describing the status for
each subcomponent of the service.
For example, a status dictionary will contain the following sub-
dictionaries, keys, and values:
IPv4 : Addresses : the assigned IP address.
PPP : Status : the PPP-specific status of type
SCNetworkConnectionPPPStatus.
LastCause : Available when the status is "Disconnected"
and contains the last error associated with
connecting or disconnecting.
ConnectTime : the time when the connection was
established.
My Code so far:
#include <SystemConfiguration/SCNetworkConnection.h>
SCNetworkConnectionContext gScncCtx;
SCNetworkConnectionStatus gStat = kSCNetworkConnectionInvalid;
const char *statusMsg (SCNetworkConnectionStatus stat)
{
static const char *statusString[] = {
"kSCNetworkConnectionInvalid",
"kSCNetworkConnectionDisconnected",
"kSCNetworkConnectionConnecting",
"kSCNetworkConnectionConnected",
"kSCNetworkConnectionDisconnecting"
};
const char *msg = NULL;
if (kSCNetworkConnectionInvalid <= stat && stat <=
kSCNetworkConnectionDisconnecting)
msg = statusString [stat + 1];
else
msg = "Unknown status";
return msg;
}
void calloutProc (
SCNetworkConnectionRef connection,
SCNetworkConnectionStatus status,
void *info )
{
gStat = status;
}
int main (int argc, const char *argv []) {
SCNetworkConnectionStatus target = kSCNetworkConnectionInvalid;
Boolean force = FALSE;
int mainRes = 0;
if (argc == 2) {
switch (argv [1][0]) {
case 'c':
target = kSCNetworkConnectionConnected;
break;
case 'f':
force = TRUE;
case 'd':
target = kSCNetworkConnectionDisconnected;
break;
case 's':
target = 4;
break;
case 't':
target = 5;
break;
case 'o':
target = 6;
break;
case 'i':
target = 7;
}
}
if (target == kSCNetworkConnectionInvalid) {
printf ("usage : %s {c | d | f | s | t | o | i} -> connect |
disconnect | force-disconnect | status | time | outgoing | incoming
\n", argv[0]);
mainRes = 1;
}
else {
const char *msg = NULL;
int err = kSCStatusOK;
CFStringRef serviceID;
CFDictionaryRef userOptions;
Boolean res = SCNetworkConnectionCopyUserPreferences (NULL,
&serviceID, &userOptions);
if (!res) {
msg = "SCNetworkConnectionCopyUserPreferences failed";
err = SCError ();
mainRes = 1;
}
else {
SCNetworkConnectionRef scncRef =
SCNetworkConnectionCreateWithServiceID (NULL, serviceID, calloutProc,
&gScncCtx);
if (scncRef == NULL) {
msg = "SCNetworkConnectionCreateWithServiceID failed";
err = SCError ();
mainRes = 1;
}
else {
gStat = SCNetworkConnectionGetStatus (scncRef);
if (target == 4) {
msg = statusMsg (gStat);
mainRes = 0;
}
else if (gStat == target) {
msg = "Already in requested state";
mainRes = 0;
}
else if (gStat != kSCNetworkConnectionDisconnected && gStat !=
kSCNetworkConnectionConnected) {
msg = "SCNetworkConnectionGetStatus returned inappropriate status";
err = SCError ();
mainRes = 1;
}
else {
switch (target) {
case kSCNetworkConnectionConnected:
res = SCNetworkConnectionStart (scncRef, NULL, TRUE);
break;
case kSCNetworkConnectionDisconnected:
res = SCNetworkConnectionStop (scncRef, force);
break;
default:
res = FALSE;
}
if (!res) {
msg = "SCNetworkConnectionStart/Stop failed";
err = SCError ();
mainRes = 1;
}
else {
int count = 10;
while (TRUE) {
sleep (1);
gStat = SCNetworkConnectionGetStatus (scncRef);
if (gStat == target) {
mainRes = 0;
break;
}
--count;
if (count < 1) {
err = SCError ();
mainRes = 1;
break;
}
}
msg = statusMsg (gStat);
}
}
}
}
if (err != kSCStatusOK) {
const char *errMsg = SCErrorString (err);
printf ("%s\n", errMsg);
}
if (msg != NULL)
printf ("%s\n", msg);
}
return mainRes;
}
Thanks a lot for your time!
Stefan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden